Logging

Use the Unnode.js logger for easy console, file and Rollbar logging.

Console logging is always enabled. File logs will be saved in JSON format if the UNNODE_LOGFILE env var is set.

Log messages of level warning and above will be sent to Rollbar if the ROLLBAR_ACCESS_TOKEN and ROLLBAR_ENVIRONMENT env vars are set correctly. You can override this behavior with the overrideRollbar option to the logger (see below).

Master and worker loggers

Use require('unnode').masterLogger inside the master process and require('unnode').workerLogger when inside a worker process:

const unnode = require('unnode')
 
if(unnode.isMaster) {
 
    const masterLog = require('unnode').masterLogger
 
    // Skip logging this error to Rollbar
    masterLog.log('error', 'Some error', 'no-rollbar')
 
} else if(unnode.isWorker) {
 
    const workerLog = require('unnode').workerLogger
 
    // Force log this to Rollbar even though it's only
    // of level 'info'
    masterLog.log('info', 'Some message', 'force-rollbar')
 
}

log(level, message, overrideRollbar)

  • level: log level, one of the 8 syslog-style log levels:
    • debug
    • info
    • notice
    • warning
    • error
    • crit
    • alert
    • emerg
  • message: the log message. You can use something like chalk to colorize it for the console:
    const unnode = require('unnode')
    const chalk  = require('chalk')
     
    if(unnode.isWorker) {
        const workerLog = require('unnode').workerLogger
    
        const coloredMsg = chalk.underline.red('a message')
     
        masterLog.log('info', coloredMsg)
     
    }
  • overrideRollbar: By default, messages of level warning and above will get sent to Rollbar. Use this to override:
    • no-rollbar: Never send to Rollbar
    • force-rollbar: Always send to Rollbar
    • only-rollbar: Only send to Rollbar, skip console and file

Next: Utils