diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 27af856835..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) 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), }, }, },