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

Log an error object along with context-specific error message #15

Open
raine opened this issue Jun 1, 2019 · 3 comments
Open

Log an error object along with context-specific error message #15

raine opened this issue Jun 1, 2019 · 3 comments

Comments

@raine
Copy link

raine commented Jun 1, 2019

Hello,

Thanks for a great library.

I want to log an error object along with a message that describes the context of the error.

log.error('Error while doing x', err)

However, that results in

[error] Error while doing x

I'm aware you can do log.error(err), which results in a nice log message of the error, but most of time I actually want to include a custom message, without logging two lines.

What if log.error('Error while doing x', err) printed the following?

[error]: Error while doing x error="[object Object]" stack="TypeError: Cannot read prope
rty 'length' of null
    at redis.mgetAsync.then.then
message="Cannot read property 'length' of null"
@ianstormtaylor
Copy link
Owner

Sounds great to me!

@raine
Copy link
Author

raine commented Jul 3, 2019

How do you feel about this?

try {
  JSON.parse('#')
} catch (err) {
  log.error('failed to parse json', err)
}
[error] failed to parse json message="Unexpected token # in JSON at position 0" stack="SyntaxError: Unexpected token # in JSON at position 0
    at JSON.parse (<anonymous>)

If this makes sense to you, I'll open a PR.


Another example that shows how extra properties of Error are merged to data as well.

log.error('failed to update podcast', err)
error] failed to update podcast message="column \"sumary\" of relation \"podcast\" does not exist" stack="error: column \"sumary\" of relation \"podcast\" does not exist
   at Connection.parseE (/node_modules/pg/lib/connection.js:602:11)
   at Connection.parseMessage (/node_modules/pg/lib/connection.js:399:19)
   at Socket.<anonymous> (/node_modules/pg/lib/connection.js:121:22)
   at Socket.emit (events.js:189:13)
   at Socket.EventEmitter.emit (domain.js:441:20)
   at addChunk (_stream_readable.js:284:12)
   at readableAddChunk (_stream_readable.js:265:11)
   at Socket.Readable.push (_stream_readable.js:220:10)
   at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)" name=error length=128 severity=ERROR code=42703 detail= hint= position=40 internalPosition= internalQuery= where= schema= table= column= dataType= constraint= file=analyze.c line=2346 routine=transformUpdateTargetList

@ianstormtaylor
Copy link
Owner

@raine sounds good to me! As close as possible to the existing log.error(e) behavior but with the updated message would be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants