I'm working on an node.js application with several dozen modules and using bunyan for logging (JSON output, multiple configurable streams). I've been looking for good examples of how to implement a instance across all the modules, but haven't seen what appears to be a really clean example I can learn from.
我在一个节点上工作。有几十个模块的js应用程序,使用bunyan进行日志记录(JSON输出,多个可配置流)。我一直在寻找如何跨所有模块实现实例的好例子,但还没有看到一个看起来非常干净的示例。
Below illustrates an approach that works, but seems quite inelegant (ugly) to me. I'm new to node & commonjs javascript in general, so looking for recommendations on how to improve it.
下面的例子说明了一种可行的方法,但在我看来很不优雅(丑陋)。我是node & commonjs javascript的新手,所以我想找一些关于如何改进它的建议。
module: ./lib/logger
模块:。/ lib /记录器
// load config file (would like this to be passed in to the constructor)
nconf.file({ file: fileConfig});
var logSetting = nconf.get('log');
// instantiate the logger
var Bunyan = require('bunyan');
var log = new Bunyan({
name: logSetting.name,
streams : [
{ stream : process.stdout,
level : logSetting.stdoutLevel},
{ path : logSetting.logfile,
level : logSetting.logfileLevel}
],
serializers : Bunyan.stdSerializers
});
function Logger() {
};
Logger.prototype.info = function info(e) { log.info(e) };
Logger.prototype.debug = function debug(e) { log.debug(e) };
Logger.prototype.trace = function trace(e) { log.trace(e) };
Logger.prototype.error = function error(e) { log.error(e) };
Logger.prototype.warn = function warn(e) { log.warn(e) };
module.exports = Logger;
module: main app
模块:主要应用
// create the logger
var logger = require('./lib/logger)
var log = new logger();
// note: would like to pass in options --> new logger(options)
module: any project module using logger
// open the logger (new, rely on singleton...)
var logger = require('./lib/logger');
var log = new logger();
或查看要点
any recommendations?
你有什么推荐吗?
EDIT:
编辑:
I've modified the constructor, making the singleton pattern explicit (rather than implicit as part of the 'require' behaviour.
我修改了构造函数,使单例模式显式(而不是隐式地作为“require”行为的一部分)。
var log = null;
function Logger(option) {
// make the singleton pattern explicit
if (!Logger.log) {
Logger.log = this;
}
return Logger.log;
};
and then changed the initialization to take an options parameter
然后更改初始化以获取选项参数
// initialize the logger
Logger.prototype.init = function init(options) {
log = new Bunyan({
name: options.name,
streams : [
{ stream : process.stdout,
level : options.stdoutLevel},
{ path : options.logfile,
level : options.logfileLevel}
],
serializers : Bunyan.stdSerializers
});
};
2 个解决方案
#1
23
Singleton pattern in nodejs - is it needed? Actually, singleton is perhaps not needed in Node's environment. All you need to do is to create a logger in a separate file say, logger.js:
node . js中的单例模式——需要吗?实际上,在Node的环境中可能不需要singleton。你所需要做的就是在一个单独的文件中创建一个日志记录器,比如logger.js:
var bunyan = require("bunyan"); // Bunyan dependency var logger = bunyan.createLogger({name: "myLogger"}); module.exports = logger;
Then, retrieve this logger from another module:
然后,从另一个模块检索这个日志记录器:
var logger = require("./logger"); logger.info("Anything you like");
#2
1
if you are using express with node.js then you can try this. By default, logging is disabled in Express. You have to do certain stuff to get logs working for your app. For access logs, we need to use the Logger middleware; for error logs we will use Forever.Hope it will help you.. Here is a good example How to Logging Access and Errors in node.js
如果您正在使用express with node。然后你可以试试这个。默认情况下,在Express中禁用日志记录。你必须做一些事情来让日志为你的应用工作。对于访问日志,我们需要使用日志程序中间件;对于错误日志,我们将永远使用。希望它对你有帮助。这里有一个很好的例子,说明如何在node.js中记录访问和错误。
#1
23
Singleton pattern in nodejs - is it needed? Actually, singleton is perhaps not needed in Node's environment. All you need to do is to create a logger in a separate file say, logger.js:
node . js中的单例模式——需要吗?实际上,在Node的环境中可能不需要singleton。你所需要做的就是在一个单独的文件中创建一个日志记录器,比如logger.js:
var bunyan = require("bunyan"); // Bunyan dependency var logger = bunyan.createLogger({name: "myLogger"}); module.exports = logger;
Then, retrieve this logger from another module:
然后,从另一个模块检索这个日志记录器:
var logger = require("./logger"); logger.info("Anything you like");
#2
1
if you are using express with node.js then you can try this. By default, logging is disabled in Express. You have to do certain stuff to get logs working for your app. For access logs, we need to use the Logger middleware; for error logs we will use Forever.Hope it will help you.. Here is a good example How to Logging Access and Errors in node.js
如果您正在使用express with node。然后你可以试试这个。默认情况下,在Express中禁用日志记录。你必须做一些事情来让日志为你的应用工作。对于访问日志,我们需要使用日志程序中间件;对于错误日志,我们将永远使用。希望它对你有帮助。这里有一个很好的例子,说明如何在node.js中记录访问和错误。