logger-nestjs
v0.0.6
Published
A powerful and flexible logger module for NestJS with support for multiple transports, CLS integration, and daily log rotation
Maintainers
Readme
Logger NestJS
A powerful and flexible logger module for NestJS with support for multiple transports, CLS integration, and daily log rotation.
Features
- Multiple Transports: Console and file transports
- Daily Log Rotation: Automatic daily log file creation
- CLS Integration: Request ID tracking via continuation-local storage
- Configurable Log Levels: Fine-grained control over log verbosity
- TypeScript Support: Fully typed for better developer experience
- Flexible Configuration: Easy to configure and extend
Installation
npm install logger-nestjs
# or
yarn add logger-nestjs
# or
pnpm add logger-nestjsQuick Start
1. Register the Module
import { Module } from '@nestjs/common'
import { LoggerModule, LogLevel } from 'logger-nestjs'
@Module({
imports: [
LoggerModule.register({
level: LogLevel.LOG,
transports: [
{
type: 'console',
enabled: true,
options: {
colorize: true,
},
},
{
type: 'file',
enabled: true,
options: {
dirname: 'logs',
},
},
],
}),
],
})
export class AppModule {}2. Use the Logger
import { Injectable } from '@nestjs/common'
import { LoggerService } from 'logger-nestjs'
@Injectable()
export class AppService {
constructor(private readonly logger: LoggerService) {
this.logger.setContext('AppService')
}
getHello(): string {
this.logger.log('Hello World')
this.logger.error('Something went wrong')
this.logger.warn('Warning message')
this.logger.debug('Debug message')
return 'Hello World'
}
}Configuration
LoggerConfig
interface LoggerConfig {
level: LogLevel
transports: TransportConfig[]
clsRequestIdKey?: string
}Log Levels
enum LogLevel {
FATAL = 0,
ERROR = 1,
WARN = 2,
LOG = 3,
VERBOSE = 4,
}Transport Types
Console Transport
{
type: 'console',
enabled: true,
options: {
colorize: boolean
}
}File Transport
{
type: 'file',
enabled: true,
options: {
dirname: string // Directory to store log files
}
}File transport automatically creates daily log files in the format: app-YYYY-MM-DD.log
CLS Integration
The logger integrates with nestjs-cls to track request IDs across your application.
Setup CLS
import { Module } from '@nestjs/common'
import { ClsModule } from 'nestjs-cls'
import { LoggerModule, LogLevel } from 'logger-nestjs'
@Module({
imports: [
ClsModule.forRoot({
global: true,
middleware: { mount: true },
}),
LoggerModule.register({
level: LogLevel.LOG,
transports: [
{
type: 'console',
enabled: true,
},
],
clsRequestIdKey: 'Request-Id-In-App', // Optional, defaults to 'Request-Id-In-App'
}),
],
})
export class AppModule {}Set Request ID
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'
import { Observable } from 'rxjs'
import { ClsService } from 'nestjs-cls'
@Injectable()
export class RequestIdInterceptor implements NestInterceptor {
constructor(private readonly cls: ClsService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest()
const requestId = request.headers['x-request-id'] || generateUniqueId()
this.cls.set('Request-Id-In-App', requestId)
return next.handle()
}
}Decorators
LoggerContext
Use the @LoggerContext() decorator to set the logger context:
import { Injectable } from '@nestjs/common'
import { LoggerService, LoggerContext } from 'logger-nestjs'
@Injectable()
@LoggerContext('MyService')
export class MyService {
constructor(private readonly logger: LoggerService) {}
doSomething() {
this.logger.log('This will have context "MyService"')
}
}License
MIT
