如何:使用其他方法扩展Aurelia Logger [英] How to: Extend the Aurelia Logger with additional methods

查看:111
本文介绍了如何:使用其他方法扩展Aurelia Logger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最重要的是我正在关注这个例子
实现和定制aurelia-logger。

Foremost I am following this example on implementing and customizing the aurelia-logger.

这种方法只能正常工作。对自定义appender中现有方法的任何更改都可以正常工作。但是,如果我将CustomLogAppender.js更改为

This approach only works fine. Any changes to the existing methods in the custom appender work fine. However if I change the CustomLogAppender.js to

export class CustomLogAppender {
  constructor(){}
  debug(logger, message, ...rest){
    console.debug(`DEBUG [${logger.id}] ${message}`, ...rest);
  }
  info(logger, message, ...rest){
    console.info(`INFO [${logger.id}] ${message}`, ...rest);
  }
  warn(logger, message, ...rest){
    console.warn(`WARN [${logger.id}] ${message}`, ...rest); 
  }
  error(logger, message, ...rest){
    console.error(`ERROR [${logger.id}] ${message}`, ...rest);
  }
  newMethod(logger, message, ...rest){
    //whatever
  }
}

然后 logger.newMethod(向后端发送错误); 在调用时不是定义的方法在视图模型中。
我正在尝试编写其他日志记录方法,如果我认为有必要,可以将警告,信息,错误等发送到后端进行数据库日志记录,但是在将这些方法添加到aurelia-logger时遇到问题。任何指导将不胜感激。

Then logger.newMethod("send error to backend"); is not a defined method when called in a view-model. I am trying to write additional logging methods that can send warns, info, errs, etc to the back-end for database logging should i deem it necessary but am having trouble adding these methods to the aurelia-logger. Any guidance would be appreciated.

推荐答案

如果您按照URL中的示例进行操作,则以下行与您的相关: https://github.com/aurelia/logging/blob/21d92e79a5f924b25b1eae0648af5a7a0ab44527/src/index .js#L96-L115

If you are following the example from the URL, the following lines are relevant for your: https://github.com/aurelia/logging/blob/21d92e79a5f924b25b1eae0648af5a7a0ab44527/src/index.js#L96-L115

正如您所看到的,它创建了一个记录器,它只有默认方法,即您返回的内容并不完全你的记录器。您需要做的是增强现有方法(在那里,您可以使用自定义方法)。

As you can see, it creates a logger, which only has the default methods, i.e. what you get returned is not exactly your logger. What you need to do is enhance the existing methods (in there, you can use your custom methods).

请参阅以下appender作为示例:

See the following appender as an example:

import { inject } from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
import {Logger} from 'aurelia-logging';

@inject(HttpClient)
export class ServerLogAppender {
    constructor(http) {
        this.http = http;
    }

    debug(logger, message, ...rest) {
        this.sendToServer(logger, message, ...rest);
    }

    info(logger, message,...rest){
        this.sendToServer(logger, message,...rest);
    }

    warn(logger, message,...rest){
        this.sendToServer(logger, message,...rest);
    }

    error(logger, message,...rest){
        this.sendToServer(logger, message,...rest);
    }

    sendToServer(logger, message,...rest){
        this.http.post('myerrorhandler', {
            url: window.location.href,
            source: logger.id,
            message: message,
            additional: rest.join('\r\n')
        });
    }
}

如您所见,默认方法(debug, info,warn,error)使用自定义方法 sendToServer()

As you can see, the default methods (debug, info, warn, error) use the custom method sendToServer().

这篇关于如何:使用其他方法扩展Aurelia Logger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆