Merge pull request #28931 from schultzp2020/issue-28905

fix: explicitly stringify extra fields passed to the logger service
This commit is contained in:
Patrik Oldsberg
2025-02-21 14:05:56 +01:00
committed by GitHub
3 changed files with 12 additions and 14 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
Explicitly stringify extra logger fields with `JSON.stringify` to prevent `[object Object]` errors.
@@ -94,27 +94,20 @@ describe('WinstonLogger', () => {
);
});
it('gracefully handles fields that are not castable to a string', () => {
const mockTransport = new Transport({
log: jest.fn(),
logv: jest.fn(),
});
it('gracefully handles fields that contain deeper object structures', () => {
const log = jest.fn();
const mockTransport = new Transport({ log });
const logger = WinstonLogger.create({
transports: [mockTransport],
});
logger.error('something went wrong', {
field: Object.create(null),
field: { foo: { bar: { baz: 'qux' } } },
});
expect(mockTransport.log).toHaveBeenCalledWith(
expect.objectContaining({
[MESSAGE]: expect.stringContaining(
'[field value not castable to string]',
),
}),
expect.any(Function),
expect(log.mock.calls[0][0][MESSAGE]).toContain(
`={"foo":{"bar":{"baz":"qux"}}}`,
);
});
});
@@ -151,7 +151,7 @@ export class WinstonLogger implements RootLoggerService {
let stringValue = '';
try {
stringValue = `${value}`;
stringValue = JSON.stringify(value);
} catch (e) {
stringValue = '[field value not castable to string]';
}