Skip to content

Logging

Rodrigo Fernandez edited this page Jul 21, 2014 · 10 revisions

##Overview

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).

###Customs 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');