Node.js Logging

前端 未结 9 1930
一个人的身影
一个人的身影 2021-01-29 17:28

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like ro

9条回答
  •  执笔经年
    2021-01-29 17:53

    The "logger.setLevel('ERROR');" is causing the problem. I do not understand why, but when I set it to anything other than "ALL", nothing gets printed in the file. I poked around a little bit and modified your code. It is working fine for me. I created two files.

    logger.js

    var log4js = require('log4js');
    log4js.clearAppenders()
    log4js.loadAppender('file');
    log4js.addAppender(log4js.appenders.file('test.log'), 'test');
    var logger = log4js.getLogger('test');
    logger.setLevel('ERROR');
    
    var getLogger = function() {
       return logger;
    };
    
    exports.logger = getLogger();
    

    logger.test.js

    var logger = require('./logger.js')
    
    var log = logger.logger;
    
    log.error("ERROR message");
    log.trace("TRACE message");
    

    When I run "node logger.test.js", I see only "ERROR message" in test.log file. If I change the level to "TRACE" then both lines are printed on test.log.

提交回复
热议问题