Skip to content

Commit

Permalink
fix(common): account for when stack is undefined in logger
Browse files Browse the repository at this point in the history
Due to a change in nestjs#10531 when the `stack` is added as `undefined` a
new `undefined` log would be added whenever `this.logger.error()`
would be called. Now, we check that the last element is either
a string or undefined, and if so strip the last element. That element
should only exist when we artificially add the stack via the changes
from nestjs#10531.

closes nestjs#11074
  • Loading branch information
jmcdo29 committed Feb 8, 2023
1 parent 4dcb8d5 commit 0fbceee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Optional } from '../decorators/core';
import { clc, yellow } from '../utils/cli-colors.util';
import { isPlainObject, isString } from '../utils/shared.utils';
import { isPlainObject, isString, isUndefined } from '../utils/shared.utils';
import { LoggerService, LogLevel } from './logger.service';
import { isLogLevelEnabled } from './utils';

Expand Down Expand Up @@ -279,7 +279,7 @@ export class ConsoleLogger implements LoggerService {
}
const lastElement = messages[messages.length - 1];
const isStack = isString(lastElement);
if (!isStack) {
if (!isStack && !isUndefined(lastElement)) {
return { messages, context };
}
return {
Expand Down
11 changes: 11 additions & 0 deletions packages/common/test/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ describe('Logger', () => {
);
expect(processStdoutWriteSpy.thirdCall.firstArg).to.include('ms');
});
it('should log out an error to stderr but not include an undefined log', () => {
const message = 'message 1';

logger.error(message);

expect(processStderrWriteSpy.calledOnce).to.be.true;
expect(processStderrWriteSpy.firstCall.firstArg).to.include(
`[${globalContext}]`,
);
expect(processStderrWriteSpy.firstCall.firstArg).to.include(message);
});
});

describe('when logging is disabled', () => {
Expand Down

0 comments on commit 0fbceee

Please sign in to comment.