20

Can someone provide an example of how to configure sails.js to log to a file?

It seems like it should be straightforward, but I'm having trouble finding examples online.

I'm looking at changes in the config/log.js or the config/sockets.js files.

rcheuk
  • 1,140
  • 1
  • 12
  • 32

3 Answers3

33

According to the source code, for v0.9.x, you just have to set the filePath in your config/log.js:

module.exports = {
  log: {
    level: 'info',
    filePath: 'application.log'
  }
};
bredikhin
  • 8,875
  • 3
  • 40
  • 44
  • 1
    Thanks...I figured this out shortly after posting this question and reading the source code as well. Not too well documented, unfortunately. Forgot to update this question though. Marking as correct! :) Thanks! – rcheuk Dec 12 '13 at 16:06
  • Is there any way to access my logger files in config/log.js ? – Mahahari Apr 21 '14 at 08:08
  • @Mahahari Technically, you can, but it's a config file, it's not meant for that. – bredikhin Apr 21 '14 at 11:00
  • 3
    The source code link is now out of date, and I think the correct answer has changed. Sails seems to invoke a library called CaptainsLog, which used to wrap an actual logging library called winston, but it no longer does unless you invoke winston explicitly – djsadinoff Nov 09 '14 at 09:58
  • 1
    Seems to be out of date - perhaps at least update with the version that this works for and I'll change my vote. – Amir T Dec 31 '14 at 17:58
20

Logging to a file doesn't work out of the box. You need to invoke functionality in libraries two levels down. See the documentation for winston.

first install winston like so:

$ npm install winston

Then adjust config/log.js to look as follows

var winston = require('winston');

/*see the documentation for Winston:  https://github.com/flatiron/winston */
var logger = new(winston.Logger)({
  transports: [
    new (winston.transports.Console)({}),
    new (winston.transports.File)({
      filename: 'logfile.log',
      level: 'verbose',
      json: false,
      colorize: false
    })
  ]
});

module.exports.log = {
  /***************************************************************************
   *                                                                          *
   * Valid `level` configs: i.e. the minimum log level to capture with        *
   * sails.log.*()                                                            *
   *                                                                          *
   * The order of precedence for log levels from lowest to highest is:        *
   * silly, verbose, info, debug, warn, error                                 *
   *                                                                          *
   * You may also set the level to "silent" to suppress all logs.             *
   *                                                                          *
   ***************************************************************************/

  level: 'silly',
  colorize: false,
  custom: logger
};
coagmano
  • 5,542
  • 1
  • 28
  • 41
djsadinoff
  • 5,519
  • 6
  • 33
  • 40
  • I'll re-accept this as the new correct answer if enough people upvote it. I am not actively using sails, so I cannot vouch for this at this time. – rcheuk Nov 09 '14 at 14:36
  • I needed to npm install winston for this to work. Moreover, I'm not seeing any connections getting logged (but that might be another topic) – Amir T Dec 31 '14 at 17:57
  • From last two days winston is not logging any logs in the file. When I restart PM2 it logs for few hours and then again stops logging. What could be the issue – Arshad Sep 27 '16 at 03:33
  • 1
    This works, but forces you to specify the log level right there. Do you know how to use winston but allow the overridden value, e.g. from `config/local.js`, to set the log level? – Tyler Collier Oct 20 '16 at 22:21
  • 1
    Works perfectly. It can safely be the accepted answer. – tkarls Dec 06 '16 at 07:44
  • problem is, we can't use env/development.js config vals here then – Raza Ahmed Jul 18 '17 at 07:55
1

For winston 3.x.x versions

@djsadinoff's answer not works.

Instead do:

 $ npm install winston

Replace your config/log.js file with the following code in Sails.js

    var winston = require('winston');
    const logger = winston.createLogger({
      level: 'silly',
      format: winston.format.json(),
      transports: [
        //
        // - Write to all logs with level `info` and below to `combined.log`
        // - Write all logs error (and below) to `error.log`.
        //
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'sails.log' })
      ]
    });

    //
    // If we're not in production then log to the `console` with the format:
    // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
    //
    if (process.env.NODE_ENV !== 'production') {
      logger.add(new winston.transports.Console({
        format: winston.format.simple()
      }));
    }

    module.exports.log = {
      /***************************************************************************
      *                                                                          *
      * Valid `level` configs: i.e. the minimum log level to capture with        *
      * sails.log.*()                                                            *
      *                                                                          *
      * The order of precedence for log levels from lowest to highest is:        *
      * silly, verbose, info, debug, warn, error                                 *
      *                                                                          *
      * You may also set the level to "silent" to suppress all logs.             *
      *                                                                          *
      ***************************************************************************/

      // Pass in our custom logger, and pass all log levels through.
      custom: logger,
      level: 'silly',

      // Disable captain's log so it doesn't prefix or stringify our meta data.
      inspect: false
    };

Then do

$ sails lift
Abhi
  • 3,361
  • 2
  • 33
  • 38