@designli/be-logger
v1.0.4
Published
Designli backend standard logger package for logging messages in a consistent format across the application
Downloads
23
Keywords
Readme
@designli/be-logger
Standard logger package for Designli backend applications. It provides a consistent logging interface with support for multiple adapters (Console, JSON, Sentry) and automatic context tracking using nestjs-cls.
It is designed to work seamlessly with NestJS but also supports a detached mode for standalone scripts or non-NestJS contexts.
Installation
npm install @designli/be-loggerUsage
NestJS Setup
- Register the Module: Import
DesignliLoggerModulein yourAppModule.
// interceptors.providers.ts
import { APP_INTERCEPTOR } from '@nestjs/core';
import { AuthContextInterceptor, AfterRequestInterceptor } from '@designli/be-logger';
export const interceptorsProviders = [
{
provide: APP_INTERCEPTOR,
useClass: AuthContextInterceptor,
},
{
provide: APP_INTERCEPTOR,
useClass: AfterRequestInterceptor,
},
];
// app.module.ts
import { Module } from '@nestjs/common';
import { DesignliLoggerModule, NestLoggerAdapter } from '@designli/be-logger';
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
imports: [
DesignliLoggerModule.forRoot({
// Configuration options (see Configuration section)
// available adapters: 'console', 'json', 'sentry'
adapters: process.env.APP_ENV === 'local' ? ['console'] : ['json', 'sentry'],
}),
],
providers: [...interceptorsProviders, NestLoggerAdapter],
})
export class AppModule {}- Use as Main Logger: Update your
main.tsto useNestLoggerAdapter.
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestLoggerAdapter } from '@designli/be-logger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Use the custom logger
app.useLogger(app.get(NestLoggerAdapter));
await app.listen(3000);
}
bootstrap();Usage in Services
Inject CompositeLogger to create a logger instance for your service.
import { Injectable } from '@nestjs/common';
import { DesignliLogger, CompositeLogger } from '@designli/be-logger';
@Injectable()
export class LoggerTestService {
private readonly logger: DesignliLogger;
constructor(private readonly loggerProvider: CompositeLogger) {
this.logger = this.loggerProvider.new(LoggerTestService);
}
test() {
this.logger.log('This is an info message', { meta: { test: 'test' } });
// This will also log an error message with stack trace
throw new Error('This is an error message');
}
}Detached Usage (Non-NestJS)
You can use the logger without the NestJS dependency injection system, which is useful for scripts or libraries.
import { CompositeLogger } from '@designli/be-logger';
// Create a detached logger instance
const loggerProvider = CompositeLogger.detached();
const logger = loggerProvider.new('MyScript');
logger.log('Starting script...');
logger.warn('This is a warning');Configuration
The DesignliLoggerModule.forRoot() method accepts a configuration object.
Example Configuration
DesignliLoggerModule.forRoot({
adapters: process.env.APP_ENV === 'local' ? ['sentry', 'console'] : ['json'],
adaptersOptions: {
console: {
colorize: true,
contextInfo: true,
},
json: {
prettyPrint: true,
},
},
})Environment Variables
The package uses the following environment variables:
| Variable | Description | Default |
| ---------------- | --------------------------------------------------------------------------------------------------------------- | --------------------- |
| LOG_TRACE_RATE | Probability (0-1) of logging a trace message when a request finalizes (used in AfterRequestInterceptor). | 0.1 (10%) |
| SENTRY_DSN | The Data Source Name (DSN) for Sentry integration. | undefined |
| LOG_LEVEL | Minimum log level enabled. Levels: trace < debug < info < warn < error < fatal. | trace (all enabled) |
| LOG_LEVELS | Comma-separated list of explicitly enabled log levels (e.g., error,fatal). Overrides/supplements LOG_LEVEL. | undefined |
