我有一个带有Express的Node.js服务器应用程序.我想将其活动记录到ElasticSearch中,并使用Kibana可视化日志.
这样做的正确方法是什么?
我应该写一个json行的日志文件并用Logstash读取它吗?
我推荐log4js.它有一系列有用的appender,而logstash就是其中之一.它适用于UDP.
以下是从log4js站点获取的示例:
var log4js = require('../lib/log4js');
/*
Sample logstash config:
udp {
codec => json
port => 10001
queue_size => 2
workers => 2
type => myAppType
}
*/
log4js.configure({
"appenders": [
{
type: "console",
category: "myLogger"
},
{
"host": "127.0.0.1",
"port": 10001,
"type": "logstashUDP",
"logType": "myAppType", // Optional, defaults to 'category'
"fields": { // Optional, will be added to the 'fields' object in logstash
"field1": "value1",
"field2": "value2"
},
"layout": {
"type": "pattern",
"pattern": "%m"
},
"category": "myLogger"
}
]
});
var logger = log4js.getLogger("myLogger");
logger.info("Test log message %s", "arg1", "arg2");