Skip to content
Patrick Wolf edited this page Jan 15, 2016 · 1 revision

The logging service uses winston. Out of the box log levels debug, verbose, info, warn, and error are enabled.

  var logger = server.get('logger');
  logger.debug('debug message');
  logger.info('info message');
  logger.warn('warn message');
  logger.error('error message');

Configuration

Configuring transports

Additional transports can be used in place of the default console transport. Here you can see a mongodb transport being used.

 "logger": {
    "transports": [
      {
        "package": "winston-mongodb",
        "field": "MongoDB",
        "options": {
          "db": "test"
        }
      }
    ]
  }

A transport requires a package, field, and options. The package will be loaded through require, the specified field will than be added to the logger with the given options.

For example, to use the file transport, the config might look like

 "logger": {
    "transports": [
      {
        "package": "winston",
        "field": "transports.File",
        "options": {
          "filename": "foo.log"
        }
      }
    ]
  }

Or to use the console transport with a different log level

    "transports": [
      {
        "package": "winston",
        "field": "transports.Console",
        "options": {
          "level": "info",
          "colorize": true
        }
      }
    ]

Programmatically adding transports

If transport settings can't be adequately represented in config, e.g. if you need to add a custom function to a transport, there's an alternative way to add a transport.

Create a logger.js in your application directory with an init method. The winston logger will be passed to the init method during server startup and let you add transports. Keep in mind though that any logger configuration in the config files will be ignored.

The example below creates a Console transport with a custom timestamp function.

var winston = require('winston');

module.exports.init = function(logger) {

    logger.add(winston.transports.Console, {
        timestamp: function() {
            return Date.now();
        }
    });
}
Clone this wiki locally