Detailed introduction and use of log plug-in log4js (super full)

log4js

log4js is a popular Node.js logging library that allows you to easily implement flexible and efficient logging in your Node.js applications. log4js provides different log levels, including: trace, debug, info, warn and error. You can set these levels as needed to better understand your application health. In addition, log4js also provides multiple output targets, including console output, file output, and logging server.

The following are some common parameters and usage methods of log4js:

1. Parameter configuration

log4js can be configured through a JSON file, for example:

{
  "appenders": {
    "console": { "type": "console" },
    "file": {
      "type": "file",
      "filename": "logs/app.log",
      "maxLogSize": 10485760,
      "backups": 3,
      "compress": true
    }
  },
  "categories": {
    "default": { "appenders": ["console", "file"], "level": "info" }
  }
}

Here we define two output targets: console and file, which are used to output to the console and file respectively. Among them, the log of file will be written to the logs/app.log file, and the size of each log file does not exceed 10MB, and a maximum of 3 backups are saved, and compression is enabled. categories are used to specify the log level and output target.

2. How to use

It is very simple to use log4js in a Node.js application. You only need to install the log4js dependency, then introduce the log4js library into the code, and configure it as needed.

For example, we can use log4js in our application in the following ways:

const log4js = require('log4js');

// 加载配置文件
log4js.configure('./log4js.json');

// 获取logger对象
const logger = log4js.getLogger();

// 记录日志
logger.trace('This is a trace message.');
logger.debug('This is a debug message.');
logger.info('This is an info message.');
logger.warn('This is a warning message.');
logger.error('This is an error message.');

In the above code, we first load the configuration file through the log4js.configure method, then obtain the logger object through the log4js.getLogger method, and finally use the logger object to record logs. According to the categories we defined in the configuration file, by default, all log information with a level of info and above will be output to the console and log files at the same time.

In addition to using log4js directly in the application, we can also integrate it with other Node.js frameworks (such as Express and Koa) to better record and manage the log information of the application.

3. The following is a detailed description of the common parameters in the log4js configuration file:

  1. appenders : Specifies the log output target, multiple can be defined, each output target has its own type and parameter configuration.
    • type: 指定输出目标的类型,包括:console、file、dateFile、logLevelFilter、logFaces-UDP、logFaces-TCP、logFaces-HTTP、multiprocess、smtp、slack、hipchat等。
    • filename: 指定输出到文件时的文件名,可以是绝对路径或相对路径。
    • maxLogSize: 指定日志文件的最大大小,超过这个大小后将自动进行文件切割。默认为null,表示不限制文件大小。
    • backups: 指定保留的备份数量,当日志文件切割时会自动保存指定数量的备份文件。默认为5。
    • compress: 指定是否启用压缩,当日志文件进行切割时,可以将备份文件进行压缩以节省磁盘空间。默认为false。
    • layout: 指定输出格式化方式,可以是pattern、json等。
  1. categories: 指定日志级别和输出目标的对应关系,可以定义多个。
    • default: 指定默认的输出目标和日志级别。
    • appenders: 指定输出目标,可以是单个或多个,对应appenders中的输出目标名称。
    • level: 指定输出目标的日志级别,包括:trace、debug、info、warn、error、fatal等。
  1. levels: 指定全局日志级别,对于没有在categories中指定的输出目标,会使用该全局日志级别。
  2. pm2: 指定在pm2中的配置参数。
    • appenders: 指定pm2启动时使用的输出目标,可以定义多个。
    • outFile: 指定标准输出日志的文件路径。
    • errorFile: 指定错误输出日志的文件路径。

以上是常见的log4js配置参数,根据具体需求,也可以定义自己的配置参数。在使用log4js时,可以根据实际情况进行配置,以满足不同场景的需求。

Guess you like

Origin juejin.im/post/7221445996135022653