Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loopback Logging - intercept all HTTP responses, regardless of which middleware/route produced it
// Title:
// Loopback Logging - intercept all HTTP responses,
// regardless of which middleware/route produced it
// References:
// https://apidocs.strongloop.com/loopback/#app-middleware
// https://apidocs.strongloop.com/loopback/#app-middlewarefromconfig
// https://apidocs.strongloop.com/loopback/#app-definemiddlewarephases
//Sample # 1 - super simple
// If you would rather use middleware.json ... make sure to
// install this middleware among the first middleware,
// as it needs to hook into the response object before
// any other middleware sends back the response.
app.middleware('initial', function logResponse(req, res, next) {
// install a listener for when the response is finished
res.on('finish', function() {
// the request was handled, print the log entry
log(req.method, req.originalUrl, res.statusCode);
});
// resume the routing pipeline,
// let other middleware to actually handle the request
next();
});
//Sample # 2 - bit more advanced
// Also helps track reponse time for all URLs
app.middleware('initial', function logResponse(req, res, next) {
// http://www.senchalabs.org/connect/responseTime.html
var start = new Date;
if (res._responseTime) {
return next();
}
res._responseTime = true;
// install a listener for when the response is finished
res.on('finish', function() { // the request was handled, print the log entry
var duration = new Date - start;
log(req.method, req.originalUrl, res.statusCode, duration + 'ms', {
lbHttpMethod:req.method,
lbUrl:req.originalUrl,
lbStatusCode:res.statusCode,
lbResponseTime:duration
});
});
// resume the routing pipeline,
// let other middleware to actually handle the request
next();
});
//Sample # 3 - bit more focused
// Does work only for the API related URLs
app.middleware('initial', '/api/*', function logResponse(req, res, next) {
// same code as sample # 2
});
@bhavinrana07

This comment has been minimized.

Copy link

@bhavinrana07 bhavinrana07 commented Feb 9, 2017

is there a way to get/log /print the Response.Body ? i am not able to get the res.body with using res.body.

@htmlauthor

This comment has been minimized.

Copy link

@htmlauthor htmlauthor commented Feb 22, 2017

Same question as from bhavinrana07. I want to log responses which in my case is in JSON, is there a way to access it from middleware?
I know I can access it from remote hook of specific remote method, but I would like to have all my logging in one place and middleware sounds like the place where it all should stay.

@musghost

This comment has been minimized.

Copy link

@musghost musghost commented Mar 9, 2017

I think you guys could use something like this strongloop/loopback#624 (comment)
I hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.