JavaScript logging framework
log4javascript is a JavaScript logging framework based on the Java logging framework log4j and log4javascript 1.4.3 is now available.It takes one <script> include tag and one line of JavaScript to start using log4javascript.
log4javascript is designed to work in all major browsers. Internet Explorer 5 onwards on Windows, Firefox 1.0 onwards, Safari 1.2 onwards, Opera 8.01 onwards, Konqueror 3.4 onwards and Google Chrome are all fully supported.
The default way to display log messages is in a separate log4javascript window, featuring advanced search and filtering functionality, as well as a command line. You can also log to an in-page log4javascript console, to Firebug, to Opera and Safari’s built-in error consoles or back to the server via Ajax.
Logging examples
A simple logging message string
log.info("Hello world");displays
19:52:03 INFO - Hello world
Logging an error with a message
try { throw new Error("Faking something going wrong!"); } catch (e) { log.error("An error occurred", e); }displays
19:52:32 ERROR - An error occurred Exception: Faking something going wrong! on line number 80 in file basic.html
Logging multiple messages with one logging call
var a = "Hello"; var b = 3; log.debug(a, b);
displays
19:53:05 DEBUG - Hello 3
Logging an object
var obj = new Object(); obj.name = "Octopus"; obj.tentacles = 8; log.info(obj);
displays
19:53:17 INFO - { name: Octopus, tentacles: 8 }Enabling / disabling log4javascript
All logging can be enabled or disabled in log4javascript in a number of ways:
At any time, you can call log4javascript.setEnabled(enabled). This will enable or disable all logging, depending on whether enabled is set to true or false.
Assign a value to the global variable log4javascript_disabled. The idea of this is so that you can enable or disable logging for a whole site by including a JavaScript file in all your pages, and allowing this file to be included before log4javascript.js to guarantee that no logging can take place without having to alter log4javascript.js itself. Your included .js file would include a single line such as the following:
var log4javascript_disabled = true;
Assign your logger object a value of log4javascript.getNullLogger().
Replace your copy of log4javascript_x.js with stubs/log4javascript_x.js, provided in the distribution. This file has a stub version of each of the functions and methods in the log4javascript API and can simply be dropped in in place of the main file. The compressed version of the stub is typically 15 times smaller than the compressed version of the main file.
Differences between log4javascript and log4j
For the sake of keeping log4javascript as light and useful as possible, many of the features of log4j that seem over-complex or inappropriate for JavaScript have not been implemented. These include:
Filters
Configurators
Renderers

Recent Comments