我需要一个使用http传输在服务器上发布日志的工作示例。我可以使用文件传输和控制台传输来记录文件和控制台上的消息。同样我尝试做 http 传输,winston clien的源代码看起来像这样:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.Http)({ host: 'localhost', port:8080 })
]
});
logger.log('info', 'Hello world');
在运行此代码时,我收到如下错误:
D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\lib\winston\transports\http.js:52
req = (self.ssl ? https : http).request({
^
ReferenceError: self is not defined
at Http._request (D:\Balwant\Devlopment- Env\LoggingDemo\node_modules\winston\lib\winston\transports\http.js:52:10)
at Http.log (D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\lib\winston\transports\http.js:109:8)
at emit (D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\lib\winston\logger.js:175:17)
at D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\node_modules\async\lib\async.js:111:13
at Array.forEach (native)
at _each (D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\node_modules\async\lib\async.js:32:24)
at Object.async.each (D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\node_modules\async\lib\async.js:110:9)
at Logger.log (D:\Balwant\Devlopment-Env\LoggingDemo\node_modules\winston\lib\winston\logger.js:203:9)
at Object.<anonymous> (D:\Balwant\Devlopment-Env\LoggingDemo\logger.js:10:8)
at Module._compile (module.js:456:26)
由于我是node.js的新手,我不知道如何处理它。我试着谷歌搜索解决方案,但无法得到任何解决方案。 请帮助我。
由于
答案 0 :(得分:1)
首先winston和winstond是不同的套餐。您可以查看this blog post有关差异的信息。
您可能还需要为winston启动自己的HTTP传输(如果您真的想使用winston)。因此它可以与您的特定端点协议一起使用。
您可以按指定的here创建自定义winston传输。然后,您可以使用类似request的内容将日志数据发送到HTTP端点。
祝你好运。我不相信winston.transports.Http
存在,但我认为你正在寻找这个:
new winston.transports.Webhook({ 'host': 'localhost', 'port': 8080, 'path': '/collectdata' })
如this example所示。
我的错误,it does have winston.transports.Http
但您需要 winstond 。
您可以检查this example以使用 winstond 作为http守护程序。然后将您的Http日志发送给它。
所以这个:
new winston.transports.Http({ host: 'localhost', port: 8080})
应指向 winstond 守护程序,如:
var winstond = require('winstond');
var http = winstond.http.createServer({
services: ['collect', 'query', 'stream'],
port: 8080
});
http.add(winstond.transports.Console, {});
http.listen();
答案 1 :(得分:-1)
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
add this line were ever u wanted to lg the error message
var logger = require('../../config/logger.js');
logger.info("the default logger with my tricked out transports is rockin this module");