nestjs-cls-logger
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

NestJS CLS Logger

Thanks very much for the excellent content below:

This is a simplified version of NestJS CLS Logger. Implementing console logging functionality while tracing the processing flow through Correlation IDs.

Also capable of sending 'fatal' and 'emergency' level information to a specified Slack webhook in the production environment

Install

npm i nestjs-cls-logger

Detailed usage examples

  1. Create a sample nestjs app
# install nest cli
npm i -g @nestjs/cli

# create sample app
nest new sample-app
cd sample-app
  1. Install this npm
npm i nestjs-cls-logger
  1. Import LoggerModule into AppModule
// ./src/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NestJsClsLoggerModule } from 'nestjs-cls-logger';

@Module({
  imports: [NestJsClsLoggerModule.forRoot({ tag: 'app.sample' })],
  // imports: [NestJsClsLoggerModule.forRoot({ tag: 'app.sample', overrideConsole: true })]
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  1. Regist logger at bootstrap()
// ./src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestjsLoggerServiceAdapter } from 'nestjs-cls-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(app.get(NestjsLoggerServiceAdapter));
  await app.listen(3000);
}
bootstrap();
  1. Use Logger in Service
// ./src/app.service.ts

import { Injectable } from '@nestjs/common';
import { LoggerService } from 'nestjs-cls-logger';

@Injectable()
export class AppService {
  constructor(private readonly logger: LoggerService) {}

  getHello(): string {
    this.logger.debug('start!');
    return 'Hello World!';
  }
}
  1. Start & refresh localhost:3000
npm run start:dev
  1. Console should have the following output:

console

Release Notes

1.1.3

  • Bugfix: correct output of Props

1.1.2

  • Disable colorization by default
  • Remove last newline of morgan message

1.1.0

  • Option to override console

1.0.0

  • Initial release.

Package Sidebar

Install

npm i nestjs-cls-logger

Weekly Downloads

35

Version

1.1.3

License

MIT

Unpacked Size

46.5 kB

Total Files

47

Last publish

Collaborators

  • t2tx