Home > Web Front-end > JS Tutorial > body text

About the actual usage of log4js in Express

亚连
Release: 2018-06-06 11:27:07
Original
3240 people have browsed it

This article mainly introduces the practical introductory guide to advanced log4js in Express. Now I share it with you and give it as a reference.

For online projects, logs are a very important part. Log4js is a frequently used logging component and is often used together with Express. This article starts with an introductory example to explain the use of log4js and how to integrate it with Express.

Getting Started Example

The output log is as follows, including log printing time, log level, log classification, 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 world
Copy after login

Log level

logger.setLevel('INFO'); means that the lowest level log you want to print is INFO, that is, call something like logger. For interfaces with levels lower than INFO such as debug(), the log will not be printed.

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: error
Copy after login

Log Category

In addition to levels, logs can also be classified, 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 - hello
Copy after login

appenders

appenders specifies the location of the log output. You can configure multiple ones at the same time and use category to distinguish them. For example, log4js.getLogger('info') applies the configuration whose type is dateFile.

It can be noticed that the configuration with type console does not declare category , therefore, all logs will be 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');

// 输出如下:
// [2017-02-28 22:51:30.723] [INFO] info - info
Copy after login

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();

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

var logger = log4js.getLogger('app');

logger.setLevel('INFO'); // 级别 > INFO 的日志才会被打印

app.use( log4js.connectLogger(logger) );

app.use(function(req, res, next){
  res.send('ok');
});

app.listen(3000);
Copy after login

Access http://127.0.0.1:3000, the print log is 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 #log4js.connectLogger(logger), you can declare the log level.

// 级别 > INFO 的日志才会被打印
logger.setLevel('INFO'); 

// 日志的级别是 WARN 
app.use( log4js.connectLogger(logger, {level: 'WARN'}) );
Copy after login

Note that if the declared log level is lower than the level defined by logger.setLevel(level), the log will not be printed, as in the following example.

logger.setLevel('INFO'); 

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

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to solve the problem that Vue cannot detect changes in arrays or objects?

What are the methods to add new properties of objects to the detection sequence in vue?

How to generate random numbers in JS (detailed tutorial)

The above is the detailed content of About the actual usage of log4js in Express. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!