Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): Single stdout.write() call per logging in default Logger #3350

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`);
}
}