Logging is a very important part of online projects. One of the more commonly used logging components is Log4JS, which is often used in conjunction with Express. This article starts with an introductory example of how to use Log4JS and how to integrate it with Express.

Introductory example

The following output logs are generated, including the log printing time, log level, log type, and log content.

// started.js var log4js = require('log4js'); var logger = log4js.getLogger(); logger.debug('hello world'); [2017-02-28 21:28:22.853] [DEBUG] [default] - Hello worldCopy the code

The level of logging

logger.setLevel(‘INFO’); Indicates that the lowest level of logs to be printed is INFO, that is, logs will not be printed by calling interfaces below INFO, such as logger.debug().

var log4js = require('log4js'); var logger = log4js.getLogger(); logger.setLevel('INFO'); logger.debug('level: debug'); logger.info('level: info'); logger.error('level: error'); // [2017-02-28 21:50:45.372] [INFO] [default] - level: Info // [2017-02-28 21:50:45.376] [ERROR] [default] - level: ERRORCopy the code

Log categories

In addition to the level, you can also classify logs, log4js.getLogger(category), as shown below

var log4js = require('log4js'); var alogger = log4js.getLogger('category-a'); var blogger = log4js.getLogger('category-b'); alogger.info('hello'); blogger.info('hello'); // [2017-02-28 22:36:57.570] [INFO] category- a-hello // [2017-02-28 22:36:57.574] [INFO] category- b-helloCopy the code

appenders

Appenders specifies the log output location. Multiple appenders can be configured at the same time, separated by category. For example, log4js.getLogger(‘info’) applies a configuration of type dateFile.

Note that configurations with type console do not declare a category, so all logs are printed to the console.

var log4js = require('log4js'); log4js.configure({ appenders: [ { type: 'console'}, { type: 'dateFile', filename: './logs/info.log', category: 'info' } ] }); var logger = log4js.getLogger('info'); logger.setLevel('INFO'); logger.trace('trace'); logger.debug('debug'); logger.info('info'); // The following output is displayed: // [2017-02-28 22:51:30.723] [INFO] info-infoCopy the code

Express application

A simple example would be to print all logs to the console.

var express = require('express'); var log4js = require('log4js'); var app = express(); log4js.configure({ appenders: [ { type: 'console', category: 'app' } ] }); var logger = log4js.getLogger('app'); logger.setLevel('INFO'); App.use (log4js.connectLogger(logger)); app.use(function(req, res, next){ res.send('ok'); }); app.listen(3000);Copy the code

Visit http://127.0.0.1:3000 and the following logs are displayed

[2017-03-01 00:28:29.301] [INFO] APP - :: FFFF :127.0.0.1 - - "GET/HTTP/1.1" 304 - "" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/ 57.36 (KHTML, like Gecko) Chrome/ 57.36"Copy the code

When log4js.connectLogger(logger), you can declare the log level.

Logger. setLevel('INFO'); // The log level is WARN app.use(log4js.connectLogger(logger, {level: 'WARN'}));Copy the code

Note that if the declared log level is lower than the specified logger.setlevel (level) level, the log will not be printed, as shown in the following example.

logger.setLevel('INFO'); 

app.use( log4js.connectLogger(logger, {level: 'DEBUG'}) );
Copy the code

A link to the

Website: https://github.com/nomiddlename/log4js-node