Search code examples
jsonnode.jsexpresssails.jswinston

How to log JSON HTTP Responses using Winston/Morgan


I'm using Winston and Morgan for all the back-end logging in Sails.js and I need to be able to log the responses from HTTP get requests. I need to log them in a file. My logFile currently takes shows all the http requests but it does not show the responses. I have searched all the options for Morgan and Winston and can't find a way/option to do this. I was just wondering if any of you had any advice on how to accomplish this? Thanks!


Solution

  • You can write a middleware function for ExpressJS that will log the body once a response is sent. Basing it off of Node's http module to see how Connect (and therefore Express) manages the response body (which is a stream): you can hook into the two methods that write to that stream to grab the chunks and then concat/decode them to log it. Simple solution and could be made more robust but it shows the concept works.

    function bodyLog(req, res, next) {
      var write = res.write;
      var end = res.end;
      var chunks = [];
    
      res.write = function newWrite(chunk) {
        chunks.push(chunk);
    
        write.apply(res, arguments);
      };
    
      res.end = function newEnd(chunk) {
        if (chunk) { chunks.push(chunk); }
    
        end.apply(res, arguments);
      };
    
      res.once('finish', function logIt() {
        var body = Buffer.concat(chunks).toString('utf8');
    
        // LOG BODY
      });
    
      next();
    }
    

    And then set it before any routes are assigned in the main app router:

    app.use(bodyLog);
    // assign routes
    

    I would assume you could also use this as an assignment for a variable in Morgan but I haven't looked into how async variable assignment would work.