@wiredcraft/nestjs-bunyan-logger

A logger module for Nestjs, built on top of node-bunyan.

Usage no npm install needed!

<script type="module">
  import wiredcraftNestjsBunyanLogger from 'https://cdn.skypack.dev/@wiredcraft/nestjs-bunyan-logger';
</script>

README

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

  1. 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
  }
});

  1. 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

License

MIT licensed.