Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js HOWTO: implement logging for HTTP requests

Tags:

sails.js

With the poor default logging of Sails.js not showing http request logs(even on verbose). What is the best way implement http request logging to console so i can see if I am getting malformed requests? Expressjs's default logging would be enough.

I would prefer a Sails.js configuration way of doing it rather then a change the source code approach is possible.

Has anyone had experience with this. My google searches seem oddly lacking information.

Running Sails v0.9.8 on Mac OSX.

like image 516
Adam Avatar asked Feb 11 '14 12:02

Adam


3 Answers

There's no Sails config option to log every request, but you can add a quick logging route at the top of config/routes.js that should do the trick:

// config/routes.js
'/*': function(req, res, next) {sails.log.verbose(req.method, req.url); next();}
like image 89
sgress454 Avatar answered Nov 06 '22 06:11

sgress454


Maybe too late, but for future references about this, I'm using Sails 0.11 and you can config that in the middleware, in the config/http.js file

Add this function (in fact it comes as an example)

// Logs each request to the console
requestLogger: function (req, res, next) {
    console.log("Requested :: ", req.method, req.url);
    return next();
},

And setup it on the order var:

order: [
  'startRequestTimer',
  'cookieParser',
  'session',
  'requestLogger',  // Just here
  'bodyParser',
  'handleBodyParserError',
  'compress',
  'methodOverride',
  'poweredBy',
  '$custom',
  'router',
  'www',
  'favicon',
  '404',
  '500'
]
like image 27
Eagle Avatar answered Nov 06 '22 04:11

Eagle


I forked the sails-hook-requestlogger module to write all the request logs (access logs) to file.

sails-hook-requestlogger-file

All you have to do is npm install sails-hook-requestlogger-file and you are good to go!

Usage

Just lift your app as normal and all your server requests will be logged, with useful information such as response-time, straight to your console. As a default it is activated in your dev environment but deactivated in production.

Configuration

By default, configuration lives in sails.config.requestloggerfile You can create config/requestlogger.js and override these defaults:

Parameter        Type        Details
format           ((string))  Defines which logging format to use. Deaults to dev.
logLocation      ((string))  Defines where to log: console or file. Defaults to console.
fileLocation     ((string))  Location of file relative to project root (if file is specified in logLocation. This has no effect if console is specified in logLocation.
inDevelopment    ((boolean)) Whether or not to log requests in development environment. Defaults to true.
inProduction     ((boolean)) Whether or not to log requests in production environment Defaults to false.

Example config/requestlogger.js file:

module.exports.requestloggerfile = {
 //see: https://github.com/expressjs/morgan#predefined-formats for more formats
  format: ':remote-addr - [:date[clf]] ":method :url" :status :response-time ms ":user-agent"',
  logLocation: 'file',
  fileLocation: '/var/log/myapp/access.log',
  inDevelopment: true,
  inProduction: true
};

Hope that it would help someone :)

like image 3
Shivam Maheshwari Avatar answered Nov 06 '22 05:11

Shivam Maheshwari