【发布时间】:2016-08-16 04:08:34
【问题描述】:
我有一个 Sails.js 应用程序,我希望能够使用 Winston 和 Loggly 来记录消息。现在,我有这样的服务:
log: function(type, message, content) {
// Require and configure Winston with Loggly
const winston = require('winston');
require('winston-loggly');
winston.add(winston.transports.Loggly, {
token : ***, // Hiding my real token
subdomain : ***,
tags : ['sails-web-service'],
json :true
});
// Attach context to the content
var finalContent = { timestamp: Date.now(), pid: process.pid };
for (var attribute in content) { finalContent[attribute] = content[attribute]; }
// Send the log
winston.log(type, message, finalContent);
}
我可以通过以下方式调用此服务:LogService.log('info', 'Log in attempt', { email: req.body.email });。
理想情况下,我只想在应用程序的任何地方直接使用 Winston,如下所示:winston.log('info', 'Log in attempt', { email: req.body.email });,而不必创建服务或必须在我想使用它的每个控制器中要求和配置 Winston。如何使其全球可用?
【问题讨论】:
标签: javascript node.js logging sails.js winston