Skip to content

Logging

Jason Strimpel edited this page Jul 21, 2014 · 10 revisions

A simple logging utility is available on both client and server side through LAZO.logger.

It supports four logging levels: info, warn, error and debug.

LAZO.logger.info('Lorem ipsum dolor sit amet');
LAZO.logger.warn('Lorem ipsum dolor sit amet');
LAZO.logger.error('Lorem ipsum dolor sit amet');
LAZO.logger.debug('Lorem ipsum dolor sit amet');

Logging levels can be changed during runtime.

LAZO.logger.setLevel('warn');
LAZO.logger.setLevel('error');

It accepts more than one argument and printf-style formatting.

LAZO.logger.debug('Lorem %s dolor %s amet', 'ipsum', 'sit'); // Lorem ipsum dolor sit amet
LAZO.logger.debug('integer: %d', 3.14159); // integer: 3
LAZO.logger.debug('integer: %i', 3.14159); // integer: 3
LAZO.logger.debug('float: %f', 3.14159); // float: 3.14159
LAZO.logger.debug('json: %j', {foo:123, bar:456}); // json: {"foo":123,"bar":456}

##Sinks

The log messages are consumed by pluggable sinks, Lazo ships with two sinks: console sink and file sink (server-side only).

###Custom sinks

A custom sink should provide a function that would take a formatted message as a single argument. A custom sink can be registered by calling LAZO.logger.addSink:

LAZO.logger.addSink('customSink', function(message) {
  // TODO: write message somewhere
});

A sink can be de-registered by calling LAZO.logger.removeSink.

LAZO.logger.removeSink('customSink');

###File sink

The file sink must be configure and include by the application. The constructor takes an object with the following parameters: filePath, maxFiles, maxSize and roll.

define(['l!fileSink'], function(FileSink) {
  LAZO.logger.addSink('fileSink', new FileSink({
        filePath: 'app.log', // defaults to 'lazojs.log'
        maxFiles: 3, // defaults to '10'
        maxSize: 100 * 1024 * 1024, // defaults to '10MB'
        roll: false // defaults to 'true'
  }));
});