我已经扩展winston-express-request-logger以打印出我的Express / NodeJs应用程序中的每个请求,进程,错误和用户信息。
但是应用程序实际上没有记录有关请求的信息。
示例日志输出:
2018-02-22T11:18:44.871Z - info: root access
我认为官方教程(found here)会引导我进行实施。我错过了教程中的任何内容吗?
整个应用程序代码:
'use strict';
const express = require('express');
const app = express();
const winston = require('winston');
var winstonExRegLogger = require("winston-express-request-logger");
winstonExRegLogger.createLogger({
transports: [
new (winston.transports.Console)({
handleExceptions: true,
timestamp: true,
level:"info"
})
],
exitOnError: false
});
function configureApp(app, env) {
app.use(winstonExRegLogger.requestDetails);
}
app.use(winstonExRegLogger.requestDetails);
var logger = require("winston-express-request-logger").getLogger();
logger.error('testing error log');
logger.info('testing info log');
app.get('/', (req, res) => {
logger.info('root access');
res.status(200).send('hello world!');
});
app.get('/home', (req, res) => {
logger.info('home');
res.status(200).send('okay');
});
app.post('/p', (req, res) => {
logger.info('post ni');
logger.error('in post');
res.status(200).send('post acknowledged');
});
if (module === require.main) {
const server = app.listen(process.env.PORT || 8081, () => {
const port = server.address().port;
console.log(`App listening on port ${port}`);
});
}
module.exports = app;

答案 0 :(得分:1)
您只需将req对象传递给logger.info调用,例如做
logger.info(req, 'home');
而不是
logger.info('home');
这将给出如下日志行:
2018-02-22T14:28:21.481Z - info: pId: 25868 - uId: cjdylrofa0000yktbj5rkrtsr - cId: anonymous - cIP: ::1 - cAction: GET /home home
2018-02-22T14:31:56.014Z - info: pId: 33536 - uId: cjdylw9yj0000vktbbqfeubz7 - cId: anonymous - cIP: ::1 - cAction: GET / root access
这可能是你想要的!