admin管理员组

文章数量:1429327

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;

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;
Share Improve this question asked Jan 1, 2018 at 22:59 Thiago PThiago P 2851 gold badge5 silver badges14 bronze badges 3
  • Yeah that's the only way if you want to work on it on multiples files via require, it's simple as a stream. (I suppose you don't work with cluster mod or fork, because if it is you can pass arguments in some cases, that suppose the other file is executed as a child_process) – nodeover Commented Jan 1, 2018 at 23:20
  • How are you using your logger module? I don't see any problem with what you're doing that would cause multiple loggers to get created. Node caches the module.exports from the first require() and returns it to subsequent calls. – Jim B. Commented Jan 2, 2018 at 0:04
  • My concern is that multiple files will contain require('logger.js'), resulting in the logger being created multiple times. I did not know node cached require() so I suppose my concerns are invalid. Thank you Jim Baldwin – Thiago P Commented Jan 2, 2018 at 2:06
Add a ment  | 

2 Answers 2

Reset to default 6

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.

For anyone who still want's to be able to get a winston logger by name when it was created using createLogger(), I haven't found an answer.

But, you can do it this way.
Create the logger using winston.loggers.add(name, options)
Get the logger using winston.loggers.get(name)

Also, Node JS require() caches modules. WHO KNEW!

Thanks for the help.

本文标签: javascriptwinstonjs Get logger created with winstoncreateLogger()Stack Overflow