style(logging): clean up log format with ISO8601 timestamps and no PID prefix
- override getTimestamp() to emit ISO 8601 instead of locale string - override formatMessage() to drop PID prefix and level padding - enable timestamp: true so timestampDiff (+Nms) appears on each line - pass TraceLogger instance to NestFactory so framework logs use same format
This commit is contained in:
1 parent
3367828c39
commit
8e62ad8b4e
2 files changed
+23
-2
No files matched your search
@@ -1,7 +1,28 @@
|
||||
import { ConsoleLogger } from '@nestjs/common';
|
||||
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from '@nestjs/common';
|
||||
import { getTraceId } from './trace-context';
|
||||
|
||||
export class TraceLogger extends ConsoleLogger {
|
||||
constructor(context?: string, options: ConsoleLoggerOptions = {}) {
|
||||
super(context as string, options);
|
||||
}
|
||||
|
||||
protected override getTimestamp(): string {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
protected override formatMessage(
|
||||
logLevel: LogLevel,
|
||||
message: unknown,
|
||||
_pidMessage: string,
|
||||
_formattedLogLevel: string,
|
||||
contextMessage: string,
|
||||
timestampDiff: string,
|
||||
): string {
|
||||
const output = this.stringifyMessage(message, logLevel);
|
||||
const level = this.colorize(logLevel.toUpperCase(), logLevel);
|
||||
return `${this.getTimestamp()} ${level} ${contextMessage}${output}${timestampDiff}\n`;
|
||||
}
|
||||
|
||||
protected override formatContext(context: string): string {
|
||||
const traceId = getTraceId();
|
||||
const traced = traceId ? `${context}:${traceId}` : context;
|
||||
|
||||
Reference in new issue
Block a user