dracoblue.net

log4j like logging in node.js (from Spludo)

One core feature of spludo, is the powerful logging system. This system is entirely independent from spludo, so you may use it in your own (even non-spludo) nodejs project with ease.

Like described in the tutorial about configuration, you can easily add logging facality to every prototyped class:

DocsManager = function() {
    // ...
}

extend(true, DocsManager.prototype, Logging.prototype);

DocsManager.prototype.logging_prefix = 'DocsManager';

It's important to add #logging_prefix, otherwise the methods won't know which class originally had the function implemented.

Usually you use it with just one parameter:

this.info("This was awesome");

and you'll get:

INFO  [DocsManager] This was awesome

If you add (multiple parameters):

this.warn("Something happened with", user.toString(),"lately!");

you'll get something like:

WARN  [DocsManager] { '0': 'Something happend with', '1': 'hans', 2: 'lately!' }

Since spludo's source is full of some special log level called "trace", at each function call, you get a very special behaviour if you do this:

this.trace("getSectionAsHtml", arguments);

it transforms to: TRACE [DocsManager.getSectionAsHtml] { '0': 'extending-the-docs-manager' }

In this case the function DocsManager#getSectionAsHtml was called with one argument "extending-the-docs-manager". This is helpful, if you want to debug the applications flow without breakpoints.

Of course Logging supports different levels. They are behaving in the same manner and meaning, like those at log4j - see

log4j manual):

Logging.LEVEL_ALL = 127;
Logging.LEVEL_TRACE = 6;
Logging.LEVEL_LOG = 5;
Logging.LEVEL_DEBUG = 5;
Logging.LEVEL_INFO = 4;
Logging.LEVEL_WARN = 3;
Logging.LEVEL_ERROR = 2;
Logging.LEVEL_FATAL = 1;
Logging.LEVEL_OFF = 0;

The default log level is Logging.LEVEL_WARN = 3.

If you want to hide specific classes from the log, you may use:

config.setValues("logging", {
    "level": 7,
    "hide_classes": [
        "DocsManager",
        "PasswordManager"
    ]
});

this will hide those from the output. Very useful, if you want to debug a specific subset of functions.

Logging for spludo's core classes is usually disabled. You can enable it back:

config.setPathValue(['logging', 'log_core'], true);

You may ask if the performance suffers, when having so much logging inside your code. The answer is: not really. If the logging level is set to WARN, for instance the method .log will be replaced with an empty function. So whenever you call it, nothing happens. Obviously you have a very fast way for debugging in case of an error :-).

That's it! Have fun logging!

There are some things to remember,

when using it without spludo.

Download the Logging.js, load it with: require("./Logging").

Replace the lines where it says

log_configuration = config.get(..);

with:

log_configuration = {
    'level': 7,
    'hide_classes': [
        'DocsManager'
    ]
}

If you want to extend your base objects with Logging, like described earlier, you'll need extend from

util.js.

In javascript, node.js, open source, spludo by @ 08 Oct 2010

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll