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

时间:2019-08-01 11:29:38

标签: angular logging ngx-logger

是否可以让ngx-logger使用其他对象进行记录?当前,所有日志均基于以下NGXLogInterface:

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

但是,我需要将日志发送到期望以下内容的API:

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

1 个答案:

答案 0 :(得分:0)

// 这个负责发送请求的服务是 NGXLoggerHttpService 所以你必须提供另一个服务来改变模型的签名。

1 - 在实例化 LoggerModule ==> LoggerModule.forRoot() ....

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 );
  }

}