簡體   English   中英

測試 log4js 每日推出日志的最佳方法是什么?

[英]What is the best way to test log4js daily rollout logs?

我在我的 nodejs 項目中包含了用於記錄應用程序的 log4js。 我想每天創建一個日志文件。 我將備份文件設置為保留 2 天。 所以我添加了以下配置。

{
"appenders": {
  "app": {
    "type": "dateFile",
    "filename": "logs/app.log",
    "pattern": "yyyy-MM-dd",
    "daysToKeep": 2,
    "timezoneOffset" : "1m",
    "keepFileExt": true
  },
  "errorFile": {
    "type": "dateFile",
    "filename": "logs/errors.log",
    "pattern": "yyyy-MM-dd",
    "daysToKeep": 2,
    "timezoneOffset" : "1m",
    "keepFileExt": true
  },
  "errors": {
    "type": "logLevelFilter",
    "level": "ERROR",
    "appender": "errorFile"
  }
},
"categories": {
  "default": { "appenders": [ "app", "errors" ], "level": "DEBUG" }
}

}

啟動應用程序后,它創建了兩個日志文件。 app.log 和 error.log。 我停止了應用程序並將這兩個文件重命名為 app.2021-07-18.log 和 error.2021-07-18(看起來已經創建了 7 月 19 日的日志文件)。 我再次啟動了應用程序並將這些文件重命名為它在 7 月 18 日創建的文件。 但是再次啟動應用程序后,它不會刪除 7 月 18 日創建的日志文件。 它只是創建了 app.log 和 error.log。

我是否錯誤地測試了它,或者我的配置是否錯誤地用於日志的日常部署?

只有兩種情況會啟動刪除日志操作:

  1. 程序不會關閉,日期會更改。
  2. 當日志文件過大(>maxLogSize)時,滾動 .

代碼: RollingFileWriteStream

問題: dateFile 附加程序“daysToKeep”不起作用

您可以使用以下代碼進行測試:

const log4js = require('log4js');
const path = require('path');
log4js.configure({
  appenders: {
    cheese: {
      type: 'dateFile',
      maxLogSize: 1 * 1024,
      filename: path.resolve('logs', 'client'),
      alwaysIncludePattern: true,
      pattern: '.yyyy-MM-dd.log',
      encoding: 'utf-8',
      category: '1og_date',
      daysToKeep: 1,
    },
  },
  categories: { default: { appenders: ['cheese'], level: 'error' } },
});

export const logger = log4js.getLogger('cheese');
for (let i = 0; i < 1000; i++) {
  logger.trace('Entering cheese testing');
  logger.debug('Got cheese.');
  logger.info('Cheese is Comté.');
  logger.warn('Cheese is quite smelly.');
  logger.error('Cheese is too ripe!');
  logger.fatal('Cheese was breeding ground for listeria.');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM