Description
A logger module for Nestjs, built on top of node-bunyan.
Features
- A gloabl bunyan logger provider can be used in the controllers/services.
- Automatically log request/response with
request-id
,timestamp
,status-code
, etc.
Usage
Installation
Yarn
yarn add @wiredcraft/nestjs-bunayn-logger
NPM
npm install @wiredcraft/nestjs-bunayn-logger --save
Integration
- Import
LoggerModule
in the root App module, this provides initialized bunyan logger instance that is available to other modules by injection.
import { Module } from '@nestjs/common';
import { LoggerConfig, LoggerModule } from '@wiredcraft/nestjs-bunyan-logger';
import configuration from './src/configuration';
@Module({
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
LoggerModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
return configService.get<LoggerConfig>('logger', { infer: true });
},
inject: [ConfigService],
}),
],
})
export class AppModule {}
A sample configuration file is as below,
export default () => ({
logger: {
name: 'awesome-app',
streamType: 'FILE' | 'STDOUT',
path: './logs/app.log', // only available for `FILE` streamType
excludeReqPath: '/health', // the path that you want to skip logging
}
});
- Inject the logger instance to your service and use it as you want.
import { Injectable } from '@nestjs/common';
import { Logger, Bunyan } from '@wiredcraf/nestjs-bunyan-logger';
@Injectable()
export class CatService {
constructor(
@Logger() private logger: Bunyan,
) {
}
}
Development
Installation
$ yarn install
Publish
$ npm version major|minor|patch
$ npm publish