@tangerinelife/nestjs-logger
v0.0.6
Published
Nestjs fastify pino logger
Downloads
2
Readme
NestJS Logger
A pino based NestJS logger for fastify with axios logging, request Id tracking and ECS
Installation
npm install @tangerinelife/nestjs-logger
Usage
Register the logger module in app.module.ts.
import { Module } from '@nestjs/common';
import { LoggerModule } from '@tangerinelife/nestjs-logger';
@Module({
imports: [LoggerModule.forRoot({
service: process.env.APP_NAME,
logHttpRequest: true, // optional and true by default
logAxiosRequest: true, // optional and true by default
redactInUrl: [], // optionally specify query strings to redact from requests url
pinoOptions: {} // optionally pass in object for the underlying pino instance, sensible defaults has been set
pinoDestination: {} // optionally pass in destination object for the underlying pino instance, default to stdOut (1)
})]
})
export class AppModule {}
// you can also use forRootAsync to initialize the module
LoggerModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
service: configService.get('APP.NAME')
}),
inject: [ConfigService],
}),
With the logger module initialized, you can now inject Logger as a dependency into your controllers or services
import { Injectable } from '@nestjs/common';
import { Logger } from '@tangerinelife/nestjs-logger';
@Injectable()
export class AppService {
private readonly logger: Logger;
constructor(logger: Loggger) {
this.logger = logger;
this.logger.setContext(AppService.name) // optionally set the context for this logger
}
getHello(): string {
this.logger.info('Testing logging 123');
return 'Hello World!';
}
}
You can also use @InjectLogger(...) decorator to inject the logger, optionally passing in the context
import { Injectable } from '@nestjs/common';
import { Logger, InjectLogger } from '@tangerinelife/nestjs-logger';
@Injectable()
export class AppService {
constructor(@InjectLogger(AppService.name) private readonly logger: Loggger) {}
getHello(): string {
this.logger.info({ 'service': 'Tangerine' }, 'Testing injected logging with object');
return 'Hello World!';
}
}
Log error
To log error, pass it as the first paramater in an object literal like so:
logger.error({ err: new Error('boom') }, 'oops there is a problem')
Sensitive data filtering Pino has a redact option that is used to prevent sensitive data from being logged. It is optional and some sensible default has been set. The redactInUrl option also allows us specify query strings that should be redacted. Here are samples:
LoggerModule.forRoot({
service: process.env.APP_NAME,
redactInUrl: string['token', 'seckey'],
pinoOptions: {
redact: ['http.request.headers.authorization', 'http.request.headers["x-refresh-token"]', 'http.request.body.content.passcode']
}
})
That is all you need to use this package
Pino docs
Visit the official pino doc to learn more about pino https://getpino.io.
Contributing
Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.
Thanks
Thank you for choosing this library