I have extended winston-express-request-logger to print out every request, process, error, and user information in my Express/NodeJs application.
But the application actually logs no information regarding the request.
Sample log output:
2018-02-22T11:18:44.871Z - info: root access
I thought the official tutorial (found here) would lead me to a working implementation. Am I missing anything from the tutorials?
The whole application code:
'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;
You just need to pass the req object to the logger.info calls, e.g. do
logger.info(req, 'home');
instead of
logger.info('home');
This will give a log line like:
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
Which is probably what you want!