Skip to content

Commit

Permalink
fix(core): fix empty error log in console logger (#524)
Browse files Browse the repository at this point in the history
The console logger stringified the data object. Appereantly the Error class has no enumerable properties which means it will be stringified as an empty object. This fix checks if a (top level) value being transformed is an error and will transform the non-enumerable properties of the error.

Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Nov 8, 2021
1 parent 5e9a641 commit 7d9c541
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/core/src/logger/ConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
import { BaseLogger } from './BaseLogger'
import { LogLevel } from './Logger'

/*
* The replacer parameter allows you to specify a function that replaces values with your own. We can use it to control what gets stringified.
*/
function replaceError(_: unknown, value: unknown) {
if (value instanceof Error) {
const newValue = Object.getOwnPropertyNames(value).reduce(
(obj, propName) => {
obj[propName] = (value as unknown as Record<string, unknown>)[propName]
return obj
},
{ name: value.name } as Record<string, unknown>
)
return newValue
}

return value
}

export class ConsoleLogger extends BaseLogger {
// Map our log levels to console levels
private consoleLogMap = {
Expand All @@ -27,7 +45,7 @@ export class ConsoleLogger extends BaseLogger {

// Log, with or without data
if (data) {
console[consoleLevel](`${prefix}: ${message}`, JSON.stringify(data, null, 2))
console[consoleLevel](`${prefix}: ${message}`, JSON.stringify(data, replaceError, 2))
} else {
console[consoleLevel](`${prefix}: ${message}`)
}
Expand Down

0 comments on commit 7d9c541

Please sign in to comment.