Skip to content

improv(logger): Format the stack trace as an array of strings in dev mode #3852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions packages/logger/tests/unit/initializeLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
})
);
});
});
6 changes: 3 additions & 3 deletions packages/logger/tests/unit/workingWithkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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),
},
},
},
Expand Down