Skip to content

Commit

Permalink
Merge pull request #11571 from TP-O/fix-error-log
Browse files Browse the repository at this point in the history
fix(common): `ConsoleLogger` doesn't log stacktrace
  • Loading branch information
kamilmysliwiec authored May 16, 2023
2 parents 760a40e + c52d333 commit b2a35f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class ConsoleLogger implements LoggerService {
* Write an 'error' level log, if the configured level allows for it.
* Prints to `stderr` with newline.
*/
error(message: any, stackOrContext?: string): void;
error(message: any, stack?: string, context?: string): void;
error(message: any, ...optionalParams: [...any, string?, string?]): void;
error(message: any, ...optionalParams: any[]) {
Expand Down Expand Up @@ -281,6 +282,19 @@ export class ConsoleLogger implements LoggerService {
}

private getContextAndStackAndMessagesToPrint(args: unknown[]) {
if (args.length === 2) {
return this.isStackFormat(args[1])
? {
messages: [args[0]],
stack: args[1] as string,
context: this.context,
}
: {
messages: [args[0]],
context: args[1] as string,
};
}

const { messages, context } = this.getContextAndMessagesToPrint(args);
if (messages?.length <= 1) {
return { messages, context };
Expand All @@ -298,6 +312,14 @@ export class ConsoleLogger implements LoggerService {
};
}

private isStackFormat(stack: unknown) {
if (!isString(stack) && !isUndefined(stack)) {
return false;
}

return /^(.)+\n\s+at .+:\d+:\d+$/.test(stack);
}

private getColorByLogLevel(level: LogLevel) {
switch (level) {
case 'debug':
Expand Down
1 change: 1 addition & 0 deletions packages/common/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class Logger implements LoggerService {
/**
* Write an 'error' level log.
*/
static error(message: any, stackOrContext?: string): void;
static error(message: any, context?: string): void;
static error(message: any, stack?: string, context?: string): void;
static error(
Expand Down
32 changes: 30 additions & 2 deletions packages/common/test/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Logger', () => {
);
});

it('should print one error to the console', () => {
it('should print one error to the console with context', () => {
const message = 'random error';
const context = 'RandomContext';

Expand All @@ -83,6 +83,20 @@ describe('Logger', () => {
expect(processStderrWriteSpy.firstCall.firstArg).to.include(message);
});

it('should print one error to the console with stacktrace', () => {
const message = 'random error';
const stacktrace = 'Error: message\n at <anonymous>:1:2';

Logger.error(message, stacktrace);

expect(processStderrWriteSpy.calledTwice).to.be.true;
expect(processStderrWriteSpy.firstCall.firstArg).to.not.include(`[]`);
expect(processStderrWriteSpy.firstCall.firstArg).to.include(message);
expect(processStderrWriteSpy.secondCall.firstArg).to.equal(
stacktrace + '\n',
);
});

it('should print one error without context to the console', () => {
const message = 'random error without context';

Expand Down Expand Up @@ -354,7 +368,7 @@ describe('Logger', () => {
);
});

it('should print one error to the console', () => {
it('should print one error to the console with context', () => {
const message = 'random error';
const context = 'RandomContext';

Expand All @@ -367,6 +381,20 @@ describe('Logger', () => {
expect(processStderrWriteSpy.firstCall.firstArg).to.include(message);
});

it('should print one error to the console with stacktrace', () => {
const message = 'random error';
const stacktrace = 'Error: message\n at <anonymous>:1:2';

logger.error(message, stacktrace);

expect(processStderrWriteSpy.calledTwice).to.be.true;
expect(processStderrWriteSpy.firstCall.firstArg).to.not.include(`[]`);
expect(processStderrWriteSpy.firstCall.firstArg).to.include(message);
expect(processStderrWriteSpy.secondCall.firstArg).to.equal(
stacktrace + '\n',
);
});

it('should print one error without context to the console', () => {
const message = 'random error without context';

Expand Down

0 comments on commit b2a35f2

Please sign in to comment.