简体   繁体   English

我可以覆盖 ngx-logger 中的消息对象吗

[英]Can I override message object in ngx-logger

Is it possible to have ngx-logger use a different object to log?是否可以让 ngx-logger 使用不同的对象进行记录? Currently all logs are based on the NGXLogInterface below:目前所有的日志都是基于下面的NGXLogInterface:

export class NGXLogInterface {
  level: NgxLoggerLevel;
  timestamp: string;
  fileName: string;
  lineNumber: string;
  message: string;
  additional: any[];
}

However I need to send my logs to an API which is expecting a body like the one below:但是,我需要将我的日志发送到一个 API,该 API 需要如下所示的主体:

{
  "application": "string",
  "component": "string",
  "area": "string",
  "level": "string",
  "details": "string",
  "executingUser": "string",
}

// this service responsible to send the request is NGXLoggerHttpService so you must provide another one to change the signature of the model. // 这个负责发送请求的服务是 NGXLoggerHttpService 所以你必须提供另一个来改变模型的签名。

1 - add the override the service in the same module which you instantiate the LoggerModule ==> LoggerModule.forRoot() .... 1 - 在您实例化 LoggerModule ==> LoggerModule.forRoot() 的同一模块中添加覆盖服务 ....

2 - Write the new service in charge ton send the request by extending the original one and overriding the logOnServer method 2 - 通过扩展原始服务并覆盖 logOnServer 方法来编写负责发送请求的新服务

providers: [
  {provide: NGXLoggerHttpService, useClass: MyLoggerHttpService, multi: false}
]

@Injectable()
export class MyLoggerHttpService extends NGXLoggerHttpService {
  public constructor(private readonly _http: HttpBackend) {
    super(_http);
  }
  public logOnServer(url: string, log: NGXLogInterface, options: object): Observable<any> {
    const value: any = JSON.parse(log.message);
    return super.logOnServer(url, value as any, options );
  }

}

You need to customise the body in the logger by overriding the NGXLoggerServerService您需要通过覆盖 NGXLoggerServerService 来自定义记录器中的主体

Example :例子 :

@Injectable()
export class ServerCustomisedService extends NGXLoggerServerService {

  /**
   * Customse the data sent to the API
   * @param metadata the data provided by NGXLogger
   * @returns the data customised
   */
  public customiseRequestBody(metadata: INGXLoggerMetadata): any {
    let body = // edit the body as you like

    return body;
  }
}

And then provide your customised service, see how in the doc : https://github.com/dbfannin/ngx-logger/blob/master/docs/customising.md#example-adds-a-property-to-the-server-log然后提供您的定制服务,请参阅文档中的方法: https ://github.com/dbfannin/ngx-logger/blob/master/docs/customising.md#example-adds-a-property-to-the-server -日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM