Skip to main content

Logging

Zelt provides a built-in Logger module with configurable log levels.

Basic Usage

Inject the Logger into your services or controllers:

import { Injectable, inject } from '@zeltjs/core';
import { Logger } from '@zeltjs/core/modules/logger';

@Injectable()
export class OrderService {
constructor(private logger = inject(Logger)) {}

processOrder(orderId: string) {
this.logger.info(`Processing order: ${orderId}`);

try {
// ... process order
this.logger.debug('Order validation passed');
} catch (error) {
this.logger.error(`Failed to process order: ${orderId}`);
throw error;
}
}
}

Log Levels

The Logger supports four log levels in order of severity:

LevelMethodDescription
debuglogger.debug()Detailed debugging information
infologger.info()General informational messages
warnlogger.warn()Warning messages
errorlogger.error()Error messages

Messages are only output if their level is equal to or higher than the configured level. For example, with level: 'info', debug() messages are suppressed.

Configuration

Configure the Logger using LoggerConfig:

import { Config } from '@zeltjs/core';
import { LoggerConfig } from '@zeltjs/core/modules/logger';

@Config
export class AppLoggerConfig extends LoggerConfig {
override get level(): 'debug' | 'info' | 'warn' | 'error' {
return process.env.LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error' ?? 'info';
}
}

Register the config when creating the app:

import { createHttpApp } from '@zeltjs/core';
import { AppLoggerConfig } from './logger.config';
import { AppController } from './app.controller';

const app = createHttpApp({
controllers: [AppController],
configs: [AppLoggerConfig],
});

Default Behavior

Without custom configuration, the Logger uses 'info' as the default level, meaning debug() messages are suppressed while info(), warn(), and error() messages are output.