Closed hotrush closed 5 years ago
Given that the format function is being called by the internals of winston (not express-winston) and that we call log
on the winston instance only once all the log meta
has been put together, I believe you should be able to just use info.meta.res
to get at the response object in your format function.
You may also need to add body
to your responseWhitelist
(and perhaps add a responseFilter
if you're worried about logging sensitive information).
Hi @rosston
Problem is that i tried to add body
into response whitelist (responseWhitelist: [...expressWinston.responseWhitelist, 'body']
), but info.meta.res
still contains only { statusCode: 200 }
Hm, @hotrush, I'm unable to replicate the issue you're seeing. Here's my code:
const express = require('express');
const winston = require('winston');
const expressWinston = require('express-winston');
const app = express();
app.use(expressWinston.logger({
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf((info) => {
console.log('res', info.meta.res);
return `${info.meta.res.body}`;
})
),
responseWhitelist: [...expressWinston.responseWhitelist, 'body']
}));
app.use('/', (req, res, next) => {
res.status(200).send('this is the response body');
});
app.listen('9999');
console.log('listening on 9999');
When it runs, I get the following output:
res { statusCode: 200, body: 'this is the response body' }
this is the response body
I'm going to close this for now since I don't think the problem is within express-winston. If you feel it still needs attention from someone working on express-winston, feel free to comment here.
It only works if the body is a string, doesn't work with json.
Can i get response body inside format function to log it?