【发布时间】:2018-03-11 00:06:40
【问题描述】:
我在我的应用程序中引入了winston 日志,我想用winston 的.info 和.error 替换所有信息或错误级别日志。到目前为止,一切正常,除非我尝试从 app.listen 回调中记录信息消息。
appStartup.js 定义记录器的文件
const app = require('express')();
const co = require('co');
const cors = require('cors');
const winston = require('winston');
const loggerTimeFormat = () => (new Date()).toLocaleTimeString();
const winstonLogger = new winston.Logger({
transports: [
new winston.transports.Console({
timestamp: loggerTimeFormat,
}),
],
});
module.exports = function appStartup() {
co(function* services() {
app.db = mongo.connect();
app.use(cors());
app.set('logger', winstonLogger);
}).catch((e) => {
console.error(e);
});
return app;
};
在我的server.js 文件中,我有:
const app = require('./src/appStartup');
const appPort = process.env.PORT;
app().listen(appPort, () => {
console.log('Info message here');
});
我想用winston info 日志替换server.js 中的console.log 我尝试这样做:
const app = require('./src/appStartup');
const appPort = process.env.PORT;
const appObj = app();
appObj.listen(appPort, (appObj.get('logger')) => {
logger.info('Info message here');
});
不确定这是否是正确的语法,我在appObj.get('logger') 中得到Parsing error: Assigned to rvalue for appObj。知道我该如何完成这项工作吗?
【问题讨论】:
-
记录器调用似乎有错字。你没有 logger.info,只有 logger。
-
@Sri 是的,谢谢,这是一个错字,虽然这并没有解决问题。
标签: javascript node.js express