Node.js Logging

前端 未结 9 1837
一个人的身影
一个人的身影 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 18:00

    Winston is a pretty good logging library. You can write logs out to a file using it.

    Code would look something like:

    var winston = require('winston');
    
    var logger = new (winston.Logger)({
      transports: [
        new (winston.transports.Console)({ json: false, timestamp: true }),
        new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
      ],
      exceptionHandlers: [
        new (winston.transports.Console)({ json: false, timestamp: true }),
        new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
      ],
      exitOnError: false
    });
    
    module.exports = logger;
    

    You can then use this like:

    var logger = require('./log');
    
    logger.info('log to file');
    

提交回复
热议问题