log4js-node / log4js-node

A port of log4js to node.js

Home Page:https://log4js-node.github.io/log4js-node/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Used together with PM2, the log did not enter the corresponding file

maoweifan opened this issue · comments

My project was launched through PM2
for example pm2 start index.js --name server

const log4js = require("log4js");

const loggerFactory = (category) => {
    const logger = log4js.getLogger(category);
    logger.level = "debug";
    return logger;
}

const logger = loggerFactory('debug');

logger.log('i am log message')
logger.debug('i am debug message')
logger.info('i am info message')
logger.warn('i am warn message')
logger.error('i am error message')

console.log('console.log')
console.debug('console.debug')
console.info('console.info')
console.warn('console.warn')
console.error('console.error')

The problem is:
When PM2 starts, it will take over the log files on the console and input them into the PM2 management point log. I used this method, and when PM2 starts, two files will be generated for the project

cd ~/.pm2/logs
image

But only console.warn and console.error are written to chat server-error.log
image

All others have been written to server-out.log
image

All logs of log4js have been written to server-out.log, and what I want is that the behavior of log4js is also the same as that of console, writing warnings and errors to server-error.log in pm2. So, what should I do?

What I want

log4js.debug -> pm2 out.log
log4js.log(log4js.info) -> pm2 out.log

log4js.warn -> pm2 error.log
log4js.error -> pm2 error.log

Hi, log4js follows the log4j logging level convention. It is hierarchical which is quite an industry standard.
This is also the same implementation in browsers; log level filtering under Developer tools (F12).

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < MARK < OFF

Hence, if your level is set to debug, you will see everything above debug.
That includes info, warn, error, fatal, mark, off.

You can read more here:
https://log4js-node.github.io/log4js-node/api.html

Vertical header displays the level of the log event.
Horizontal header shows the logging level configuration.
image
(src: https://www.javatpoint.com/log4j-logging-levels)

PM2, however, is different. It has only 4 types of log levels (debug, info, warn, error) and 3 types of files:

  • error_file only contains warn and error
  • out_file only contains debug and info
  • log_file contains everything (debug, info, warn, error)
Field Type Example Description
error_file (string)   error file path (default to $HOME/.pm2/logs/-error-.log)
out_file (string)   output file path (default to $HOME/.pm2/logs/-out-.log)
log_file (string)   file path for both output and error logs (disabled by default)

(src: https://pm2.keymetrics.io/docs/usage/log-management)

So, if you want to achieve the kind of PM2 log level implementation, you would need to use logLevelFilter appender.
In that, you can specify the level (minLevel) and the maxLevel.
https://log4js-node.github.io/log4js-node/logLevelFilter.html