ngx-persistence-logger
v18.0.0
Published
This packages aims to take care of most of your logging concerns, including: - nicely formatted output that takes you directly to the error when developing - sending logs to a backend - a component where you can display and filter logs from multiple appli
Downloads
5
Readme
ngx-persistence-logger
This packages aims to take care of most of your logging concerns, including:
- nicely formatted output that takes you directly to the error when developing
- sending logs to a backend
- a component where you can display and filter logs from multiple applications
This library was built with customization in mind, so most things can easily be modified.
Usage
Create your logger service
Extend the base logger service:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BaseLoggerService } from 'ngx-persistence-logger';
import { environment } from '../../environments/environment';
/**
* Service that is responsible for logging.
*/
@Injectable({ providedIn: 'root' })
export class LoggerService extends BaseLoggerService {
protected override readonly LOG_URL: string = `${environment.apiUrl}/logs`;
protected override readonly APPLICATION_NAME: string = 'NgxPersistenceLoggerShowcase';
constructor(http: HttpClient) {
super(http);
}
}
(optional) Define a global logger variable
If you want to use this library just as easy as console.log you can provide a global object. That way you don't need to inject the service all the time:
import { Component } from '@angular/core';
import { LoggerService } from './services/logger.service';
export let logger: LoggerService;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private readonly loggerService: LoggerService) {
logger = this.loggerService;
}
}
WARNING:
Use the logger
That's it, now you can use the logger inside your code.
HEADS UP: This is not as straightforward as you might think:
//...
logger.info('This is a test log')();
//...
As you can see we have additional() after the info() call. This is not a mistake but unfortunately the only way that the browser recognizes the correct file and line number where the service was called.
Technically, the info method returns a binding to the console.info method that is then called with the second pair of brackets. You need to be careful to not forget the second brackets, as no errors will be shown if you forget them.
(optional) Use the global error handler
To log any uncaught erros, you can use the provided GlobalErrorHandler.
Provide your logger service
app.module.ts:
import { NGX_LOGGER_SERVICE, GlobalErrorHandler } from 'ngx-persistence-logger';
// ...
providers: [
// ...
{
provide: NGX_LOGGER_SERVICE,
useExisting: LoggerService
},
{
provide: ErrorHandler,
useClass: GlobalErrorHandler
}
// ...
]
// ...
Log History Component
To view all logs from different applications the library provides a built in component with filter functionality:
<ngx-log-history [configuration]="config"></ngx-log-history>
import { LogHistoryComponent, LogHistoryConfiguration } from 'ngx-persistence-logger';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
standalone: true,
imports: [
LogHistoryComponent
]
})
export class HomeComponent {
config: LogHistoryConfiguration = {
logsBaseUrl: 'http://localhost:3000/logs',
availableApplications: ['ShowcaseApplication', 'NgxPersistenceLoggerShowcase']
};
}