SoFunction
Updated on 2025-03-09

Express Advanced Log4js Practical Beginner Guide

Logging is a very important part for online projects. log4js is a log component that is used more frequently and is often used with Express. This article starts with the introduction example and explains the use of log4js and how to integrate it with Express.

Beginner examples

The output log is as follows, including log printing time, log level, log classification, and log content.

// 
var log4js = require('log4js');
var logger = ();
('hello world');

// Output:// [2017-02-28 21:28:22.853] [DEBUG] [default] - hello world

Log Level

('INFO'); indicates that the lowest level log to be printed is INFO, that is, if an interface similar to () level is lower than INFO, the log will not be printed.

var log4js = require('log4js');
var logger = ();
('INFO');

('level: debug');
('level: info');
('level: error');

// The output is as follows:// [2017-02-28 21:50:45.372] [INFO] [default] - level: info
// [2017-02-28 21:50:45.376] [ERROR] [default] - level: error

Log Category

In addition to levels, logs can also be classified, (category) as shown below

var log4js = require('log4js');
var alogger = ('category-a');
var blogger = ('category-b');

('hello');
('hello');

// The output is as follows:// [2017-02-28 22:36:57.570] [INFO] category-a - hello
// [2017-02-28 22:36:57.574] [INFO] category-b - hello

appenders

Appenders specifies the location of log output. Multiple can be configured at the same time and distinguished by category. For example ('info') applies the configuration with type dateFile.

It can be noted that the type of console configuration does not declare category , so all logs are printed to the console.

var log4js = require('log4js');

({
  appenders: [
    { type: 'console'},
    { type: 'dateFile', filename: './logs/', category: 'info' }
  ]
});

var logger = ('info');
('INFO');

('trace');
('debug');
('info');

// The output is as follows:// [2017-02-28 22:51:30.723] [INFO] info - info

Express Application

A relatively simple example is as follows: all logs are printed to the console.

var express = require('express');
var log4js = require('log4js');
var app = express();

({
  appenders: [
    { type: 'console', category: 'app' }
  ]
});

var logger = ('app');

('INFO'); // Level > INFO logs will be printed
( (logger) );

(function(req, res, next){
  ('ok');
});

(3000);

Visit http://127.0.0.1:3000 and print the log as follows

[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/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"

When (logger), the level of the log can be declared.

// Level > INFO logs will be printed('INFO'); 

// The log level is WARN( (logger, {level: 'WARN'}) );

Note that if the declared log level is below the level specified by (level), the log will not be printed, as shown in the following example.

('INFO'); 

( (logger, {level: 'DEBUG'}) );

Related links

Official website:/nomiddlename/log4js-node

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.