首页 文章

Winston:尝试写没有传输的日志

提问于
浏览
8

我正在尝试使用Winston为我的快速服务器设置访问日志和错误日志,但我似乎做错了什么 .

这是我对配置文件的尝试:

const winston = require('winston'),
    fs = require('fs');

const tsFormat = () => (new Date()).toLocaleTimeString();
winston.loggers.add('errorLog', {
        file: {
                filename: '<path>/errors.log', //<path> is replaced by the 
                timestamp: tsFormat,           //absolute path of the log
                level: 'info'
        }

});
winston.loggers.add('accessLog', {
        file: {
                filename: '<path>/access.log', //same as before
                timestamp: tsFormat,
                level: 'info'
        }
});

这就是我将其包含在我的其他文件中的方式:

var winston = require('winston'),
    accessLog = winston.loggers.get('accessLog'),
    errorLog = winston.loggers.get('errorLog');

这似乎在我看来它遵循文档(https://github.com/winstonjs/winston/tree/2.4.0#working-with-multiple-loggers-in-winston)但我在尝试登录时遇到此错误:

[winston] Attempt to write logs with no transports {"message":"pls","level":"info"}
[winston] Attempt to write logs with no transports {"message":"Bad request: undefined","level":"warn"}

任何帮助将不胜感激,我已经相当困难了几天了 .

2 回答

  • 5

    我尝试这样的事情,将所有与 Logger 相关的东西放入模块logger.js:

    logger.js

    var winston = require('winston');
        var path = require('path');
    
        // Set this to whatever, by default the path of the script.
        var logPath = __dirname;
    
        const tsFormat = () => (new Date().toISOString());
    
      const errorLog = winston.createLogger({
      transports: [
        new winston.transports.File({
          filename: path.join(logPath, 'errors.log'),
          timestamp: tsFormat,
          level: 'info'})
      ]
    });
    
    const accessLog = winston.createLogger({
      transports: [
        new winston.transports.File({
          filename: path.join(logPath, 'access.log'),
          timestamp: tsFormat,
          level: 'info'})
      ]
    });
    
    
        module.exports = {
            errorLog: errorLog,
            accessLog: accessLog
        };
    

    然后在index.js中测试:

    index.js

    var logger = require('./logger');
    
    logger.errorLog.info('Test error log');
    logger.accessLog.info('Test access log');
    

    您应该看到如下日志行:

    errors.log:

    {"level":"info","message":"Test access log","timestamp":"2018-03-14T07:51:11.185Z"}
    

    access.log的:

    {"level":"info","message":"Test error log","timestamp":"2018-03-14T07:51:11.182Z"}
    

    EDIT

    在Winston上一个版本中,new(winston.Logger)已被winston.createLogger取代(https://github.com/bithavoc/express-winston/issues/175)

  • 1

    1用于开发目的的Logger控制台日志记录:

    logger.js

    var logPath = '';
    var log_level = '';
    
    const log = winston.createLogger({
      level: log_level,
      format: winston.format.json(),
      transports: [
        new winston.transports.File({
          filename: path.join(logPath, 'access.log'),
          timestamp: tsFormat,
          level: log_level
        }),
        new winston.transports.File({
          filename: path.join(logPath, 'error.log'),
          timestamp: tsFormat,
          level: 'error'
        }),
      ]
    });
    
    if (process.env.NODE_ENV !== 'production') {
      log.add(new winston.transports.Console({
        format: winston.format.simple()
      }));
    }
    
    module.exports = {
      log: log
    };
    

    app.js

    const logger = require('./logger');
    
    logger.log.info("starting application..");
    

相关问题