Search code examples
loggingsails.js

Sails.js logging system


I need to implement logging to our sails server. I have found sails logging to file

But I am afraid I don't understand it very well. Intention is to replace console.log with log system.

This is just one example of many places where I need to implement logger:

  s3.putObject(params, function (err, data) {
                        if (err)
                            console.log(err);
                        else                         
                            console.log("Successfully uploaded " + 

//etc


Solution

  • Just put this in your config/log.js

    var winston = require('winston');
    var customLogger = new winston.createLogger();
    
    // A console transport logging debug and above.
    customLogger.add(new winston.transports.Console, {
      level: 'debug',
      colorize: true
    });
    
    // A file based transport logging only errors formatted as json.
    customLogger.add(new winston.transports.File({
      level: 'error',
      filename: './error.log',
      json: true,
      colorize: true
    }));
    
    module.exports.log = {
      // Pass in our custom logger, and pass all log levels through.
      custom: customLogger,
      level: 'silly',
    
      // Disable captain's log so it doesn't prefix or stringify our meta data.
      inspect: false
    };
    

    now whenever you call sails.log.error() it will list your log in error.log file

    sails.log.error("Error demo message");