JavaScript logging redone

JSNLog.js is a tiny JavaScript logging library, simple and well documented. It has unique options to cut down on the amount of logging data you receive while still getting the information you actually need.

Vital Stats

Insert loggers in your JavaScript

JL("jsLogger").info("log message");

Log JavaScript exceptions, with their stack traces

try {
    } catch(e) {
        JL().fatalException("Exception info", e);

Configure loggers in your JavaScript code

JL("jsLogger").setOptions({ "level": 4000 });

Only log for certain user agents or IP addresses

        { "userAgentRegex": "MSIE 7|MSIE 8" });

Log objects as well as strings

var obj = {"f1": "v1", "f2": "v2"};
    JL().log(2500, obj);

Pass in function that returns log info

JL().log(2500, function() {
        // Only executed when log info
        // will actually be logged.
        var loginfo = expensiveOperation();
        return loginfo;

Supports AMD and CommonJS loaders

As well as loading with a simple script tag.

See which messages belong to the same request

Request IDMessage
CB0734CE....A debug message
D623A22B....An error message
CB0734CE....Some other message
D623A22B....Yet another message
D623A22B....A fatal message

Suppress messages that match a regular expression

        { "disallow": "suppress me" });

Limit total messages sent to server

JL.setOptions({"maxMessages": 3});

Reduce AJAX requests by batching log messages

        { "batchSize": 3 });

Easily send logging data to the server

Built in AjaxAppender uses simple, well documented message format.

Suppress duplicate messages

jsLoggerx too high (x = 5)
jsLoggerx too high (x = 6)
jsLoggerx too high (x = 7)
jsLoggerx too high (x = 8)
jsLoggerx too high (x = 9)

Get the info you need, but only when you need it

  • To solve JavaScript exceptions, you often need to know what happened before they happened.
  • But you don't want to send trace messages with that context unless there actually is an exception.
  • Solution: JSNLog lets you buffer log messages on the client, and only send them when there is a fatal message.

Show log data in the console

Switch console logging on or off with one line of code.

screenshot of browser console with log generated by JSNLog