Node Log log4js error logging

Source: Internet
Author: User

SET debug=mylog:* & NPM  start
Source: HTTP://BLOG.FENS.ME/NODEJS-LOG4JS/1. Default console Output

When we use the Express framework, when the development mode launches the Nodejs app with node or supervisor, the console displays the following logs.

GET /css/bootstrap.min.css 304 1msGET /css/my.css 304 0msGET /js/bootstrap.min.js 304 4msGET /js/jquery-1.9.1.min.js 304 6msGET /js/holder.js 304 3msGET /cat/json/latest 200 6msGET /cat/json/master 200 4msGET /cat/json/classic 200 2msGET /about 200 6msGET /css/bootstrap.min.css 304 2msGET /css/my.css 304 2msGET /js/bootstrap.min.js 304 2msGET /js/jquery-1.9.1.min.js 304 1msGET /js/holder.js 304 1msGET /js/bootstrap.min.js 304 1msGET / 304 6msGET /js/jquery-1.9.1.min.js 304 2msGET /css/my.css 304 1msGET /css/bootstrap.min.css 304 1msGET /js/bootstrap.min.js 304 2msGET /js/holder.js 304 2msGET /cat/json/latest 200 3msGET /cat/json/master 200 2msGET /cat/json/classic 200 2msGET /admin/ 304 13msGET /css/bootstrap.min.css 304 3msGET /js/jquery-1.9.1.min.js 304 2msGET /css/my.css 304 2msGET /js/bootstrap.min.js 304 1msGET /js/holder.js 304 2ms

We can also print some console logs in the code, using Console.log ().

Modify Routes/index.js

exports.index = function(req, res){console.log("This is an index page!");res.render(‘index‘, {  title:‘首页|moive.me‘,  page:‘index‘  });};

To access the page, the results are as follows.

This is an index page!GET / 304 19msGET /css/bootstrap.min.css 304 4msGET /css/my.css 304 2msGET /js/jquery-1.9.1.min.js 304 38msGET /js/holder.js 304 29msGET /js/bootstrap.min.js 304 28ms

The result of this output is displayed on the console, and once the server restarts the log is lost. For program development, this output is sufficient. But in a production environment, we want to be able to save the console output to a file, and need more information, not just the default simplified log information.

Since the express framework does not have a log function, we need to introduce the LOG4JS package to complete this function.

2. Output logs via Log4js

Let's take a look at what the log output through the Log4js looks like, and the next section describes the specific configuration.

This is an index page! get/304 17ms[2013-06-19 17:45:55.981] [INFO] normal-127.0.0.1--"get/http/1.1" 304-"http://localhost:3000/admin/ "" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/css/bootstrap.min.css 304 10ms[ 2013-06-19 17:45:56.015] [INFO] normal-127.0.0.1--"Get/css/bootstrap.min.css http/1.1" 304-"http://localhost:3000/ Admin/crawler/youku "" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/css/my.css 304 8ms[2013-06-19 17:45:56.017] [INFO] normal-127.0.0.1--"Get/css/my.css http/1.1" 304-"Http://localhost:3000/admin/crawler/youku" " mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/jquery-1.9.1.min.js 304 19ms[ 2013-06-19 17:45:56.031] [INFO] normal-127.0.0.1--"Get/js/jquery-1.9.1.min.js http/1.1" 304-"http://localhost:3000 /admin/"" mozilla/5.0 (WindOWS NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/bootstrap.min.js 304 13ms[ 2013-06-19 17:45:56.037] [INFO] normal-127.0.0.1--"Get/js/bootstrap.min.js http/1.1" 304-"http://localhost:3000/ad Min/"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/holder.js 304 20ms[2013-06-19  17:45:56.040] [INFO] normal-127.0.0.1--"Get/js/holder.js http/1.1" 304-"http://localhost:3000/admin/" "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "

The same request, the console outputs a lot more results, the full Web server log format.
This is what the production environment needs! The information is richer and is in the same format as Nginx and Apache.

3. Configure LOG4JS and express Framework Integration

Download LOG4JS Package

~ npm install log4js[email protected] node_modules\log4js├── [email protected]├── [email protected]├── [email protected]└── [email protected]

Modify App.js

~ vi app.jsvar log4js = require(‘log4js‘);log4js.configure({  appenders: [    { type: ‘console‘ }, //控制台输出    {      type: ‘file‘, //文件输出      filename: ‘logs/access.log‘,       maxLogSize: 1024,      backups:3,      category: ‘normal‘     }  ]});var logger = log4js.getLogger(‘normal‘);logger.setLevel(‘INFO‘);...//app.use(...)//app.use(...)app.use(log4js.connectLogger(logger, {level:log4js.levels.INFO}));app.use(app.router);

I need to configure the LOG4JS in App.js.

The appenders is configured with two outputs, one for the console output and one for the file output.

Appenders.type=file object that specifies the file output location and file size, and automatically generates a new file when the Maxlogsize size is exceeded. Logs the file directory to be created manually.
Level:log4js.levels.INFO, setting the default log output level is INFO.

Log4js Output Level 6 : Trace, Debug, info, warn, error, fatal

    • Logger.trace (' Entering cheese testing ');
    • Logger.debug (' Got cheese. ');
    • Logger.info (' Cheese is Gouda. ');
    • Logger.warn (' Cheese is quite smelly. ');
    • Logger.error (' Cheese is too ripe! ');
    • Logger.fatal (' Cheese was breeding ground for listeria. ');

If the output level is info, the log trace,debug below the info level is not printed and only info,warn,error,fatal is printed. The advantage of this is that in a production environment we may only be concerned with exceptions and errors, and do not care about debugging information. This greatly reduces the output of the log and can reduce disk writes. In the development environment, we can print very much information to help developers locate errors and debug code.

Another benefit is that the code can be mixed with a variety of log printing code. As long as we modify the output level in a single configuration file, the log output changes, without modifying all of the code. If all the places are Console.log (), then on-line, it takes a lot of time to change this thing.

4. According to the project configuration Log4js

In the previous section, LOG4JS and express integration were described. However, the default configuration may not be appropriate for our project, and some adjustments to the parameters of the log4js are required.

1. Replace Console.log ()
Increase the Replaceconsole configuration so that all console outputs are in the log and the [INFO] console replaces the console default style.

~ vi app.jsvar log4js = require(‘log4js‘);log4js.configure({  appenders: [    { type: ‘console‘ },{      type: ‘file‘,       filename: ‘logs/access.log‘,       maxLogSize: 1024,      backups:4,      category: ‘normal‘     }  ],  replaceConsole: true});

To view the results of the output:

[2013-06-19 18:18:41.997] [INFO] Console-this is an index page! get/304 15ms[2013-06-19 18:18:42.010] [INFO] normal-127.0.0.1--"get/http/1.1" 304-"http://localhost:3000/admin/ "" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/css/bootstrap.min.css 304 5ms[ 2013-06-19 18:18:42.042] [INFO] normal-127.0.0.1--"Get/css/bootstrap.min.css http/1.1" 304-"http://localhost:3000/ Admin/crawler/youku "" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/css/my.css 304 11ms[2013-06-19 18:18:42.051] [INFO] normal-127.0.0.1--"Get/css/my.css http/1.1" 304-"Http://localhost:3000/admin/crawler/youku" " mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/jquery-1.9.1.min.js 304 35ms[ 2013-06-19 18:18:42.089] [INFO] normal-127.0.0.1--"Get/js/jquery-1.9.1.min.js http/1.1" 304-"HTTP:Localhost:3000/admin/"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/holder.js 304 42ms[2013-06-19  18:18:42.098] [INFO] normal-127.0.0.1--"Get/js/holder.js http/1.1" 304-"http://localhost:3000/admin/" "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "Get/js/bootstrap.min.js 304 11ms[ 2013-06-19 18:18:42.101] [INFO] normal-127.0.0.1--"Get/js/bootstrap.min.js http/1.1" 304-"http://localhost:3000/ad Min/"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.110 safari/537.36 "

2. Adjust the format of the log output

 app.use(log4js.connectLogger(logger, {level: level:log4js.levels.INFO, format:‘:method :url‘}));

Output Result:

[2013-06-19 18:23:25.230] [INFO] console - This is an index page!GET / 304 28ms[2013-06-19 18:23:25.251] [INFO] normal - GET /GET /css/bootstrap.min.css 304 5ms[2013-06-19 18:23:25.287] [INFO] normal - GET /css/bootstrap.min.cssGET /css/my.css 304 3ms[2013-06-19 18:23:25.292] [INFO] normal - GET /css/my.cssGET /js/jquery-1.9.1.min.js 304 15ms[2013-06-19 18:23:25.310] [INFO] normal - GET /js/jquery-1.9.1.min.jsGET /js/holder.js 304 9ms[2013-06-19 18:23:25.321] [INFO] normal - GET /js/holder.jsGET /js/bootstrap.min.js 304 17ms[2013-06-19 18:23:25.338] [INFO] normal - GET /js/bootstrap.min.js

3. Automatically adjust the log output level

Log level corresponding rules:
HTTP responses 3xx, level = WARN
HTTP Responses 4xx & 5xx, level = ERROR
else, level = INFO

Set level to Auto

 app.use(log4js.connectLogger(logger, {level: ‘auto‘, format:‘:method :url‘}));

The following log in order to compare the convenience I played a few more lines.

 [2013-06-19 18:24:56.040] [INFO] console-this is an index page! get/304 16ms[2013-06-19 18:24:56.053] [WARN] normal-get/get/css/bootstrap.min.css 304 9ms[2013-06-19 18:24:56.086] [ WARN] normal-get/css/bootstrap.min.cssget/css/my.css 304 9ms[2013-06-19 18:24:56.097] [WARN] NORMAL-GET/CSS/MY.CSSG Et/js/jquery-1.9.1.min.js 304 26ms[2013-06-19 18:24:56.128] [WARN] normal-get/js/jquery-1.9.1.min.jsget/js/ Holder.js 304 32ms[2013-06-19 18:24:56.164] [WARN] normal-get/js/holder.jsget/js/bootstrap.min.js 304 1ms[2013-06-19 1 8:24:56.166] [WARN] normal-get/js/bootstrap.min.js[2013-06-19 18:24:56.204] [INFO] Normal-get/cat/json/latestget/ca T/json/latest 10ms[2013-06-19 18:24:56.211] [INFO] Normal-get/cat/json/masterget/cat/json/master 4ms[2013-06- 18:24:56.219] [INFO] normal-get/cat/json/classicget/cat/json/classic 9msget/img/movie/emptysmall.png 304 1ms[2 013-06-19 18:24:56.263] [WARN] normal-get/img/movie/emptysmall.png 
5. Optimize LOG4JS structure

There should be a classmate found that we have a problem when configuring LOG4JS. Is that all configuration information is done in app.js, and logger is defined directly here. If the controller (routes) wants to output with log4js, we can't get the logger handle now.

Modify App.js,

~ vi app.jsvar log4js = require(‘log4js‘);log4js.configure({  appenders: [    { type: ‘console‘ },{      type: ‘file‘,       filename: ‘logs/access.log‘,       maxLogSize: 1024,      backups:4,      category: ‘normal‘     }  ],  replaceConsole: true});//var logger = log4js.getLogger(name);//logger.setLevel(‘INFO‘);....app.use(log4js.connectLogger(this.logger(‘normal‘), {level:‘auto‘, format:‘:method :url‘}));....exports.logger=function(name){  var logger = log4js.getLogger(name);  logger.setLevel(‘INFO‘);  return logger;}

We define the logger separately and expose it as an API.

Using the logger output in Index.js

~ vi routes/index.jsvar logger = require(‘../app‘).logger(‘index‘);exports.index = function(req, res){console.log("This is an index page!");logger.info("This is an index page! -- log4js");res.render(‘index‘, {  title:‘首页|moive.me‘,  page:‘index‘  });};

Print out results

[2013-06-19 18:56:51.924] [INFO] console - This is an index page![2013-06-19 18:56:51.925] [INFO] index - This is an index page! -- log4jsGET / 304 17ms[2013-06-19 18:56:51.938] [WARN] [default] - GET /GET /css/bootstrap.min.css 304 5ms[2013-06-19 18:56:51.978] [WARN] [default] - GET /css/bootstrap.min.cssGET /css/my.css 304 2ms[2013-06-19 18:56:51.981] [WARN] [default] - GET /css/my.cssGET /js/jquery-1.9.1.min.js 304 2ms[2013-06-19 18:56:51.984] [WARN] [default] - GET /js/jquery-1.9.1.min.jsGET /js/holder.js 304 3ms[2013-06-19 18:56:51.989] [WARN] [default] - GET /js/holder.jsGET /js/bootstrap.min.js 304 9ms[2013-06-19 18:56:52.002] [WARN] [default] - GET /js/bootstrap.min.js

So we have been playing log4js, for the deployment to the production environment, do a good job of log preparation.

Node Log log4js error logging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.