15

What is the actual syntax to log in SailsJS?

Docs don't have anything, but found the following line in the site's roadmap

"Pull out Sails.log (winston wrapper) as a separate module so it can be used by waterline"

I image it's something like:

Sails.log(...)
Sails.error(...)
Sails.warn(...)
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
binarygiant
  • 6,362
  • 10
  • 50
  • 73

2 Answers2

28

In your controllers, models, services, and anywhere else that the sails global is available, you can log with one of:

sails.log();
sails.log.warn();
sails.log.info();
sails.log.debug();
sails.log.error();
sails.log.verbose();
sails.log.silly();

The logging level (that is, the level at which logs will be output to the console) is set in /config/log.js.

sgress454
  • 24,870
  • 4
  • 74
  • 92
4

To extend on the Scott Answer, dont forget to add the filePath: property to log.js file... otherwise it will not work :)

So it should be something like:

log: {
level: 'info',
maxSize: 1000,
filePath: 'c://serverlogs/mylogfilename.log'

Answer is changed based on Joseph question.

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • Which location the log file is created? I tried filePath: '~/name.log'; filePath: 'name.log'. But the files are not created though I can see the log messages in the console. I'm using 0.10.0-rc5. – jjude May 20 '14 at 01:46
  • 2
    @Joseph please take a look at the previous answer. I have updated it. Hope it helps. – Amiga500 May 20 '14 at 08:17
  • Mine is not created too even specifying filePath. My sails version is 0.10.0-rc7 – A-letubby Aug 25 '14 at 05:50
  • I found the solution. The filePath only worked for under 0.10. For >0.9, please see https://stackoverflow.com/questions/25211183/sails-js-0-10-x-log-to-file – A-letubby Aug 25 '14 at 06:45