reusable-winston-logger
v1.0.2
Published
A reusable NestJS Winston logger package providing structured logging, request interceptors, and automatic log rotation for production applications.
Downloads
31
Maintainers
Readme
Reusable Winston Logger
A professional, opinionated Winston logger module for NestJS, featuring daily file rotation and environment-aware configuration.
Features
- Daily File Rotation: Automatically manages log files with rotation (combined and error logs).
- Environment Aware: Configurable via
.envvariables or standard NestJS module options. - Bootstrapping Support: Easily use the logger as the main NestJS application logger from the start.
- Async Configuration: Supports
forRootAsyncfor integration withConfigService. - Pre-configured Formats: Professional "Nest-like" console output for development and JSON for production.
Installation
npm install reusable-winston-loggerQuick Start (Bootstrap)
To use this logger as the primary NestJS logger for bootstrapping:
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggerModule } from 'reusable-winston-logger';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
// This ensures even the internal NestJS logs use your custom Winston logger
logger: LoggerModule.createLogger({
serviceName: 'my-app-name',
}),
});
await app.listen(3000);
}
bootstrap();Configuration
Synchronous Usage
// app.module.ts
import { Module } from '@nestjs/common';
import { LoggerModule } from 'reusable-winston-logger';
@Module({
imports: [
LoggerModule.forRoot({
level: 'debug',
serviceName: 'my-app',
logDir: 'logs',
maxFiles: '14d',
maxSize: '20m',
}),
],
})
export class AppModule {}Asynchronous Usage (Recommended)
// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoggerModule } from 'reusable-winston-logger';
@Module({
imports: [
ConfigModule.forRoot(),
LoggerModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
level: configService.get('LOG_LEVEL'),
serviceName: 'my-app',
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}Options
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| level | string | info (prod) / debug (dev) | Minimum logging level |
| serviceName | string | nestjs-app | Name of the service for meta data |
| logDir | string | logs | Directory where log files will be stored |
| maxFiles | string | 14d | Retention period for log files |
| maxSize | string | 20m | Maximum size of a single log file |
| winstonOptions| winston.LoggerOptions | undefined | Custom winston options to merge |
| instance | winston.Logger | undefined | Pre-created winston instance |
Environment Variables
The package automatically looks for these variables if options are not provided:
LOG_LEVELLOG_MAX_FILESLOG_MAX_SIZESERVICE_NAMELOG_DIRNODE_ENV(used to toggle console colors and level defaults)
Usage in Services
import { Injectable } from '@nestjs/common';
import { AppLoggerService } from 'reusable-winston-logger';
@Injectable()
export class MyService {
constructor(private readonly logger: AppLoggerService) {}
doSomething() {
this.logger.log('Doing something...', 'MyService');
this.logger.error('Something went wrong!', 'Stacktrace', 'MyService');
this.logger.debug('Debugging info...', 'MyService');
}
}