Search code examples
javascriptnode.jsknex.jswinston

Piping debug.js output through winston


At my company we've been using Winston for a while for our own logs but it's become cumbersome to deal with other libraries' logs that use debugjs, such as Knex.js. We were thinking if there was anyway to pipe knex's debug output through winston but couldn't come up with anything.

What woould be the best way to have everything piped through out winston loggers?

Cheers!


Solution

  • Depends on what do you want to log.

    You can wire knex.on('query', ...), knex.on('query-response', ...), knex.on('query-error', ...) to printout sent queries:

    http://knexjs.org/#Interfaces-Events More info about that is found also in this answer Tracking DB querying time - Bookshelf/knex

    We are using this kind of solution to track slow queries and transactions.

    If you want to move just all internal console.logs to go to winston, monkey patching knex helpers https://github.com/tgriesser/knex/blob/master/src/helpers.js or global console.log is pretty much the only option.