Last Updated: February 25, 2016
·
676
· smallhadroncollider

Using LogLevel for environment specific logging

LogLevel is a useful tool that lets you log different levels of message to the JavaScript console. For example log.error(jqXHR.status, jqXHR.responseText) would log an error message and log.info('Loading resources...') would do a normal console.log().

There are two advantages of using LogLevel:

  • If console.log() is not supported by the browser (i.e. older versions of IE) then it will not cause an error if you are using log.info()
  • You can selectively disable logging - this is very handy for development environments

This second advantage is particularly useful as it means you don't need to strip out all of your logging information when you move the code to production, you simply call log.disableAll() and all logging will be disabled.

This can be used in combination with using a config file with RequireJS in a bootstrap file:

define(['config', 'log', 'app'], function (config, log, app) {
    // Setup logging
    if (config.environment === 'development') {
        log.enableAll();
        log.warn('Running in Development Mode');
    } else {
        log.disableAll();
    }

    // Initialise app...
    app.start()
 });