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

perf(common): In case of an expensive log, allow to pass a function #11281

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Injectable, Optional } from '../decorators/core';
import { clc, yellow } from '../utils/cli-colors.util';
import { isPlainObject, isString, isUndefined } from '../utils/shared.utils';
import {
isFunction,
isPlainObject,
isString,
isUndefined,
} from '../utils/shared.utils';
import { LoggerService, LogLevel } from './logger.service';
import { isLogLevelEnabled } from './utils';

Expand Down Expand Up @@ -221,7 +226,10 @@ export class ConsoleLogger implements LoggerService {
}

protected stringifyMessage(message: unknown, logLevel: LogLevel) {
return isPlainObject(message) || Array.isArray(message)
// If the message is a function, call it and re-resolve its value.
return isFunction(message)
? this.stringifyMessage(message(), logLevel)
: isPlainObject(message) || Array.isArray(message)
? `${this.colorize('Object:', logLevel)}\n${JSON.stringify(
message,
(key, value) =>
Expand Down
28 changes: 28 additions & 0 deletions packages/common/test/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,34 @@ describe('Logger', () => {
loggerWithContext.resetContext();
expect(loggerWithContext['context']).to.equal('context');
});

describe('functions for message', () => {
let processStdoutWriteSpy: sinon.SinonSpy;
const logger = new ConsoleLogger();
const message = 'Hello World';

beforeEach(() => {
processStdoutWriteSpy = sinon.spy(process.stdout, 'write');
});
afterEach(() => {
processStdoutWriteSpy.restore();
});

it('works', () => {
logger.log(() => message);

expect(processStdoutWriteSpy.calledOnce).to.be.true;
expect(processStdoutWriteSpy.firstCall.firstArg).to.include(message);
// Ensure we didn't serialize the function itself.
expect(processStdoutWriteSpy.firstCall.firstArg).not.to.include(' => ');
expect(processStdoutWriteSpy.firstCall.firstArg).not.to.include(
'function',
);
expect(processStdoutWriteSpy.firstCall.firstArg).not.to.include(
'Function',
);
});
});
});

describe('[instance methods]', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/utils/shared.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const normalizePath = (path?: string): string =>
export const stripEndSlash = (path: string) =>
path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path;

export const isFunction = (val: any): boolean => typeof val === 'function';
export const isFunction = (val: any): val is Function =>
typeof val === 'function';
export const isString = (val: any): val is string => typeof val === 'string';
export const isNumber = (val: any): val is number => typeof val === 'number';
export const isConstructor = (val: any): boolean => val === 'constructor';
Expand Down