neumino / rethinkdbdash

An advanced Node.js driver for RethinkDB with a connection pool, support for streams etc.
MIT License
848 stars 109 forks source link

Improved logging #355

Open aleclarson opened 6 years ago

aleclarson commented 6 years ago

This provides improved logging, as requested by #325, #351, and https://github.com/neumino/rethinkdbdash/issues/334#issuecomment-293423825.

Overview

Log levels

The valid values for options.logLevel are based off winston, minus the silly level and custom levels.

Log levels are assigned an integer representing their importance. This integer is used to silence logs whose log level has an integer of higher value. As an example, if options.logLevel equals verbose, only debug logs are silenced. Likewise, if options.logLevel equals error, only error logs are not silenced.

The default value of options.logLevel is info, but you could argue that debug is more appropriate so the end user can provide more context when something goes wrong.

I made my best guesses on which log level should be used for each log-worthy event. Let me know if the classification of any events should be changed.

The debug events are always followed by an error event, which emits the stack trace.

'error' event

I added an error event to the PoolMaster. This allows the end user to do whatever they like with the Error object (as requested in #325). My use case for this is overriding Error.prepareStackTrace to preserve the callsite objects, listening to the error event, and logging the error as JSON for easier consumption by logging dashboards.

If you would like, I can separate this change into its own PR, but I think it's useful and harmless to anyone who doesn't need it.

marshall007 commented 6 years ago

I don't see a strong need for options.logLevel. Virtually all logging implementations have mechanisms for controlling log level behavior upstream. Having any default value whatsoever for options.logLevel complicates things when you are providing a logging instance/function that you would expect to control that behavior.

Aside from that I think this is pretty fantastic.

aleclarson commented 6 years ago

@marshall007 Thanks for the feedback! I would argue that options.logLevel is useful when options.log is undefined or a function, providing easy filtering with only 13 lines of code. I think the best compromise is to make debug the default. Then anyone using a logging library won't be disturbed with setting options.logLevel for the rethinkdb constructor.

aleclarson commented 6 years ago

Small change: When options.log is a function, its arguments are now (level, message) instead of (message, level), but any log event listeners are still passed (message, level)

neumino commented 6 years ago

A few thoughts

neumino commented 6 years ago

Overall the first 2 points are the main reason why I think it's a bit wonky to have warn/info for rethinkdbdash.

I think it's fine to move the draining messages to debug, but the other messages should all be at the same level.

aleclarson commented 6 years ago

Good points, @neumino!

Here's an updated list of log levels per message:

The verbose log level has been removed.

aleclarson commented 6 years ago

@neumino Are there any blockers on getting this merged? Should I update the docs too?