From 85b24478f349fbc17e09d0be00c845393b0dacbb Mon Sep 17 00:00:00 2001 From: Connor Kirkpatrick Date: Wed, 23 Apr 2025 16:55:52 +0100 Subject: [PATCH 1/2] fix(logger): Format the stack trace as an array of strings in dev mode When POWERTOOLS_DEV is enabled, the stack trace will be split on newline characters to create an array. This improves readability compared to a single long string --- packages/logger/src/formatter/LogFormatter.ts | 7 ++++++ .../tests/unit/initializeLogger.test.ts | 23 +++++++++++++++++++ .../logger/tests/unit/workingWithkeys.test.ts | 6 ++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 27af856835..7bea010289 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -131,6 +131,13 @@ abstract class LogFormatter { } } + if ( + this.envVarsService?.isDevMode() && + typeof formattedError.stack === 'string' + ) { + formattedError.stack = formattedError.stack.split('\n'); + } + return formattedError; } diff --git a/packages/logger/tests/unit/initializeLogger.test.ts b/packages/logger/tests/unit/initializeLogger.test.ts index 689c954493..e133ae71f0 100644 --- a/packages/logger/tests/unit/initializeLogger.test.ts +++ b/packages/logger/tests/unit/initializeLogger.test.ts @@ -163,4 +163,27 @@ describe('Log levels', () => { vi.useRealTimers(); }); + + it('splits the stack trace into an array when POWERTOOLS_DEV is set', () => { + // Prepare + const logger = new Logger(); + const err = new Error('Hello, world!'); + + // Act + logger.error('Error occured', err); + + // Assess + expect(console.error).toHaveBeenCalledTimes(1); + expect(console.error).toHaveLoggedNth( + 1, + expect.objectContaining({ + error: { + location: expect.any(String), + message: 'Hello, world!', + name: 'Error', + stack: expect.any(Array), + }, + }) + ); + }); }); diff --git a/packages/logger/tests/unit/workingWithkeys.test.ts b/packages/logger/tests/unit/workingWithkeys.test.ts index 94c388fc55..e4d4bbc1ec 100644 --- a/packages/logger/tests/unit/workingWithkeys.test.ts +++ b/packages/logger/tests/unit/workingWithkeys.test.ts @@ -56,7 +56,7 @@ describe('Working with keys', () => { location: expect.any(String), message: 'Something happened!', name: 'Error', - stack: expect.any(String), + stack: expect.any(Array), }, }, info: 'adds the message and error', @@ -72,7 +72,7 @@ describe('Working with keys', () => { location: expect.any(String), message: 'Something happened!', name: 'Error', - stack: expect.any(String), + stack: expect.any(Array), }, }, info: 'adds the message and custom error', @@ -104,7 +104,7 @@ describe('Working with keys', () => { location: expect.any(String), message: 'Arbitrary object error', name: 'Error', - stack: expect.any(String), + stack: expect.any(Array), }, }, }, From dc238e1b65f1b564c3404ad424564ef7ab13166b Mon Sep 17 00:00:00 2001 From: Connor Kirkpatrick Date: Wed, 23 Apr 2025 17:35:15 +0100 Subject: [PATCH 2/2] Incorporate feedback --- packages/logger/src/formatter/LogFormatter.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 7bea010289..9b9eea671a 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -116,7 +116,10 @@ abstract class LogFormatter { name, location: this.getCodeLocation(error.stack), message, - stack, + stack: + this.envVarsService?.isDevMode() && typeof stack === 'string' + ? stack?.split('\n') + : stack, cause: error.cause instanceof Error ? this.formatError(error.cause) @@ -131,13 +134,6 @@ abstract class LogFormatter { } } - if ( - this.envVarsService?.isDevMode() && - typeof formattedError.stack === 'string' - ) { - formattedError.stack = formattedError.stack.split('\n'); - } - return formattedError; }