0

Currently I have implemented and express.js application and I have added log4js logger for logging purposes. Now I wanted to write logs into Redis and then background worker which continuously pulls logs from the Redis List and write those logs into a local log files.

App.js would look like this

import { getLogger, configure } from 'log4js';
import {initServer} from './middleware/Express'
import { initRoutes } from './routes/RouterAggregator';

configure('./src/config/log4js-config.json');
const  logger = getLogger("app");

const init = ()=> {
  logger.info("Initializing Application");
  initServer();
  initRoutes();
};

And log configuration file would look like this.

{
  "appenders": [
    {
      "type": "console",
      "layout": {
        "type": "pattern",
        "pattern": "[%d]  [%[%p%]] %c {%x{ln}} - %m",
        "tokens": {
          "ln" : "loggerFunction()"
        }
      }
    },
    {
      "type": "dateFile",
      "filename": "log/access.log",
      "pattern": "-yyyy-MM-dd",
      "alwaysIncludePattern": false,
      "category": "http",
      "layout": {
        "type"    : "pattern",
        "pattern": "[%d]  [%p] %c {%x{ln}} - %m",
        "tokens": {
          "ln" : "loggerFunction()"
        }
      }
    },
    {
      "type": "dateFile",
      "filename": "log/app.log",
      "maxLogSize": 10485760,
      "numBackups": 3,
      "alwaysIncludePattern": false,
      "layout": {
        "type"    : "pattern",
        "pattern": "[%d]  [%p] %c {%x{ln}} - %m",
        "tokens": {
          "ln" : "loggerFunction()"
        }
      }
    },
    {
      "type": "logLevelFilter",
      "level": "ERROR",
      "appender": {
        "type": "file",
        "filename": "log/errors.log"
      },
      "layout": {
        "type"    : "pattern",
        "pattern": "[%d]  [%p] %c {%x{ln}} - %m",
        "tokens": {
          "ln" : "loggerFunction()"
        }
      }
    }
  ]
}

I want to know how to add redis to support above mentioned functionality.

Chamika Kasun
  • 96
  • 3
  • 16

1 Answers1

1

Not sure the right way to do this but it seems you will have to write a custom appender that write to redis.

Example of built-in appenders for log4js are on github.

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • True, i found a package which has added support for this one, but note sure how to configure this logstash server conf, if you know please let me know. Thanks for your valuable input https://github.com/beyond5959/log4js-logstash-redis – Chamika Kasun Feb 15 '17 at 01:33