Is it possible to use winston logging and debug module together? Is it possible to use winston logging and debug module together? express express

Is it possible to use winston logging and debug module together?


After using several different loggers I gain more insight about nodejs loggers, now I believe they are not supposed to be used together b/c they are designed for different purposes. On the other hand, morgan and winston can be used together, e.g. Node.js - logging / Use morgan and winston, morgan and debug can be used together too, Nodejs - How to use morgan with debug

But first of all, to quote from Logging in Node.js done right

Setting up proper logging in Node.js applications can be bitoverwhelming at first due to the plethora of modules available throughNPM.

That was indeed my situation when I had morgan, winston, debug together. But then I realized they are for different purposes with overlaps. So I use debugjs when I really debug an issue instead of using console.log(some said debugjs is not a logger). After I am done with it I turn that debug off.

Morgan is to log express HTTP request specifically. Better to use it in dev/prod env for different purposes.

Using different log levels for Winston in dev/prod env is probably a common practice. I can also use express-winston to log HTTP request instead of using morgan.

Winston uses a different log level to turn off some log, unlike debugjs to use namespace to turn log off so I don't think they can work together.

--- update 2021 ---

Since I first answered my own question in 2018, I was asked several times about the log level related question. I found an issue opened against debugjs confirmed what I said about debug, to quote the answer from its current maintainer

Debug isn't a general purpose logging library, it's meant toconditionally turn on debug logs. There is no concept of log levels.Instead, break up your namespaces into different components and enablethe ones you care about


It can be useful to start passing debug(...) calls into winston. You can override the debug.log function to achieve that.

const logger  = require('./v1/lib/logger'); //my winston loggerconst util    = require('util');const debug     = require('debug');//for testing and dev use the regular debug (optional) if (process.env.NODE_ENV === 'production') {  debug.log = (...args) => logger.info(util.format(...args))}module.exports =  debug;


When dealing with debug and winston, you may also want to override formatArgs function, since debug.log is receiving already colored arguments (depending on enviroment) and it may have unexpected results in some cases (e.g. setting env vs module loading order).

import debug from "debug";import util from "util";import winston from "winston";// create winston loggerconst logger = new winston.Logger({ /*...*/ });// keep original argumentsdebug.formatArgs = (args) => { /* do nothing */ };// override logging (keep function to use `this`)debug.log = function(...args) {  // log to winston  logger.log("info", {    // `this` is bound to debug instance    namespace: this.namespace,    // format as in `console.log`    message: util.format(...args),    // add message time    timestamp: new Date().toISOString(),    // optionally log also diff time    diff: '+' + debug.humanize(this.diff),  });};// enable all debugdebug.enable('*');