Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override message object in ngx-logger

Is it possible to have ngx-logger use a different object to log? Currently all logs are based on the NGXLogInterface below:

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:

{
  "application": "string",
  "component": "string",
  "area": "string",
  "level": "string",
  "details": "string",
  "executingUser": "string",
}
like image 663
Cragly Avatar asked Dec 29 '25 21:12

Cragly


1 Answers

// this service responsible to send the request is NGXLoggerHttpService so you must provide another one to change the signature of the model.

1 - add the override the service in the same module which you instantiate the LoggerModule ==> LoggerModule.forRoot() ....

2 - Write the new service in charge ton send the request by extending the original one and overriding the logOnServer method

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

}
like image 158
dpassyann Avatar answered Dec 31 '25 18:12

dpassyann