Skip to content

Commit

Permalink
Merge pull request #3350 from ulrichb/SingleWriteInLogger
Browse files Browse the repository at this point in the history
fix(common): single `stdout.write()` call per logging in default `Logger`
  • Loading branch information
kamilmysliwiec committed Nov 15, 2019
2 parents 22e0f56 + ca1ca5f commit 793ddc6
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/common/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,31 @@ export class Logger implements LoggerService {
undefined,
localeStringOptions,
);
process.stdout.write(color(`[Nest] ${process.pid} - `));
process.stdout.write(`${timestamp} `);

context && process.stdout.write(yellow(`[${context}] `));
process.stdout.write(output);
const pidMessage = color(`[Nest] ${process.pid} - `);
const contextMessage = context ? yellow(`[${context}] `) : '';
const timestampDiff = this.updateAndGetTimestampDiff(isTimeDiffEnabled);

this.printTimestamp(isTimeDiffEnabled);
process.stdout.write(`\n`);
process.stdout.write(
`${pidMessage}${timestamp} ${contextMessage}${output}${timestampDiff}\n`,
);
}

private static printTimestamp(isTimeDiffEnabled?: boolean) {
private static updateAndGetTimestampDiff(
isTimeDiffEnabled?: boolean,
): string {
const includeTimestamp = Logger.lastTimestamp && isTimeDiffEnabled;
if (includeTimestamp) {
process.stdout.write(yellow(` +${Date.now() - Logger.lastTimestamp}ms`));
}
const result = includeTimestamp
? yellow(` +${Date.now() - Logger.lastTimestamp}ms`)
: '';
Logger.lastTimestamp = Date.now();
return result;
}

private static printStackTrace(trace: string) {
if (!trace) {
return;
}
process.stdout.write(trace);
process.stdout.write(`\n`);
process.stdout.write(`${trace}\n`);
}
}

0 comments on commit 793ddc6

Please sign in to comment.