Search code examples
node.jsloggingsails.jserror-logging

Sails.js 0.10.x: Log to file


I want to use the built in Sails.js logger and log everything to a file. The "filePath" variable used to work in 0.9.x in config/log.js, but it no longer appears to work in 0.10.x. Has this feature been removed - if so, what's the best way to log to disk now?


Solution

  • For logging, Sails.js (>= 0.10.0) relies on captains-log, a "lightweight logger with a simple pass-through configuration for use with fancier logging libraries."

    To log to a file, you need to configure a custom logger. I would suggest you use Winston, and configure it with the File Transport. This is relatively easy to do:

    Install winston

    $ npm install winston --save
    

    In your app, edit the config/log.js file and add the following content

    var winston = require('winston');
    
    var customLogger = new winston.Logger({
        transports: [
            new(winston.transports.File)({
                level: 'debug',
                filename: './logs/my_log_file.log'
            }),
        ],
    });
    
    module.exports.log = {
        colors: false,  // To get clean logs without prefixes or color codings
        custom: customLogger
    };
    

    Log any messages with the standard sails log command

    sails.log.debug("Message to be logged");
    

    Restart sails

    $ sails lift
    

    You should see content getting stored in your ./logs/my_log_file.log file.