admin管理员组文章数量:1426655
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
2 Answers
Reset to default 6Formal 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
版权声明:本文标题:javascript - winstonjs Get logger created with winston.createLogger(...) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745463300a2659421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论