为什么Winston 3.0.0无法使用Sails 0.12?

时间:2018-03-11 00:57:02

标签: node.js sails.js winston

我在文件config/log.js

中有这些设置
let {transports: transp, createLogger} = require('winston');    
let commonSettings = {
    colorize: false,
    maxsize: 10000000,
    maxfiles: 10,
    timestamp: true
};

let settings = [{ filename: 'logs/warn.log', level: 'warn' }];    
let logger = createLogger({
    transports: settings.map(s => new transp.File({...s, ...commonSettings}))
});

module.exports.log = {
    custom: logger,
    inspect: false
};

当我尝试使用此命令sails lift启动Sails服务器时,我收到以下错误:

Uncaught TypeError: this.write is not a function
  at Function.DerivedLogger.(anonymous function) (node_modules/winston/lib/winston/create-logger.js:45:14)
  at Function._writeLogToConsole [as error] (node_modules/captains-log/lib/write.js:78:16)
  at Sails.runBootstrap (node_modules/sails/lib/app/private/bootstrap.js:56:17)
  at Sails.bound [as runBootstrap] (node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
  at Sails.initialize (node_modules/sails/lib/app/private/initialize.js:48:9)
  at bound (node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:607:21
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:246:17
  at iterate (node_modules/sails/node_modules/async/lib/async.js:146:13)
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:157:25
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:248:21
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:612:34
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/lib/app/load.js:201:13
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:451:17
  at /Users/egomezr/Documents/stack/backend/node_modules/sails/node_modules/async/lib/async.js:441:17
  at _each (node_modules/sails/node_modules/async/lib/async.js:46:13)
  at Immediate.taskComplete (node_modules/sails/node_modules/async/lib/async.js:440:13)
  at process.topLevelDomainCallback (domain.js:101:23)

可能是什么问题?

2 个答案:

答案 0 :(得分:2)

看起来Sails对Winston记录器进行了一种绑定,因此记录器失去了一些功能。

未解决的问题: issues/4337

我找到了解决方法,让它发挥作用:

let commonSettings = {
    colorize: false,
    maxsize: 10000000,
    maxfiles: 10,
    json: false
};

let settings = [
    {filename: `${__dirname}/../logs/warn.log`, level: 'warn'},
    {filename: `${__dirname}/../logs/error.log`, level: 'error'},
    {filename: `${__dirname}/../logs/debug.log`, level: 'debug'},
    {filename: `${__dirname}/../logs/info.log`, level: 'info'}
];

let {transports, createLogger, format} = require('winston');
let loggerSettings = {
    transports: [...settings.map(s => new transports.File({...s, ...commonSettings})), new transports.Console({
        format: format.simple()
    })],
    exitOnError: false
};

// This is the workaround
let winstonLogger = createLogger(loggerSettings);
let logger = {
    'info': function () {
        winstonLogger.info(...arguments);
    },
    'debug': function () {
        winstonLogger.debug(...arguments);
    },
    'error': function () {
        winstonLogger.error(...arguments);
    },
    'warn': function () {
        winstonLogger.warn(...arguments);
    },
    'log': function () {
        winstonLogger.log(...arguments);
    }
};
// End of workaround

module.exports.log = {
    custom: logger,
    inspect: false
};

答案 1 :(得分:1)

对于它的价值,看起来他在Sails问题中被报道但在https://github.com/balderdashy/sails/issues/4209无意中被关闭了。