update
This commit is contained in:
86
ts/logger.ts
86
ts/logger.ts
@ -1,9 +1,91 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
export const logger = new plugins.smartlog.Smartlog({
|
||||
// Map NODE_ENV to valid TEnvironment
|
||||
const nodeEnv = process.env.NODE_ENV || 'production';
|
||||
const envMap: Record<string, 'local' | 'test' | 'staging' | 'production'> = {
|
||||
'development': 'local',
|
||||
'test': 'test',
|
||||
'staging': 'staging',
|
||||
'production': 'production'
|
||||
};
|
||||
|
||||
// Default Smartlog instance
|
||||
const baseLogger = new plugins.smartlog.Smartlog({
|
||||
logContext: {
|
||||
environment: 'production',
|
||||
environment: envMap[nodeEnv] || 'production',
|
||||
runtime: 'node',
|
||||
zone: 'serve.zone',
|
||||
}
|
||||
});
|
||||
|
||||
// Extended logger compatible with the original enhanced logger API
|
||||
class StandardLogger {
|
||||
private defaultContext: Record<string, any> = {};
|
||||
private correlationId: string | null = null;
|
||||
|
||||
constructor() {}
|
||||
|
||||
// Log methods
|
||||
public log(level: 'error' | 'warn' | 'info' | 'success' | 'debug', message: string, context: Record<string, any> = {}) {
|
||||
const combinedContext = {
|
||||
...this.defaultContext,
|
||||
...context
|
||||
};
|
||||
|
||||
if (this.correlationId) {
|
||||
combinedContext.correlation_id = this.correlationId;
|
||||
}
|
||||
|
||||
baseLogger.log(level, message, combinedContext);
|
||||
}
|
||||
|
||||
public error(message: string, context: Record<string, any> = {}) {
|
||||
this.log('error', message, context);
|
||||
}
|
||||
|
||||
public warn(message: string, context: Record<string, any> = {}) {
|
||||
this.log('warn', message, context);
|
||||
}
|
||||
|
||||
public info(message: string, context: Record<string, any> = {}) {
|
||||
this.log('info', message, context);
|
||||
}
|
||||
|
||||
public success(message: string, context: Record<string, any> = {}) {
|
||||
this.log('success', message, context);
|
||||
}
|
||||
|
||||
public debug(message: string, context: Record<string, any> = {}) {
|
||||
this.log('debug', message, context);
|
||||
}
|
||||
|
||||
// Context management
|
||||
public setContext(context: Record<string, any>, overwrite: boolean = false) {
|
||||
if (overwrite) {
|
||||
this.defaultContext = context;
|
||||
} else {
|
||||
this.defaultContext = {
|
||||
...this.defaultContext,
|
||||
...context
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Correlation ID management
|
||||
public setCorrelationId(id: string | null = null): string {
|
||||
this.correlationId = id || randomUUID();
|
||||
return this.correlationId;
|
||||
}
|
||||
|
||||
public getCorrelationId(): string | null {
|
||||
return this.correlationId;
|
||||
}
|
||||
|
||||
public clearCorrelationId(): void {
|
||||
this.correlationId = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Export a singleton instance
|
||||
export const logger = new StandardLogger();
|
||||
|
Reference in New Issue
Block a user