Skip to content

Commit

Permalink
Merge pull request #11281 from hansl/console-fn
Browse files Browse the repository at this point in the history
perf(common): In case of an expensive log, allow to pass a function
  • Loading branch information
kamilmysliwiec committed Mar 22, 2023
2 parents 57a1858 + 618bcc4 commit 92d5415
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
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

0 comments on commit 92d5415

Please sign in to comment.