Introduction#
In the fields of security and development, logs can reflect ongoing potential security risks, performance issues, and various events, which are beneficial for program improvement and testing.
Through logs, we can easily identify the causes of problems afterward and quickly make modifications to the source of the issues.
Quick Start#
Official documentation for log4js: https://log4js-node.github.io/log4js-node/
Basic usage method, using logger to print a string, and calling getLogger to obtain an instance of the logger:
const log4js = require('log4js')
const logger = log4js.getLogger()
logger.debug('This is a test message')
Log Levels#
Logs have different levels, each with different weights, from low to high:
1: TRACE
2: DEBUG
3: INFO
4: WARN
5: ERROR
6: FATAL
7: MARK
Type#
In log4js, there is a concept of category (type), which allows you to see which module the logs are coming from, or to configure custom types.
const log4js = require('log4js')
const logger = log4js.getLogger('abc')
logger.debug('At this point, the logger type is abc')
Appender#
If you want to output logs to a file or customize some configurations, you can configure the Appender to accomplish this.
Below is the default Appender configuration for log4js; by default, all logs will be output to the console:
defaultConfig = {
appenders: [{
type: "console"
}]
}
You can also create your own Appender configuration using log4js's configuration method:
log4js.configure({
appenders: {
out: {
type: 'stdout'
},
info: {
type: 'file',
filename: 'logs/info.log',
maxLogSize: 52428800
}
}
})
Under construction...