Lumberjack

Set localStorage.lumberjack to on to enable logging.

Params
enabled Boolean True to force logging regardless of the localStorage setting.
Returns
Object A new Lumberjack.
See
module.exports = function (enabled) {
    var log,
        record = {},
        cbQueue = {},
        master = [],
        ls = global.localStorage || {};


log(channel, data)

Record a log entry for an channel.

Params
channel String A string describing this channel.
data String Object Number Boolean Some data to log.
    log = function (channel, data) {
        var i, len, channel, entry;
        var channelValid = typeof channel === 'string';
        var dataType = typeof data;
        var dataValid = dataType !== 'undefined' && dataType !== 'function';
        if (ls.lumberjack !== 'on' && !enabled) {

Do nothing unless enabled.

            return;
        }
        if (channelValid && dataValid) {

All log entries take the form of:

 {
     time: // timestamp when entry was logged
     data: // the logged data
     channel: // channel of entry
     id: // id of entry in master record
 }
            entry = {
                time: new Date(),
                data: data,
                channel: channel,
                id: master.length
            };

Record the channel.

            record[channel] = record[channel] || []
            record[channel].push(entry);
            master.push(entry);


Perform any attached callbacks.

            cbQueue[channel] = cbQueue[channel] || [];
            len = cbQueue[channel].length;
            for (i = 0; i < len; i += 1) {
                cbQueue[channel][i](data);
            }
        } else {
            throw Error('Lumberjack Error: log(channel, data) requires an channel {String} and a payload {String|Object|Number|Boolean}.');
        }
    };


log.clear([channel])

Clear all data from a the log.

Params
[channel] String Name of a channel.
    log.clear = function (channel) {
        if (channel) {
            record[channel] = [];
        } else {
            record = {};
            master = [];
        }
    };


log.readback(channel, [pretty])

Fetch the log of an channel.

Params
channel String A string describing this channel.
[pretty] Boolean True to create a formatted string result.
Returns
Array String This channel's current record.
    log.readback = function (channel, pretty) {
        var channelValid = typeof channel === 'string';
        if (channelValid) {
            if (pretty) {
                return JSON.stringify(record[channel], null, 4);
            }
            return record[channel] || [];
        }
        throw Error('log.readback(channel, pretty) requires an channel {String}.');
    };


log.readback.master([pretty])

Get a full readback of all channels' entries.

Params
[pretty] Boolean True to create a formatted string result.
Returns
Array String This log's master record.
    log.readback.master = function (pretty) {
        if (pretty) {
            return JSON.stringify(master, null, 4);
        }
        return master;
    };


log.readback.channels([pretty])

Fetch list of log channels currently in use.

Params
[pretty] Boolean True to create a formatted string result.
Returns
Array String This log's set of used channels.
    log.readback.channels = function (pretty) {
        var keys = Object.keys(record);
        if (pretty) {
            return JSON.stringify(keys);
        }
        return keys;
    };


log.flush([channel])

Flush all logs from a single channel or from the entire system if no channel name is provided.

Params
[channel] String Optional name of channel to flush.
Returns
Array {Array}
    log.flush = function (channel) {
        var logs;
        if (channel) {
            logs = record[channel];
            record[channel] = [];
        } else {
            record = {};
            master = [];
            logs = [];
        }
        return logs;
    };


log.on(channel, cb)

Attach a callback to run anytime a channel is logged to.

Params
channel String A string describing this channel.
cb Function The callback.
    log.on = function (channel, cb) {
        var channelValid = typeof channel === 'string';
        var cbValid = typeof cb === 'function';
        if (channelValid && cbValid) {
            cbQueue[channel] = cbQueue[channel] || [];
            cbQueue[channel].push(cb);
        } else {
            throw Error('log.on(channel, cb) requires an channel {String} and a callback {Function}.');
        }
    };


log.off(channel)

Disable side-effects for a given channel.

Params
channel String A string describing this channel.
    log.off = function (channel) {
        var channelValid = typeof channel === 'string';
        if (channelValid) {
            cbQueue[channel] = [];
        } else {
            throw Error('log.off(channel) requires an channel {String}.');
        }
    };


log.enable()

Activate logging regardless of previous settings.

    log.enable = function () {
        enabled = true;
    };


log.disable()

Force logging off.

    log.disable = function () {
        enabled = false;
    };

    return log;
};