我正在将Sails(@ 1.2.2)与NodeJs(@ 10.15)一起使用。我已经使用Winston(@ 3.2.1)实现了logger,并遇到以下错误:
我在config / log.js中添加了以下代码
const winston = require('winston')
module.exports = {
'log': {
level:'info',
filePath:'sails.log',
'colors': false,
'custom': new (winston.Logger)({
'transports': [
new (winston.transports.Console)({
'level': 'info',
'colorize': true,
'timestamp': false,
'json': false
}),
new winston.transports.File({
'level': 'debug',
'colorize': false,
'timestamp': true,
'json': true,
'filename': './sails.log',
'maxsize': 5120000,
'maxFiles': 3
})
]
})
}
};
当我在控制台中运行风帆举升时,我在控制台中看到以下错误:
error: A hook (`userconfig`) failed to load!
error:
error: Attempted to `require('/var/www/html/node-project/config/log.js')`, but an error occurred:
--
TypeError: winston.Logger is not a constructor
at Object.<anonymous> (/var/www/html/node-project/config/log.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
我搜索了上述问题,然后发现使用createLogger而不是Logger。然后,请在下面查看我的更新代码:
const winston = require('winston')
module.exports = {
'log': {
level:'info',
filePath:'sails.log',
'colors': false,
'custom': new (winston.createLogger)({
'transports': [
new (winston.transports.Console)({
'level': 'info',
'colorize': true,
'timestamp': false,
'json': false
}),
new winston.transports.File({
'level': 'debug',
'colorize': false,
'timestamp': true,
'json': true,
'filename': './sails.log',
'maxsize': 5120000,
'maxFiles': 3
})
]
})
}
};
然后我在控制台上发现了新错误:
/var/www/html/node-project/node_modules/winston/lib/winston/create-logger.js:80
self._addDefaultMeta(info);
^
TypeError: self._addDefaultMeta is not a function
at Function.DerivedLogger.(anonymous function) (/var/www/html/node-project/node_modules/winston/lib/winston/create-logger.js:80:14)
at Sails._writeLogToConsole [as log] (/var/www/html/node-project/node_modules/captains-log/lib/write.js:90:18)
at whenSailsIsReady (/var/www/html/node-project/node_modules/sails/lib/app/lift.js:99:13)
at /var/www/html/node-project/node_modules/async/dist/async.js:3861:9
at /var/www/html/node-project/node_modules/async/dist/async.js:421:16
我想在error.log文件中插入错误。另外,如果有人知道更好的实现方法。请分享。
预先感谢您的帮助。
答案 0 :(得分:0)
使用这个:
const logger = winston.createLogger({ transports })
或切换到2个版本:
npm uninstall winston
npm install winston@2.4.3