In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use log4js in Express. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Getting started example
The output log is as follows, including log print time, log level, log classification, log content.
/ / started.jsvar log4js = require ('log4js'); var logger = log4js.getLogger (); logger.debug (' hello world'); / / output: / / [2017-02-28 21 purge 28 purl 22.853] [DEBUG] [default]-hello world
Log level
Logger.setLevel ('INFO'); indicates that the lowest level of log you want to print is INFO, that is, when you call an interface with a level lower than INFO, such as logger.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'); / / the output is as follows: / / [2017-02-28 21-21 50 level level 45.372] [INFO] [default]-level: info// [2017-02-28 21 21 log4js' 50 log4js'] [ERROR] [default]-level error:
Log category
In addition to the level, the logs can also be classified, log4js.getLogger (category), as follows
Var log4js = require ('log4js'); var alogger = log4js.getLogger (' category-a'); var blogger = log4js.getLogger ('category-b'); alogger.info (' hello'); blogger.info ('hello'); / / the output is as follows: / / [2017-02-28 22 log4js' 36RV 57.570] [INFO] category-a-hello// [2017-02-28 2236 Rod 57.574] [INFO] category-b-hello
Appenders
Appenders specifies the location of log output, which can be configured at the same time and distinguished by category. For example, log4js.getLogger ('info') applies the configuration of type for dateFile.
Notice that the configuration where type is console does not declare 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 output is as follows: / / [2017-02-28 22 info 51 purl 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 (); log4js.configure ({appenders: [{type: 'console', category:' app'}]}); var logger = log4js.getLogger ('app'); logger.setLevel (' INFO'); / / logs with level > INFO will be printed app.use (log4js.connectLogger (logger)) App.use (function (req, res, next) {res.send ('ok');}); app.listen (3000)
Visit http://127.0.0.1:3000 and print the log as follows
[2017-03-01 00 HTTP/1.1 28 GET 29.301] [INFO] app -:: ffff:127.0.0.1-- "GET / HTTP/1.1" 304-"" Mozilla/5.0 (Macintosh; Intel Mac OS X 1011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 "
When log4js.connectLogger (logger), you can declare the level of the log.
/ / only logs with level > INFO will be printed with logger.setLevel ('INFO'); / / the log level is WARN app.use (log4js.connectLogger (logger, {level:' WARN'}))
Note that if the declared log level is lower than the level limited 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'})); this is the end of the article on "how to use log4js in Express". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.