Search code examples
javascriptnode.jsloggingwinston

winstonjs Get logger created with winston.createLogger(...)


Question
Winston provides winston.loggers.get("logger name") in order to get a logger. How do I get a logger created using winston.createLogger(options) (it does not have a name)

Why I'm asking
I've created a logger in a file using winston.createLogger(options), and then export the created logger using module.exports.

I'd like to use this logger throughout my application but everytime I require("") it, createLogger(options) is called, thus creating a new logger every time.

Code sample

const winston = require('winston');

// this logger does not have a name!
// Thus, how do I get it.
const logger = winston.createLogger({});


module.exports = logger;

Solution

  • Formal answer for posterity:

    Multiple require() calls in a single node.js process will not create new instances of the module. The first one will, then subsequent calls will return a reference to the first one. This is by design, and very handy.