@blaugold/angular-logger v0.0.3
Angular Logger
Logger for Angular 2 Apps.
Installation
npm i --save @blaugold/angular-loggerUsage
Include the LoggerModule and the ConsoleWriterModule at the root module.
@NgModule({
imports: [
LoggerModule.forStd(),
ConsoleWriterModule.forRoot()
]
})
export class AppModule {
}In your components, directives, pipes and services get the Logger through DI.
@Injectable()
export class MyService {
constructor(private log: Logger) {}
getSome() {
this.log.trace('Getting something for MyService')
}
}To change the log level at which the logger emits logs, type in the console logat.trace to set
the logger to log level Trace for example. Per default the logger is set to Info. You can also
permanently change the log level:
LoggerModule.forStd(new LoggerDef().level(LogLevel.Warn))Noop Logger
When testing classes which use a logger, but you are not interested in what is logged,
add LoggerModule.forStd() to the testing module. As long as there is no log consumer like the
ConsoleWriterModule imported the LoggerModule will inject a noop logger for the Logger token.
Package Logger
Packages which are included in other apps to provide some functionality can make use of aux loggers in their classes:
export const myPkgLogger = new LoggerDef('MyPkg')
@Injectable()
export class MyService {
constructor(@Inject(myPkgLogger) log: Logger) {
log.info('MyService was instantiated')
}
}
@NgModule({
imports: [
LoggerModule.forAux([myPkgLogger])
]
})
export class MyPkgModule {
}Consumers of this package can use myPkgLogger to set the log level of the package's logger.
If the consuming app or package does not register a LogConsumer the classes are injected with
a noop logger.