温斯顿:尝试在没有传输的情况下写入日志

Winston: Attempt to write logs with no transports

我正在尝试使用 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"}

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

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

编辑

On Winston version 3.x.x, new (winston.Logger) has been replaced by winston.createLogger (https://github.com/bithavoc/express-winston/issues/175)

1 个记录器 + 用于开发目的的控制台记录:

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