morgan-body
morgan-body copied to clipboard
Can we access log details by key
Request: POST /api/v1/auth/signup at Sat Jan 16 2021 22:58:37 GMT+0100... Request Body: {...} Response Body: {...} Response: 500 10.412 ms
I want to store only response body value on cloud. How can I access it?
My code is :
const loggerStream = { write: message => { console.log(message); // do anything - emit to websocket? send message somewhere? log to cloud? }, };
morganBody(app, { // .. other settings noColors: true, stream: loggerStream });
ah, this is an interesting ask... right now by the time it arrives there it's a string, so you could to a regular expression, something like:
const regex = /Response Body\:\s+?(\{.*\n\})/s
const loggerStream = {
write: message => {
const [, responseBody] = regex.exec(message);
if (responseBody) {
doSomething(responseBody);
}
},
};
I'm not sure what the best way would be to expose the "tokens" to a consumer function. The optimized way morgan was written using the "Function" constructor makes this more complex than one might initially think. I would need to dig, I don't have time right now but feel free to go for it :)