Skip to content

Commit c17e26d

Browse files
committed
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
1 parent 316fee1 commit c17e26d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

packages/logger/src/formatter/LogFormatter.ts

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ abstract class LogFormatter {
131131
}
132132
}
133133

134+
if (
135+
this.envVarsService?.isDevMode() &&
136+
typeof formattedError.stack === 'string'
137+
) {
138+
formattedError.stack = formattedError.stack.split('\n');
139+
}
140+
134141
return formattedError;
135142
}
136143

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)