Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log from Node.js with Express to ELK?

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?

like image 770
Michael Avatar asked Jan 06 '16 16:01

Michael


People also ask

Can you send logs directly to Elasticsearch?

Java logs can be sent to Elasticsearch for later retrieval and analysis. They can be sent directly from the application or written to files and later shipped by a data shipper such as Elasticsearch's own Filebeat.

How do I enable logging in node JS?

Logging in Node. By default, it will not produce any output. To enable this logger, you have run your application with a special environment variable, called DEBUG . Once you do that, the debug module will come to life and will start producing log events for stdout.


1 Answers

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");
like image 176
Simon Johnson Avatar answered Oct 06 '22 00:10

Simon Johnson