@leroymerlin/nestjs-logger
v2.0.0
Published
Logger for NestJS based on pino
Downloads
30
Readme
@leroymerlin/nestjs-logger
Nest.js module wrapper for pino logger.
Installation
npm install --save @leroymerlin/nestjs-logger @leroymerlin/nestjs-async-hooks
Quick Start
Import LoggerModule into the root AppModule and use the LoggerModule#forRoot() method to configure it.
// app.module.ts
import { LoggerModule } from '@leroymerlin/nestjs-logger';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [LoggerModule.forRoot({ name: 'app-name' })],
controllers: [AppController],
providers: [AppService],
})
class AppModule {}
Use logger as the main Nest logger
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { PinoLoggerService } from '@leroymerlin/nestjs-logger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useLogger(app.get(PinoLoggerService));
await app.listen(3000);
}
bootstrap();
Use logger in app as default logger
// app.service.ts
import { Injectable, Logger } from '@nestjs/common';
@Injectable()
export class AppService {
private readonly logger = new Logger(AppService.name);
constructor(private readonly appRepository: AppRepository) {}
async read(id: string) {
try {
this.logger.debug('read', id);
const message = await this.appRepository.readById(id);
return message;
} catch (error) {
this.logger.error(error, 'read failed', id);
return 'Error';
}
}
}
License
@leroymerlin/nestjs-logger is MIT.