LOG4JS Log Management

Source: Internet
Author: User

Project to use LOG4JS log, the online search for tutorials are long ago, the new version Log4js with the old version of some different, see the document in conjunction with the old version of the configuration of the LOG4JS can be used, record, The current LOG4JS version 2.3.12, in the way of day shards, output to both the console and the file.

LOG4JS is configured separately to one file, and the other to use references to this file.

Log-config.js:

Const LOG4JS = require (' Log4js ') log4js.configure ({replaceconsole:true,appenders: {stdout: {//console Output Type: ' stdout '}, Req: {//Request log type: ' Datefile ', filename: ' logs/reqlog/', pattern: ' Req-yyyy-mm-dd.log ', Alwaysincludepattern:true},err : {//error log type: ' Datefile ', filename: ' logs/errlog/', pattern: ' Err-yyyy-mm-dd.log ', Alwaysincludepattern:true},oth: {/ /other log type: ' Datefile ', filename: ' logs/othlog/', pattern: ' Oth-yyyy-mm-dd.log ', Alwaysincludepattern:true}}, Categories: {default: {appenders: [' stdout ', ' req '], Level: ' Debug '},//appenders: Appender used, take appenders, Level: Set levels err: {appenders: [' stdout ', ' err '], Level: ' Error '},oth: {appenders: [' stdout ', ' oth '] and level: ' Info '}}) EXPORTS.G Etlogger = function (name) {//name takes categories item return Log4js.getlogger (name | | ' Default ')}exports.uselogger = function (app, logger) {//used to combine with Express App.use (Log4js.connectlogger (Logger | | Log4js.getlogger (' default '), {format: ' [: Remote-addr:method:url:status:response-timems][:referrer http/: http-version:user-Agent]//Custom output format})} 

  

Use:

Const log4js= require ('./log-config ') Const logger = Log4js.getlogger ()//get loggerconst Errlogger = Log4js.getlogger as needed (' err ') const Othlogger = Log4js.getlogger (' oth ')//Combined express use, logging request log app = Express () Log4js.uselogger (App,logger)// This will automatically record each request information, placed on the other use//manual recording, can replace Console.loglogger.info (' Test info 1 ') errlogger.err (' Test error 1 ') Othlogger.info (' Test info 2 ')

The new version of the category is no longer included in the appenders, but placed in the same class as appenders categories inside, appenders to have a name, to specify categories each Appender, The log level is changed to be set in each item of categories.

LOG4JS Log Management

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.