fix: explicitly stringify extra fields passed to the logger service

Signed-off-by: Paul Schultz <pschultz@pobox.com>
This commit is contained in:
Paul Schultz
2025-02-20 16:11:03 -06:00
parent 8155b0493f
commit ecb9babcfc
3 changed files with 6 additions and 25 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.
@@ -93,28 +93,4 @@ describe('WinstonLogger', () => {
expect.any(Function),
);
});
it('gracefully handles fields that are not castable to a string', () => {
const mockTransport = new Transport({
log: jest.fn(),
logv: jest.fn(),
});
const logger = WinstonLogger.create({
transports: [mockTransport],
});
logger.error('something went wrong', {
field: Object.create(null),
});
expect(mockTransport.log).toHaveBeenCalledWith(
expect.objectContaining({
[MESSAGE]: expect.stringContaining(
'[field value not castable to string]',
),
}),
expect.any(Function),
);
});
});
@@ -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]';
}