Getting started with JavaScript logging
The Basics
After you have installed JSNLog, add this to your JavaScript code to send a log message to your server side log:
JL().fatal("log message");
JL() returns a nameless logger, called the root logger. In your server side log, it will be called ClientRoot.
You can log objects as well:
JL().fatal({"i": 5, "j": 6});
Named loggers
To make it easier to track where in your code the message was logged, give your loggers names. For example:
JL("function1.logger1").warn("log message");
Message severity
JSNLog is very similar to Log4J / Log4Net. It uses the same severities: TRACE, DEBUG, INFO, WARN, ERROR or FATAL.
For example:
JL().trace("a trace message");
Set logger options
JSNLog has many features that help you reduce log traffic by only logging useful information. For example:
var logger = JL(); logger.setOptions({ "userAgentRegex": "MSIE 7|MSIE 8" }) ; logger.trace("Only IE7 and IE8 will send this message.");
Log exceptions in a try-catch block
JSNLog logs uncaught exceptions for you out of the box. You can add to this by catching exceptions in a try-catch block and then logging the exception (details):
try { ... dodgy code } catch (e) { JL().fatalException("something went wrong!", e); }
Next Step
- Dive into specialized topics - such as error handling in AngularJs apps.