JavaScript reference

Client code exposes class JsLog in a number of ways:

Initialization

To start tracking errors you need to make an object of JsLog class. You should specify parameters in the constructor - API key is a mandatory setting, other available parameters are described below

It is better to include logger module before any other modules or javascript code. It will allow you to track all initialization errors.

Using global declaration

<head>
    <script type="text/javascript" src="//jslog.me/js/jslog.js"></script>
    <script type="text/javascript">jslog = new JsLog({key: "YOUR-API-KEY"});</script>
</head>

Using RequireJS module

bower install requirejs
bower install jslog-me

Samle application with RequireJS includes index html file, which include only link to requirejs loader and specifies main file for requirejs (main.js in our case).

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <script data-main="main" src="bower_components/requirejs/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

Main.js includes loader configuration and some root bootstrap code, which will launch application activities.

main.js

require.config({
});

require(['bower_components/jslog-me/dist/jslog.min'], function(JsLog){
    var jslog = new JsLog({ key: "<API-KEY>" });
    jslog.log('First message');
    console.log('Another message');
});

Using CommonJS module (NodeJS logger)

CommonJS modules are common approach to make modules in NodeJS.

npm install jslog --save

Sample NodeJS application with error logging:

var JsLog = require('jslog'),
    jslog = new JsLog({ key: "<API-KEY>" });

jslog.log('First message from NodeJS');
console.log('Another message from NodeJS');

Constructor options

var jslog = new JsLog(options);

Options is an object which can include properties:

  • enabled: boolean - is logging enabled? (Optional, default 'true')
  • key: string - API key for project. Mandatory option.
  • version: string - Your application version. It lets you identify which versions of your application are affected by errors (Optional, default '')
  • sessionId: string - Session identifier. If not specified it will be generated automatically (Optional)
  • logUncaughtExceptions: boolean - hooks on window.onerror to intercept unhandled errors (Optional, default 'true')
  • hookConsole: boolean - hooks on window.console to intercept console.log, console.warn, console.error, console.info to send all local logs to the cloud (Optional, default 'true')
  • trackHost: boolean - tags a client device. It will send a tag in each message, which allow your to group sessions by user's device (Optional, default 'true')
  • collectSystemInfo: boolean - collect basic browser system information (user agent) (Optional, default 'true')
  • trackLaunches: boolean - send empty message to log even no errors or logs occured. It can be used to get application runs count and error rate (Optional, default 'true')

JsLog.init

Runs initialization code. It can be useful if you create object with {enabled: false} option, and then enabling logging with some additional options.

jslog.options.version = '1.2.3.4-beta';
jslog.options.enabled = true;
jslog.init();

JsLog.log, JsLog.info

Log ordinary message

var obj = {fieldA: 'First field', fieldB: 'Second field'};
jslog.log('Logging obj', obj');
console.log('Logging obj form console call', obj');

JsLog.warn

Log warning

jslog.warn('Logging warning');
console.warn('Logging warning from console');

JsLog.error, JsLog.exception

Log error or exception

try {
   throw new Error('Some error');
} catch(e) {
   // log error with call stack
   jslog.error(e);
}

JsLog.renewSession

Starts a new session. It helps you to split a large log to smaller pieces on your choice. You can split it on a new user login, or on long operations. Calling .renewSession() generates a new guid sessionId option, and lets you to send new session information (version, client ip, browser, etc.)

Note Calling .renewSession() does not make hooks on system services like a window.onerror or window.console. Use .init() for this.