How to use logging mechanism efficiently?

3k Views Asked by At

I am using log4javascript to log and track the issues in my JavaScript code. I have seen similar logging aids before but I am having difficulty in understanding how each of these log levels should be used to be more useful and productive.

Most of the time I end up logging debug, info or trace without being really aware of how productive each of them are or not. As the code grows larger and larger it's getting tough and I feel logs are more trouble than help.

Can someone give me some guidelines/help so that I can use the logging mechanism well.

Following are the different log levels supported by the log4javascript:

  1. log4javascript.Level.ALL
  2. log4javascript.Level.TRACE
  3. log4javascript.Level.DEBUG
  4. log4javascript.Level.INFO
  5. log4javascript.Level.WARN
  6. log4javascript.Level.ERROR
  7. log4javascript.Level.FATAL
1

There are 1 best solutions below

2
On BEST ANSWER

I'm the author of log4javascript and I use it every day in my work. Here's how I use it:

  • I tend to use debug() and trace() most frequently. I use trace() for logging low level operations when trying to hunt down a bug and debug() for more general logging of program flow. I have the console threshold set to DEBUG for my usual coding so that I don't have trace messages cluttering up the log and then switch it to ALL when I need to see the trace messages.
  • I use info() quite a bit, usually to make particular messages stand out a little in the logging console.
  • I make liberal use of group() (see http://log4javascript.org/docs/manual.html#loggers) to group logging for a particular operation and allows me to expand and collapse chunks of logging. Groups can also be nested.
  • I keep my logging initialization code in one place and give each component of my application a separate logger (which inherits from the root logger). This allows me to set logging thresholds for particular components.

For example:

var component1 = (function() {
    var log = log4javascript.getLogger("MyApp.Components.Component1");

    // Implementation stuff
})();

var component2 = (function() {
    var log = log4javascript.getLogger("MyApp.Components.Component2");

    // Implementation stuff
})();

In the logging initialization code:

// Create a console appender that is inherited by all loggers
var appender = new log4javascript.PopUpAppender();
appender.setThreshold(log4javascript.Level.DEBUG);

// Limit the number of messages displayed in the console at any one time
appender.setMaxMessages(2000);

log4javascript.getRootLogger().addAppender(appender);

// Disable all logging except ERROR and FATAL for the "MyApp.Components"
// logger and all its descendants (including "MyApp.Components.Component1" and
// "MyApp.Components.Component2")
log4javascript.getLogger("MyApp.Components").setLevel(log4javascript.Level.ERROR);

This stuff is common to all log4x logging frameworks, so documentation from log4j or log4net will apply. For example, the old but still relevant log4j short manual may help.