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,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;
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import { name as pkgName, version as pkgVersion } from '../package.json';
|
||||
|
||||
async function bootstrap() {
|
||||
const logger = new TraceLogger('Bootstrap');
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const app = await NestFactory.create(AppModule, { logger: new TraceLogger('Bootstrap', { timestamp: true }) });
|
||||
|
||||
app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
|
||||
Reference in New Issue
Block a user