2

I have an Angular application and looking to add ngx-logger framework where I can configure a logging level for only a specific component/class.

So lets say if an Angular application has 5 different components/classes and the default Root level is set to INFO. Then I want to be able to set to DEBUG for only 1 component/class. This way I will only get debug messages for only this component/class and not the others. Is this possible?

This is the same functionality provided by Java's Log4j where it allows you to configure different log levels for specific packages.

If ngx-logger does not support this feature, anyone knows any other Angular logging framework that supports this?

Marquinio
  • 4,601
  • 13
  • 45
  • 68

1 Answers1

0

The log level is shared by the instance of the logger, so to have specific log levels per components/modules/etc... you need to provide a different instance

Example for a component

import { Component } from "@angular/core";
import { NGXLogger, NgxLoggerLevel } from "src/public_api";

@Component({
  templateUrl: './local-provider.component.html',
  providers: [NGXLogger],
})
export class LocalProviderComponent {

  constructor(public logger: NGXLogger) {
  }

  log(): void {
    this.logger.debug('Test');
  }

  changeLogLevel(): void {
    const config = this.logger.getConfigSnapshot()
    config.level = config.level === NgxLoggerLevel.TRACE ? NgxLoggerLevel.ERROR : NgxLoggerLevel.TRACE;
    this.logger.updateConfig(config);
  }
}

Full example can be found on the repo here : https://github.com/dbfannin/ngx-logger/tree/master/projects/not-a-singleton

bmtheo
  • 974
  • 1
  • 7
  • 16