Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 632
Thiago P Avatar asked Dec 12 '25 10:12

Thiago P


1 Answers

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.

like image 110
Jim B. Avatar answered Dec 14 '25 23:12

Jim B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!