forked from Shared/esg
initial commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { ConsoleLogger, LogLevel } from '@nestjs/common';
|
||||
import { logs, SeverityNumber } from '@opentelemetry/api-logs';
|
||||
|
||||
const nestLogger = logs.getLogger('nestjs', '1.0.0');
|
||||
|
||||
function stringifyLogValue(value: unknown): string {
|
||||
if (typeof value === 'string') return value;
|
||||
if (value instanceof Error) return value.stack ?? value.message;
|
||||
|
||||
try {
|
||||
return typeof value === 'object' ? JSON.stringify(value) : String(value);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
function getContext(optionalParams: unknown[]): string | undefined {
|
||||
const lastParam = optionalParams[optionalParams.length - 1];
|
||||
return typeof lastParam === 'string' ? lastParam : undefined;
|
||||
}
|
||||
|
||||
function emitNestLog(
|
||||
severityNumber: SeverityNumber,
|
||||
severityText: string,
|
||||
message: unknown,
|
||||
optionalParams: unknown[],
|
||||
): void {
|
||||
const context = getContext(optionalParams);
|
||||
const attributes: Record<string, string> = {
|
||||
'log.source': 'nestjs',
|
||||
};
|
||||
|
||||
if (context) {
|
||||
attributes['nestjs.context'] = context;
|
||||
}
|
||||
|
||||
const stack = optionalParams.find(
|
||||
(param) => typeof param === 'string' && param.includes('\n'),
|
||||
);
|
||||
|
||||
if (typeof stack === 'string') {
|
||||
attributes['exception.stacktrace'] = stack;
|
||||
}
|
||||
|
||||
nestLogger.emit({
|
||||
severityNumber,
|
||||
severityText,
|
||||
body: stringifyLogValue(message),
|
||||
attributes,
|
||||
});
|
||||
}
|
||||
|
||||
export class OpenTelemetryNestLogger extends ConsoleLogger {
|
||||
log(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.INFO, 'INFO', message, optionalParams);
|
||||
super.log(message, ...optionalParams);
|
||||
}
|
||||
|
||||
error(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.ERROR, 'ERROR', message, optionalParams);
|
||||
super.error(message, ...optionalParams);
|
||||
}
|
||||
|
||||
warn(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.WARN, 'WARN', message, optionalParams);
|
||||
super.warn(message, ...optionalParams);
|
||||
}
|
||||
|
||||
debug(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.DEBUG, 'DEBUG', message, optionalParams);
|
||||
super.debug(message, ...optionalParams);
|
||||
}
|
||||
|
||||
verbose(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.TRACE, 'VERBOSE', message, optionalParams);
|
||||
super.verbose(message, ...optionalParams);
|
||||
}
|
||||
|
||||
fatal(message: unknown, ...optionalParams: unknown[]): void {
|
||||
emitNestLog(SeverityNumber.FATAL, 'FATAL', message, optionalParams);
|
||||
super.fatal(message, ...optionalParams);
|
||||
}
|
||||
|
||||
setLogLevels(levels: LogLevel[]): void {
|
||||
super.setLogLevels(levels);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user