Logger

A Javascript logging utility, with Log4j-style levels.

Download:

Features

  • Log messages at five levels of severity/granularity: DEBUG, INFO, WARN, ERROR and FATAL.
  • Errors from window.onError are also logged.

  • Logging level is configurable at run time.

Usage

Include suitable rules in your CSS to style log messages. See the example stylesheet above.

Initialise the logger with an output area, a level and true if you want to display controls to clear the log or change the logging level on the fly, false otherwise:

Logger.initialize("logger", Logger.INFO, false);

Sprinkle logging messages throughout your code while developing:

myFunction: function()
{
    Logger.debug(">>>myFunction");

    try
    {
        var aVar = this.doSomething();
        Logger.info(aVar);
    }
    catch(e)
    {
        Logger.error("Error doing something: " + e.message);
    }
    Logger.debug("<<<myFunction");
}

If you want to guard against creating log messages which are expensive or slow to create, use the is...Enabled() methods:

if (Logger.isInfoEnabled())
{
    Logger.info(this.assembleAHugeMessage());
}

Reap the rewards of being able to easily track the flow and state of your Javascript application.

Turn the Logger off for production:

Logger.initialize("logger", Logger.OFF, false);

Dependencies

Reference

Property Type Description
DEBUG Number 0: Code for the DEBUG level
INFO Number 1: Code for the INFO level
WARN Number 2: Code for the WARN level
ERROR Number 3: Code for the ERROR level
FATAL Number 4: Code for the FATAL level
OFF Number 5: Code for the OFF level
levelNames Array Used to translate logging levels to their names for logging control
out DOM Element The Element logged messages will be added to
level Number The current logging level
Method Kind Argument Description
initialize(out, level, showControls) static
out
the id of the element to log messages to - will be created if it does not exist
level
the lowest logging level which will be enabled
showControls
if true, logging controls will be shown
Logs the given message at a DEBUG level
debug(message) static
message
the message to be logged
Logs the given message at a DEBUG level
info(message) static
message
the message to be logged
Logs the given message at an INFO level
props(obj) static
obj
an object whose properties will be logged
Logs the properties of the given object at an INFO level
warn(message) static
message
the message to be logged
Logs the given message at a WARN level
error(message) static
message
the message to be logged
Logs the given message at a ERROR level
fatal(message) static
message
the message to be logged
Logs the given message at a FATAL level
isDebugEnabled() static (none) Returns true if DEBUG level logging is enabled, false otherwise
isInfoEnabled() static (none) Returns true if INFO level logging is enabled, false otherwise
isWarnEnabled() static (none) Returns true if WARN level logging is enabled, false otherwise
isErrorEnabled() static (none) Returns true if ERROR level logging is enabled, false otherwise
isFatalEnabled() static (none) Returns true if FATAL level logging is enabled, false otherwise
_displayMessage(message, className, label) static
message
a log message
className
the class name to give the message which is output
label
a label for the log message
Outputs a log message
_getLogTime() static (none) Returns the current time in HH:MM:SS.mmm format
_internal(message) static
message
the message to be logged
Logs the given message at an INTERNAL level
_logWindowError(message, url, line) static
message
an error message
url
the URL of a script in which an error occurred
line
the line number on which an error occurred
Bound to winow.onerror by initialize() to capture error events
_clearMessages() static (none) Clears the message log
_setLevel(level) static
level
a logging level
Used to change the current logging level from the logging controls
_populateSelect(select, options) static
select
a DOM Element
options
a list of options with name and value properties
Used to populate the logging control select list
_removeChildNodes(node) static
node
a DOM Element from which to remove child nodes
Removes all child nodes from the given DOM Element