| 40 | }; |
| 41 | |
| 42 | export class LoggerContextImpl implements LoggerContext { |
| 43 | private config: LoggerContextConfig; |
| 44 | |
| 45 | /** |
| 46 | * Create a logger context. Context and metadata are defensively copied so |
| 47 | * mutations after construction do not affect emitted logs. |
| 48 | */ |
| 49 | constructor(config: LoggerContextConfig) { |
| 50 | const correlationContext = config.correlationContext ? { ...config.correlationContext } : undefined; |
| 51 | |
| 52 | this.config = { |
| 53 | ...config, |
| 54 | traceId: config.traceId ?? correlationContext?.traceId, |
| 55 | spanId: config.spanId ?? correlationContext?.spanId, |
| 56 | correlationContext, |
| 57 | metadata: config.metadata ? structuredClone(config.metadata) : undefined, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** Log at DEBUG level. */ |
| 62 | debug(message: string, data?: Record<string, unknown>): void { |
| 63 | this.log('debug', message, data); |
| 64 | } |
| 65 | |
| 66 | /** Log at INFO level. */ |
| 67 | info(message: string, data?: Record<string, unknown>): void { |
| 68 | this.log('info', message, data); |
| 69 | } |
| 70 | |
| 71 | /** Log at WARN level. */ |
| 72 | warn(message: string, data?: Record<string, unknown>): void { |
| 73 | this.log('warn', message, data); |
| 74 | } |
| 75 | |
| 76 | /** Log at ERROR level. */ |
| 77 | error(message: string, data?: Record<string, unknown>): void { |
| 78 | this.log('error', message, data); |
| 79 | } |
| 80 | |
| 81 | /** Log at FATAL level. */ |
| 82 | fatal(message: string, data?: Record<string, unknown>): void { |
| 83 | this.log('fatal', message, data); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Build an ExportedLog, check against the minimum level, and emit it through the bus. |
| 88 | */ |
| 89 | private log(level: LogLevel, message: string, data?: Record<string, unknown>): void { |
| 90 | const minLevel = this.config.minLevel ?? 'warn'; |
| 91 | if (LOG_LEVEL_PRIORITY[level] < LOG_LEVEL_PRIORITY[minLevel]) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | const exportedLog: ExportedLog = { |
| 96 | logId: generateSignalId(), |
| 97 | timestamp: new Date(), |
| 98 | level, |
| 99 | message, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…