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

Posted on

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?

Solution :

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

  "appenders": [
      type: "console",
      category: "myLogger"
      "host": "",
      "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");"Test log message %s", "arg1", "arg2");

Leave a Reply

Your email address will not be published. Required fields are marked *