@guigaib/ngx-logger
v0.2.0
Published
## Introduction A lightweight, configurable logger for Angular applications. Supports various log levels and optional color output.
Downloads
255
Readme
NgxLogger
Introduction
A lightweight, configurable logger for Angular applications. Supports various log levels and optional color output.
Installation
Install the package:
npm install @guigaib/ngx-logger
Usage
Import the LoggerModule and provide a custom configuration if desired:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { provideLogger, LoggerConfig, LogLevel } from '@guigaib/ngx-logger';
@NgModule({
imports: [BrowserModule],
providers: [
provideLogger({
level: LogLevel.DEBUG,
enableColors: true,
colors: {
[LogLevel.ERROR]: 'red',
[LogLevel.WARN]: 'yellow',
[LogLevel.INFO]: 'blue',
[LogLevel.DEBUG]: 'purple',
[LogLevel.TRACE]: 'gray',
},
} as LoggerConfig),
],
// ...existing code...
})
export class AppModule {}
Example
Inject and use the LoggerService in a component:
import { Component } from '@angular/core';
import { LoggerService } from '@guigaib/ngx-logger';
@Component({
selector: 'app-root',
template: `<!-- ...your template... -->`
})
export class AppComponent {
constructor(private logger: LoggerService) {}
ngOnInit(): void {
this.logger.info('AppComponent initialized.');
this.logger.debug('Debug level message.');
this.logger.error('An error occurred.');
}
}
Configuration
Use updateConfig to adjust settings at runtime:
this.logger.updateConfig({
level: LogLevel.INFO,
enableColors: false,
colors: { /* ...color changes... */ },
});
Structured Logging
If you want to pass structured data to your logs, you can use the StructuredLoggerInput interface:
this.logger.info({
message: 'Logging with prefix and suffix',
prefix: 'MyModule',
suffix: ['A', 'B'],
});