Adam
Adam

Reputation: 1099

Sails.js HOWTO: implement logging for HTTP requests

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.

Upvotes: 23

Views: 7493

Answers (4)

Shivam Maheshwari
Shivam Maheshwari

Reputation: 106

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 :)

Upvotes: 3

lime
lime

Reputation: 64

I found this matched my needs - it uses the Morgan module for Express and hooks it all up for you: https://www.npmjs.com/package/sails-hook-requestlogger

Upvotes: 0

Eagle
Eagle

Reputation: 1074

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'
]

Upvotes: 18

sgress454
sgress454

Reputation: 24948

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();}

Upvotes: 24

Related Questions