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

前端 未结 1 961
心在旅途
心在旅途 2020-12-30 05:57

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 wa

相关标签:
1条回答
  • 2020-12-30 06:38

    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");

    0 讨论(0)
提交回复
热议问题