class StructuredLogger
extends Logger

Structured logger implementation for production observability Outputs JSON-formatted logs compatible with log aggregation systems (CloudWatch, Datadog, Splunk, etc.)

Examples

Example 1

const logger = new StructuredLogger({
    level: LogLevel.Info,
    correlationId: 'abc-123',
    traceId: 'trace-456'
});
logger.info('Processing started', { itemCount: 42 });
// Output: {"timestamp":"2024-01-01T12:00:00.000Z","level":"info","message":"Processing started","context":{"itemCount":42},"correlationId":"abc-123","traceId":"trace-456"}

Constructors

new
StructuredLogger(options?: LoggerOptions)

Properties

private
optional
correlationId: string
private
optional
traceId: string

Methods

child(prefix: string): StructuredLogger

Creates a child logger with an additional prefix

private
createLogEntry(
level: LogLevel,
message: string,
context?: Record<string, unknown>,
): string

Creates a structured log entry

debug(
message: string,
context?: Record<string, unknown>,
): void

Logs a debug message with optional context

error(
message: string,
context?: Record<string, unknown>,
): void

Logs an error message with optional context

info(
message: string,
context?: Record<string, unknown>,
): void

Logs an info message with optional context

setCorrelationId(correlationId: string): void

Sets the correlation ID for grouping related logs

setTraceId(traceId: string): void

Sets the trace ID for distributed tracing

success(
message: string,
context?: Record<string, unknown>,
): void

Logs a success message (info level) with optional context

trace(
message: string,
context?: Record<string, unknown>,
): void

Logs a trace message with optional context

warn(
message: string,
context?: Record<string, unknown>,
): void

Logs a warning message with optional context