以后聚合日志并写入文件而不直接写入文件

时间:2017-02-14 11:42:56

标签: javascript node.js logging redis log4js-node

目前我已经实现了express.js应用程序,并且我已经添加了log4js记录器以进行日志记录。现在我想将日志写入Redis,然后将后台工作程序写入Redis列表中,并将这些日志写入本地日志文件。

App.js看起来像这样



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();
};




日志配置文件看起来像这样。



{
  "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()"
        }
      }
    }
  ]
}




我想知道如何添加redis来支持上述功能。

1 个答案:

答案 0 :(得分:1)

不确定正确的方法,但似乎你必须写一个写入redis的自定义appender。

log4js的built-in appenders示例位于github上。