I have a Node.js server application with Express. I would like to log its activity into ElasticSearch and visualize the logs using Kibana.
What would be the right way to do that?
Should I write a log file of json lines and read it with Logstash?
I'd recommend log4js. It has a range of useful appenders, and logstash is one of them. It works over UDP.
Here is an example taken from the log4js site:
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");