evented-logger

Version:
0.2.0 (2011-03-14)
Module:
require("evented-logger");
In browsers:
eventedLogger; / buster.eventedLogger;

A simple evented console logger. In contrast to typical loggers, evented-logger does not print directly to an output stream. Instead, log messages are emitted as events, decoupling the visualization/persistence of log messages.

evented-logger supports setting a log level to filter messages, logging arbitrary many arguments with one call, customizable formatting of objects and even custom log level hierarchies.

Events

Event: log

Signature:

"log", function (envelope) {}

Emitted every time data is logged and is allowed by the current log level. Any messages logged to the current level or a higher priority level will be emitted.

The argument to the callback is an Envelope.

Methods

Parts of the logger API is dynamically assembled by the eventedLogger.create() method. The API documentation also describes the methods dynamically generated by this method in the default setup.

eventedLogger.create()
create([options]);

Creates a new logger. The default behavior is to create a logger with the levels/methods error, warn, log and debug. "debug" is the default log level, meaning that all messages are emitted.

The options` argument can be used to control what logging methods the logger should have, default logging level and formatting method. See Options for details on the options object.

Typical usage

var logger = eventedLogger.create();
logger.on("log", function (msg) {
    console.log("[" + msg.level.toUpperCase() + "] " + msg.message);
});
logger.warn("Watch it!");
logger.log([], 42, {});
// Prints the following to stdout:
// [WARN] Watch it!
// [LOG] [] 42 {}

Setting the default level

var logger = eventedLogger.create({ level: "warn" });
logger.on("log", function (msg) {
    console.log("[" + msg.level.toUpperCase() + "] " + msg.message);
});
logger.warn("Watch it!");
logger.log([], 42, {});
// Will not print the log message, so stdout looks like:
// [WARN] Watch it!

Creating custom loggers

Custom levels should be passed ordered from highest to lowest severity. The generated methods will pass through messages if the current log level is set to either the same level as the message or one in the lower indexes of the levels array.

When you create a logger with customized levels, the default log level will be set to the most permissive one, i.e. the last level in the array.

var logger = eventedLogger.create({
    levels: ["nuclear", "eerie", "info", "debug"]
});
logger.level == "debug"; //=> true
typeof logger.error == "undefined";
logger.nuclear("This is NOT good");

If you want the logger to have some other default log level than the most permissive one, include level:

var logger = eventedLogger.create({
    levels: ["nuclear", "eerie", "info", "debug"],
    level: "eerie"
});
logger.info("This is NOT good"); // Won't be emitted
logger.format()
format(object);

Formats a logged object. This function is called once for each argument passed to a logger method. The default implementation serializes objects through JSON.stringify. Functions and primitives are converted to strings by way of their toString methods.

The method can be overridden to provide more powerful formatting of objects such as functions and problematic host objects.

buster-test provides more readable formatting through the buster-format module. There is basically three ways to achieve this:

Override the original method

eventedLogger.format = buster.format.ascii;

Override the method on an instance

var logger = eventedLogger.create();
logger.format = buster.format.ascii;

Pass the formatter to ``create``

var logger = eventedLogger.create({
    logger: buster.format.ascii
});
logger.error()
logger.error(message1[, message2, ...]);

Logs messages with the "error" level. Messages will always be emitted from the logger unless the log level has been set to a non-existent level.

var logger = eventedLogger.create();
// ...
logger.error("Something went wrong", myObjToDebug);

Note

If you have created a logger with custom levels, the error method will not exist unless you explicitly included it.

logger.warn()
logger.warn(message1[, message2, ...]);

Logs messages with the "warn" level. This message will be emitted from the logger unless its level is set to "error" or a non-existent level.

var logger = eventedLogger.create();
// ...
logger.warn("Something fishy?", myObjToDebug);

Note

If you have created a logger with custom levels, the warn method will not exist unless you explicitly included it.

logger.log()
logger.log(message1[, message2, ...]);

Logs messages with the "log" level. This message will be emitted from the logger if its level is set to "log" or "debug" (default).

var logger = eventedLogger.create();
// ...
logger.log("Here's an object", myObjToDebug);

Note

If you have created a logger with custom levels, the log method will not exist unless you explicitly included it.

logger.debug()
logger.debug(message1[, message2, ...]);

Logs messages with the "debug" level. This message will only be emitted from the logger if its level is set to "debug" (default).

var logger = eventedLogger.create();
// ...
logger.debug("What's going on??", myObjToDebug);

Note

If you have created a logger with custom levels, the debug method will not exist unless you explicitly included it.

Properties

logger.level

Default: "debug"

Set the level of the logger, silence all messages for less severe levels. The default level is the most permissive one - "debug" - when not using custom levels.

Supporting objects

Envelope

An object representing a logged message. Contains two properties:

loggerEnvelope.level

The log level as a lower case string, e.g. "debug".

loggerEnvelope.message

A formatted log message, containing all arguments passed to the log method joined by a single blank space.

Options

Options passed to eventedLogger.create().

loggerOptions.level

The default log level, i.e. the minimum required level the logger will emit events for. Default value is "debug", i.e. all messages.

loggerOptions.levels

An array of levels the logger supports. Default is ["error", "warn", "log", "debug"]. Each string in this array names methods created on the logger.

loggerOptions.formatter

The function that should format arguments. See logger.format().

Table Of Contents

Related Topics

This Page