Skip to content

Commit 98f5c45

Browse files
improv(logger): Format the stack trace as an array of strings in dev mode (#3852)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent a722e70 commit 98f5c45

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/logger/src/formatter/LogFormatter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ abstract class LogFormatter {
116116
name,
117117
location: this.getCodeLocation(error.stack),
118118
message,
119-
stack,
119+
stack:
120+
this.envVarsService?.isDevMode() && typeof stack === 'string'
121+
? stack?.split('\n')
122+
: stack,
120123
cause:
121124
error.cause instanceof Error
122125
? this.formatError(error.cause)

packages/logger/tests/unit/initializeLogger.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,27 @@ describe('Log levels', () => {
163163

164164
vi.useRealTimers();
165165
});
166+
167+
it('splits the stack trace into an array when POWERTOOLS_DEV is set', () => {
168+
// Prepare
169+
const logger = new Logger();
170+
const err = new Error('Hello, world!');
171+
172+
// Act
173+
logger.error('Error occured', err);
174+
175+
// Assess
176+
expect(console.error).toHaveBeenCalledTimes(1);
177+
expect(console.error).toHaveLoggedNth(
178+
1,
179+
expect.objectContaining({
180+
error: {
181+
location: expect.any(String),
182+
message: 'Hello, world!',
183+
name: 'Error',
184+
stack: expect.any(Array),
185+
},
186+
})
187+
);
188+
});
166189
});

packages/logger/tests/unit/workingWithkeys.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Working with keys', () => {
5656
location: expect.any(String),
5757
message: 'Something happened!',
5858
name: 'Error',
59-
stack: expect.any(String),
59+
stack: expect.any(Array),
6060
},
6161
},
6262
info: 'adds the message and error',
@@ -72,7 +72,7 @@ describe('Working with keys', () => {
7272
location: expect.any(String),
7373
message: 'Something happened!',
7474
name: 'Error',
75-
stack: expect.any(String),
75+
stack: expect.any(Array),
7676
},
7777
},
7878
info: 'adds the message and custom error',
@@ -104,7 +104,7 @@ describe('Working with keys', () => {
104104
location: expect.any(String),
105105
message: 'Arbitrary object error',
106106
name: 'Error',
107-
stack: expect.any(String),
107+
stack: expect.any(Array),
108108
},
109109
},
110110
},

0 commit comments

Comments
 (0)