@edirect/logger
v11.0.50
Published
Structured logging module for eDirect NestJS applications. Built on top of [Pino](https://github.com/pinojs/pino), providing high-performance JSON logging to stdout/stderr, configurable log levels, and correlation ID tracking (sessionId / quoteId).
Maintainers
Keywords
Readme
@edirect/logger
Structured logging module for eDirect NestJS applications. Built on top of Pino, providing high-performance JSON logging to stdout/stderr, configurable log levels, and correlation ID tracking (sessionId / quoteId).
Features
- Structured JSON logs with consistent field names (
message,level,timestamp,name,version) - Outputs to stdout/stderr
- Configurable log level via environment variable or module options
- Tracks
sessionIdandquoteIdas correlation context in all log entries - Global module — register once, inject
LoggerServiceanywhere - Supports both sync (
register) and async (registerAsync) configuration
Installation
pnpm add @edirect/logger
# or
npm install @edirect/loggerUsage
Simple registration (static options)
import { Module } from '@nestjs/common';
import { LoggerModule } from '@edirect/logger';
@Module({
imports: [
LoggerModule.register({
output: 'console',
level: 'info',
name: 'my-service',
}),
],
})
export class AppModule {}Async registration (with ConfigService)
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@edirect/config';
import { LoggerModule } from '@edirect/logger';
@Module({
imports: [
ConfigModule,
LoggerModule.registerAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
level: (configService.get('LOGS_LEVEL') as any) ?? 'info',
name: configService.get('APP_NAME') ?? 'app',
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}Inject and use LoggerService
import { Injectable } from '@nestjs/common';
import { LoggerService } from '@edirect/logger';
@Injectable()
export class OrderService {
constructor(private readonly logger: LoggerService) {}
async processOrder(orderId: string, sessionId: string) {
this.logger.setSessionId(sessionId);
this.logger.setQuoteId(orderId);
this.logger.info('Processing order', JSON.stringify({ orderId }));
try {
// business logic...
this.logger.log('Order processed successfully');
} catch (err) {
this.logger.error('Order processing failed', (err as Error).stack ?? '');
}
}
}API
LoggerService methods
| Method | Signature | Description |
| -------------- | ------------------------------------------- | ----------------------------------------------------------- |
| log | (message: string, payload?: string): void | Alias for info |
| info | (message: string, payload?: string): void | Log at INFO level |
| warn | (message: string, payload?: string): void | Log at WARN level |
| error | (message: string, trace: string): void | Log at ERROR level |
| debug | (message: string): void | Log at DEBUG level |
| verbose | (message: string): void | Log at VERBOSE level (maps to INFO internally) |
| setSessionId | (sessionId: string): void | Attach a session ID to all subsequent log entries |
| setQuoteId | (quoteId: string): void | Attach a quote/correlation ID to all subsequent log entries |
LoggerModuleOptions
| Option | Type | Default | Description |
| ---------- | ------------ | -------------------------------- | ---------------------------------- |
| level | pino.Level | 'info' | Minimum log level |
| name | string | npm_package_name \| 'app' | Service name in every log entry |
| version | string | npm_package_version \| '0.0.0' | Service version in every log entry |
| host | string | '0.0.0.0' | Host tag |
| protocol | string | 'http' | Protocol tag |
Environment Variables
When options are not passed explicitly, the logger reads from environment variables:
| Variable | Description | Default |
| --------------- | -------------------------------------------------------------- | -------------------------------- |
| LOGS_LEVEL | Log level (trace, debug, info, warn, error, fatal) | info |
| LOGS_HOST | Host tag in log entries | 0.0.0.0 |
| LOGS_PROTOCOL | Protocol tag in log entries | http |
| APP_NAME | Service name | npm_package_name or app |
| APP_VERSION | Service version | npm_package_version or 0.0.0 |
Log Output Format
Every log entry is a JSON object:
{
"level": "info",
"timestamp": "2024-01-15T10:30:00.000Z",
"name": "payment-gateway",
"version": "1.2.3",
"host": "pod-abc-123",
"protocol": "http",
"sessionId": "session-abc-123",
"quoteId": "quote-xyz-456",
"remote-address": "",
"message": "Order created successfully",
"payload": { "orderId": "ORD-789" }
}