DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • node

    • NODE
    • parseArgs
    • compose
    • date-format
    • dayjs
    • encrypt
    • env-cmd
    • glob
    • Koa 洋葱模型
    • lodash
    • logger
    • log4js
    • mongod
    • nanoid
    • node-red
    • node-stream
    • node:test
    • nodemailer
    • nodemon
    • nodered
    • parse-curl
    • pm2
    • toml
    • ws
    • yargs
    • zx

log4js

日志管理

npm i log4js

appenders

type=dateFile

datefile:{
  type: 'dateFile',  // 导出文件
  pattern: 'hh',  // 切割日志文件的时候  按小时 划分 .yyyy-MM-dd 
  filename: getPath(), // 文件路径 
  maxLogSize: 5242880, // 5M
  daysToKeep: 1e5,  //  保留天数
  compress: true, // 压缩成 .tar.gz 文件
  keepFileExt: true,  // 保留文件后缀 如 log.1.log log.2.log
}

layout

%x{time} 指定自定义 tokens,可添加自定义 日志前缀

{
  type: 'pattern',
  pattern: '[%x{time}] [%p] %m',
  tokens: {
    time: function (logEvent) {
      // eslint-disable-next-line no-unused-vars
      const { startTime, categoryName, data } = logEvent;
      return `${timeStamp()}`;
    },
  },
}

categories

`logger.js` 日志切割,生成日志文件
const path = require('path');
const log4js = require('log4js');
const { timeStamp } = require('.');
const serverLogPath = '/serverpath/log';
const localLogPath = '../../log';
const dayjs = require('dayjs');

const folder = dayjs(Date.now() + (process.env.prod ? 8 * 36e5 : 0)).format('YYYY-MM-DD');
const getPath = (str = 'log') => {
  return path.resolve(__dirname, process.env.prod ? serverLogPath : localLogPath, `${folder}/${str}`);
};

// 
log4js.configure({
  appenders: {
    default: {
      type: 'dateFile',
      pattern: 'hh',
      filename: getPath(),
      maxLogSize: 5242880, // 5M
      daysToKeep: 1e5,
      // compress: true,
      keepFileExt: true,
      layout: {
        type: 'pattern',
        pattern: '[%x{time}] [%p] %m',
        tokens: {
          time: function (logEvent) {
            // eslint-disable-next-line no-unused-vars
            const { startTime, categoryName, data } = logEvent;
            return `${timeStamp()}`;
          },
        },
      },
    },
    order: {
      type: 'dateFile',
      pattern: 'hh',  // defaul  .yyyy-MM-dd
      filename: getPath('order'),
      maxLogSize: 5242880, // 5M
      daysToKeep: 1e5,
      // compress: true,
      keepFileExt: true,
      layout: {
        type: 'pattern',
        pattern: '[%x{time}] [%p] %m',
        tokens: {
          time: function (logEvent) {
            // eslint-disable-next-line no-unused-vars
            const { startTime, categoryName, data } = logEvent;
            return `${timeStamp()}`;
          },
        },
      },
    },
    console: {
      type: 'console',
      layout: {
        type: 'pattern',
        pattern: '[%x{time}] [%p] %m',
        tokens: {
          time: function (logEvent) {
            // eslint-disable-next-line no-unused-vars
            const { startTime, categoryName, data } = logEvent;
            return `${timeStamp()}`;
          },
        },
      },
    },
  },
  categories: {
    order: { appenders: ['order', 'console'], level: 'all' },
    default: { appenders: ['default', 'console'], level: 'all' },
  },
});
process.on('exit', () => {
  log4js.shutdown();
});
process.on('uncaughtException', () => {
  log4js.shutdown();
});

module.exports = {
  logger: log4js,
  getLogger: categorie => log4js.getLogger(categorie || 'default'),
};

Refer

  • npm log4js
  • log4js Doc
Last Updated:
Contributors: rosendo
Prev
logger
Next
mongod