Logger
A Javascript logging utility, with Log4j-style levels.
Download:
Features
- Log messages at five levels of severity/granularity:
DEBUG,INFO,WARN,ERRORandFATAL. - Errors from
window.onErrorare 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 |
|
Logs the given message at a DEBUG level |
| debug(message) | static |
|
Logs the given message at a DEBUG level |
| info(message) | static |
|
Logs the given message at an INFO level |
| props(obj) | static |
|
Logs the properties of the given object at an INFO level |
| warn(message) | static |
|
Logs the given message at a WARN level |
| error(message) | static |
|
Logs the given message at a ERROR level |
| fatal(message) | static |
|
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 |
|
Outputs a log message |
| _getLogTime() | static | (none) | Returns the current time in HH:MM:SS.mmm format |
| _internal(message) | static |
|
Logs the given message at an INTERNAL level |
| _logWindowError(message, url, line) | static |
|
Bound to winow.onerror by initialize() to capture error events |
| _clearMessages() | static | (none) | Clears the message log |
| _setLevel(level) | static |
|
Used to change the current logging level from the logging controls |
| _populateSelect(select, options) | static |
|
Used to populate the logging control select list |
| _removeChildNodes(node) | static |
|
Removes all child nodes from the given DOM Element |