Nodejs log management
When developing a project, you can obtain the running information of the project through console output or debug. When the project goes online, we need to analyze it through logs. Like Java log4j, nodejs also has related log4js. Those who have used log4j should be familiar with this. 1. Log Level: log4js has six log levels: trace, debug, info, warn, error, and fatal. The initialization code of the weight is TRACE: new Level (5000, "TRACE"), DEBUG: new Level (10000, "DEBUG"), INFO: new Level (20000, "INFO"), WARN: new Level (30000, "WARN"), ERROR: new Level (40000, "ERROR"), FATAL: new Level (50000, "FATAL"). If the default log Level is info, logs with a weight less than info will not be recorded. That is to say, only log.info () and log are called. warn (), log. error () or log. fatal () triggers logging. This part of the code is in lib/logger. js. Copy the code Logger. prototype. log = function () {var args = Array. prototype. slice. call (arguments), logLevel = levels. toLevel (args. shift (), loggingEvent; if (this. isLevelEnabled (logLevel) {loggingEvent = new LoggingEvent (this. category, logLevel, args, this); this. emit ("log", loggingEvent) ;}; copy code 2. Integration of expresslog4js can be used as a middleware of express. First, introduce log4js var express = require ("express"); var log4js = require ("log4js"); var app = express (); then configure log4js log4js. configure ({appenders: [{type: 'console'}, {type: 'filename: 'cheese. log', category: 'cheese '}]}); this configuration means that the console is the default appender. When cheese is used, it will record the log file, the log file name is cheese. log. Connect to the middleware with use. By default, we use the cheese appender with the info level. App. use (log4js. connectLogger (log4js. getLogger ("cheese"), {level: log4js. levels. INFO}); the output is similar to this: [20:27:21. 205] [INFO] cheese-127.0.0.1-"GET/HTTP/1.1" 200 22896 "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) chrome/31.0.1650.63 Safari/537.36 "...... 3. You can modify the middleware above, but when there are too many middleware, it may feel ugly to write them in the same file. So I tend to separate it as a separate module, that is, a separate file. Then expose the interface. Copy the code var path = require ("path"); var log4js = require ("log4js");/*** log configuration */exports. configure = function () {log4js. configure (path. join (_ dirname, "log4js. json ");}/*** expose to the application's log interface. Before calling this method, make sure that you have configured * @ param name to specify the category in the log4js configuration file. Find the corresponding appender. * If the appender does not write category, it is the default category. There can be multiple * @ returns {Logger} */exports. logger = function (name) {var dateFileLog = log4js. getLogger (name); dateFileLog. setLevel (log4js. levels. INFO); return dateFileLog;}/*** used in express middleware. Before calling this method, make sure that the * @ returns {Function | *} */exports has been configure. useLog = function () {return log4js. connectLogger (log4js. getLogger ("app"), {level: log4js. levels. INFO});} copy the code log4js. the json file contains the following copy code {"appenders": [{"type ": "Console" },{ "type": "dateFile", "filename": "logs/booklist. log "," pattern ":"-yyyy-MM-dd "," alwaysIncludePattern ": true}]} The Code copy configuration is very simple. Two appender and one on the console are configured, one is dateFile, which means a log file is generated every day. Note that category is not configured here. In this case, when the corresponding appender is not found, the two appender are the default appender. Sometimes it is clear that the configuration is correct, but the log file does not generate logs, and the problem is often found here. Then in the app. in js, we modify it to the following copy code var express = require ("express"); // This is the custom module var log4js = require (". /log "); var app = express (); app. configure (); app. use (log4js. useLog ());... copy code 4, single process and multi-process are good, the above is applicable to single process, but if your nodejs application is multi-process, using the above configuration, you will see that the log output is a bit strange, for example: It feels like a resource is preemptible. The multiprocess configuration is provided in the wiki of log4js. However, there were also problems when using it at that time, but I did not go into details at that time. However, someone in the Community has set up another method, which I use. Refer to this issue. Next we will configure it. Modify the log Module file we modified above to: copy the code var path = require ("path"); var log4js = require ("log4js "); /*** multi-process log configuration */exports. configure = function (mode) {if (mode = "master") {log4js. configure (path. join (_ dirname ,". /log4js-master.json ");} else {// multi-process configuration item log4js. configure (path. join (_ dirname ,". /log4js-worker.json "); // configuration of a single process // log4js. configure (path. join (_ dirname ,".. /config/log4js. json ");}/*** exposed To the Application Log interface, make sure that you have configured * @ param name to specify the category in the log4js configuration file before calling this method. Find the corresponding appender. * If the appender does not write category, it is the default category. There can be multiple * @ returns {Logger} */exports. logger = function (name) {var dateFileLog = log4js. getLogger (name); dateFileLog. setLevel (log4js. levels. INFO); return dateFileLog;}/*** used in express middleware. Before calling this method, make sure that the * @ returns {Function | *} */exports has been configure. useLog = function () {return log4js. connectLogger (log4js. getLogger ("app"), {level: log4js. levels. INFO}) ;}copy the code to modify the configure method. The log4js-master.json content is: Copy code {"appenders": [{"type": "clustered", "appenders": [{"type": "console "}, {"type": "dateFile", "filename": "logs/booklist. log "," pattern ":"-yyyy-MM-dd "," alwaysIncludePattern ": true," pollInterval ": 1," category ": "dateFileLog"}]} the content of the copied code log4js-worker.js is: {"appenders": [{"type ": "clustered"}]} assume that the content of the master process is in the file master. js, Working Process in worker. js. Master. the configuration content in js is: var log4js = require (". /lib/log "); log4js. configure ("master"); worker. the js configuration content is: copy the code var express = require ("express"); // This is the custom module var log4js = require (". /log "); var app = express (); app. configure ("worker"); app. use (log4js. useLog ());... copy the code. When we need to record the log somewhere, we can do this var log = require (". /log "). logger ("index"); // The parameters in logger can be set as needed... log.info ("... "); log. error ("... ")