【问题标题】:Winston: Attempt to write logs with no transportsWinston:尝试在没有传输的情况下写入日志
【发布时间】:2018-08-22 14:50:15
【问题描述】:

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

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

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"}

任何帮助将不胜感激,这几天我一直很困惑。

【问题讨论】:

    标签: node.js express npm winston


    【解决方案1】:

    我会尝试这样的事情,将所有与记录器相关的东西放入模块 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');
    

    您应该会看到如下日志行:

    错误日志:

    {"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"}
    

    编辑

    在 Winston 版本 3.x.x 上,new (winston.Logger) 已替换为 winston.createLogger (https://github.com/bithavoc/express-winston/issues/175)

    【讨论】:

    • 成功了,谢谢!这实际上与我最初的接近,但我不断收到错误。不知道有什么不同,但现在可以了。
    • 太好了,通常是一两行代码,但找到问题仍然很痛苦!
    • 注意:即使没有从模块中导出记录器,这仍然可以工作 - 代码的不同之处在于您的 logger.js 模块现在正在 index.js 中加载/执行,这将触发winston.loggers.add 调用,从而使记录器可供winston.loggers.get使用
    • 我不认为这是正确的答案,因为它有效。这避免了使用包的 loggers.addlogggers.get 方法——这是 OP 的原始问题;相反,您正在创建记录器的实例并从模块中导出它们。
    【解决方案2】:

    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..");
    

    【讨论】:

      【解决方案3】:

      由于您在原始代码中将winston 变量创建为const winston,因此无法在以下行中添加传输和其他选项。建议的解决方案将类似的代码移动到一个模块中,但将winston 变量分配给var winston

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 2021-04-26
        • 2018-03-21
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        • 2019-05-20
        • 2011-12-15
        • 1970-01-01
        相关资源
        最近更新 更多