Skip to content
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

fix(core): fix empty error log in console logger #524

Merged
Merged
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
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