Winston custom logger integration module for Nest.js framework.
Installation
Yarn
yarn add @mobizerg/nest-logger winston
NPM
npm install @mobizerg/nest-logger winston --save
Description
Custom logger module for Nest.js based on Winston package.
Usage
Import the LoggerModule in app.module.ts
import { Module } from '@nestjs/common';
import { LoggerModule } from '@mobizerg/nest-logger';
@Module({
imports: [
LoggerModule.register(options),
],
})
export class AppModule {}
With Async
import { Module } from '@nestjs/common';
import { LoggerModule } from '@mobizerg/nest-logger';
@Module({
imports: [
LoggerModule.registerAsync({
imports: [ConfigModule],
useExisting: LoggerConfigService,
}),
],
})
export class AppModule {}
Example config file (async)
import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { LoggerModuleOptions, LoggerOptionsFactory } from '@mobizerg/nest-logger';
import { transports, format } from 'winston';
@Injectable()
export class LoggerConfigService implements LoggerOptionsFactory {
constructor(private readonly config: ConfigService) {}
createLoggerOptions(name?: string): LoggerModuleOptions {
return {
name,
// Winston logger options
loggerOptions: {
level: 'error',
format: format.cli(),
transports: [new transports.Console()]
},
};
}
}
Add LoggerModule in main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggerService } from '@mobizerg/nest-logger';
async function bootstrap() {
const server = express();
const app = await NestFactory.create(AppModule, { logger: false });
const logger = app.get(LoggerService);
app.useLogger(logger);
}
License
MIT