nestjs-cls-logger
v1.1.3
Published
nestjs cls logger npm
Downloads
701
Readme
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
- Create a sample nestjs app
# install nest cli
npm i -g @nestjs/cli
# create sample app
nest new sample-app
cd sample-app
- Install this npm
npm i nestjs-cls-logger
- 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 {}
- 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();
- 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!';
}
}
- Start & refresh localhost:3000
npm run start:dev
- Console should have the following output:
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.