4

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

Community
  • 1
  • 1
Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • This might sound silly, but I would really like to use logger if it exists, rather then making my own... even thought making my own sounds tempting :) – Amiga500 Mar 22 '14 at 21:37
  • It looks like the question you linked to tells you exactly how to make `console.log` log to a file instead of the screen. Is there something else you're looking to do? – sgress454 Mar 22 '14 at 22:26
  • Does it mean that all of the console.logs that I have in application, such as in my example will be written to a file instead to screen? Given that I do what is instructed in that post? Or if not... how do I use it? warn("My warning here") / error("My error here")... – Amiga500 Mar 22 '14 at 23:06
  • 1
    Actually, looking at that answer more closely it looks like it relates to the Sails logger, so you'd want to do `sails.log` or `sails.log.warn`, `sails.log.info`, etc., but yes--just follow those instructions. – sgress454 Mar 23 '14 at 00:31
  • Scott would you please make your answer so I can vote it? :) – Amiga500 Mar 23 '14 at 15:47
  • Actually someone beat you to it--but you can just go [vote for this answer](http://stackoverflow.com/questions/22591861/how-to-log-in-sailsjs/22592079#22592079) and then delete this question :) – sgress454 Mar 23 '14 at 16:49
  • Voted on the other thread :) – Amiga500 Mar 23 '14 at 18:08

3 Answers3

4

You can use sails.log.error(), sails.log.warn(), sails.log.verbose(), etc to send logs to the console, or even to a file if you configure so in the config/logs.js file. There you can also specify the level of log to get in the output, or you can do it passing parameters in the sails command line (--verbose).

md asif rahman
  • 389
  • 3
  • 9
Diego Pamio
  • 1,377
  • 13
  • 21
2

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");
Abhi Patel
  • 214
  • 1
  • 6
1

I have used sails.log("message"), sails.log.debug("message or variable"), sails.log.error("error message or variable").

Syed Tabish Ali
  • 309
  • 3
  • 5