@origins-digital/logger
v1.2.0
Published
Origins Digital NestJS Logger with Winston
Readme
@origins-digital/logger
A comprehensive logging package for NestJS applications using Winston.
Installation
npm install @origins-digital/loggerFeatures
- Winston-based logging with JSON formatting
- Request logging middleware
- Configurable log levels
- Environment-specific formatting (local vs production)
- Automatic stack trace inclusion
- Request/response metadata logging
- Sensitive data filtering
Usage
Basic Setup
import { Module } from '@nestjs/common';
import { LoggerModule } from 'nestjs-winston';
import { LoggerConfig } from '@origins-digital/logger';
@Module({
imports: [LoggerModule.forRoot(LoggerConfig.instance().loggerOptions())],
})
export class AppModule {}Using the Logger
import { Controller, Get } from '@nestjs/common';
import { Logger } from '@nestjs/common';
import { LoggerMiddleware } from '@origins-digital/logger';
@Controller('example')
export class ExampleController {
private readonly logger = new Logger(ExampleController.name);
@Get()
getExample() {
this.logger.log('This is an info message');
this.logger.debug('This is a debug message');
this.logger.warn('This is a warning message');
this.logger.error(
'This is an error message',
new Error('Something went wrong'),
);
return { message: 'Example response' };
}
}Request Logging Middleware
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { LoggerMiddleware } from '@origins-digital/logger';
@Module({})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}Log Levels
The package supports the following log levels:
silly(5)debug(4)verbose(3)info(2) - Defaultwarn(1)error(0)
You can configure the log level using the LOG_LEVEL environment variable, either by name or number.
Log Format
Production Format
{
"timestamp": "2024-03-21 10:30:45:123",
"level": "info",
"message": "Example message",
"context": "ExampleController",
"meta": {
"key": "value"
}
}Local Development Format
2024-03-21 10:30:45:123 [ExampleController] info: Example message {"key":"value"}Request Logging
The middleware automatically logs:
- Request method and URL
- Request headers (excluding sensitive data)
- Request body (excluding sensitive data)
- Response status and timing
- Client information (IP, user agent, etc.)
- Error stack traces (if any)
Configuration
The logger can be configured through environment variables:
LOG_LEVEL: Set the logging level (default: 'info')APP_ENV: Set the environment ('local' for colored output, 'production' for JSON)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
