Logger

Overview

JavaScript definitely needs logging. In fact, all development environments need a good logging interface. JavaScript perhaps needs it even more because it handles the concepts of asynchronism and closures.

JavaScript is the only language which does not provide any logging feature by default (the alert function cannot be a solution as it is intrusive and interrupts execution). The only solution is to use browser interfaces or, more recently, JavaScript debugging interfaces.

One of the first features of Archetype was to implement a simple logger interface which we could configure to redirect to different outputs (browser, page body, etc.).

The configuration process is fully described in the configuration chapter: Choose your logger

Logger object

The Logger object is defined globally, so you can use it everywhere in your code. You can use the following methods in order to use different ways of logging:

  • log : default value
  • debug : log with the "debug" level
  • info : log with the "info" level
  • warn : log with the "warn" level
  • error : log with the "error" level
  • fatal : log with the "fatal" level

Log level representation depends on the logger implementation.

Filtering logs on the log level is planed for future releases but not currently ready, it's just about representation.

You can use as many parameters as you want with these methods. By default, it will concatenate all string representations of the parameters. On interfaces which allow "object" representation, parameters are not converted into strings. This is useful in Firebug which presents objects with its rich interface.

A very simple example:


var foo = "example";
Logger.debug("My log: ", foo);
// => Print: [debug] My log: example
			

Logger implementation

Archetype comes with a few different implementations. We usually work with the Firebug logger but use other ones as well sometimes (for browser compatibility testing for example).

  • alert : uses the basic JavaScript alert function. To be used cautiously since this is ugly and very intrusive (and probably endless)
  • body : inserts logs into the page body, not very pretty but useful for browser compatibility
  • firebug : redirects logs into the firebug console (but also in Safari, Opera and Chrome -- it's based on "console.log")
  • nitobibug : redirects logs into the nitobibug interface
  • null : no logs, useful for production
  • piDebugger : redirects logs into the piDebugger interface

We plan to add the full integration of the new Firebug Lite very soon.