Home Web Front-end JS Tutorial About the actual usage of log4js in Express

About the actual usage of log4js in Express

Jun 06, 2018 am 11:27 AM
express

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

In-depth comparison of Express and Laravel: How to choose the best framework? In-depth comparison of Express and Laravel: How to choose the best framework? Mar 09, 2024 pm 01:33 PM

In-depth comparison of Express and Laravel: How to choose the best framework? When choosing a back-end framework suitable for your project, Express and Laravel are undoubtedly two popular choices among developers. Express is a lightweight framework based on Node.js, while Laravel is a popular framework based on PHP. This article will provide an in-depth comparison of the advantages and disadvantages of these two frameworks and provide specific code examples to help developers choose the framework that best suits their needs. Performance and scalabilityExpr

Comparative analysis of Express and Laravel: Choose the framework that suits you better Comparative analysis of Express and Laravel: Choose the framework that suits you better Mar 10, 2024 pm 10:15 PM

Express and Laravel are two very popular web frameworks, representing the excellent frameworks of the two major development languages ​​of JavaScript and PHP respectively. This article will conduct a comparative analysis of these two frameworks to help developers choose a framework that is more suitable for their project needs. 1. Framework Introduction Express is a web application framework based on the Node.js platform. It provides a series of powerful functions and tools that enable developers to quickly build high-performance web applications. Express

Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? Mar 10, 2024 am 08:39 AM

Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? In the field of web development, Express and Laravel are two frameworks that have attracted much attention. Express is a flexible and lightweight web application framework based on Node.js, while Laravel is an elegant and feature-rich web development framework based on PHP. This article will compare the advantages and disadvantages of Express and Laravel in terms of functionality, ease of use, scalability, and community support, and combine

Let's talk about how node+express operates cookies Let's talk about how node+express operates cookies Jun 22, 2022 am 10:01 AM

How does node+express operate cookies? The following article will introduce to you how to use node to operate cookies. I hope it will be helpful to you!

Which is better express or laravel? Which is better express or laravel? Jul 05, 2023 pm 01:16 PM

Laravel is good for the following reasons: 1. Laravel uses PHP language, has elegant and intuitive syntax, and provides a large number of functions and tools; 2. Laravel has a variety of extension packages, plug-ins and solutions; 3. Laravel has very complete and easy-to-understand official documentation; 4. Laravel provides good support for data security and user authentication; 5. Laravel can effectively separate the business logic and display layer; 6. Laravel has a large and active community. Have access to the latest technology.

How to build a full-stack JavaScript application using React and Express How to build a full-stack JavaScript application using React and Express Sep 26, 2023 pm 01:09 PM

How to use React and Express to build a full-stack JavaScript application Introduction: React and Express are currently very popular JavaScript frameworks. They are used to build front-end and back-end applications respectively. This article will introduce how to use React and Express to build a full-stack JavaScript application. We will explain step by step how to build a simple TodoList application and provide specific code examples. 1. Preparation before starting

Express or Laravel? Choose the backend framework that works best for you Express or Laravel? Choose the backend framework that works best for you Mar 10, 2024 pm 06:06 PM

When it comes to choosing a backend framework, both Express and Laravel are very popular choices. Express is a web application development framework based on Node.js, while Laravel is a web application development framework based on PHP. Both have their own advantages, and choosing the framework that best suits you requires considering many factors. The strengths of the Express framework are its flexibility and easy learning curve. The core idea of ​​Express is "small enough and flexible enough", and it provides a large number of middleware

See all articles