Skip to content

Log implementation and configuration #2

Description

@sebez

On CI-T, I added log4javascript for client logging :

It uses log4j-like configuration and API with named loggers, appenders, level, layout, etc.
Available appenders include : console appender, DOM appender, popup appender, Server API appender. Adding new appender (for instance a localStorage appender) seems uncomplicated.

Currently, Focus is logging using a hard-coded reference to a generic unnamed hand-made unconfigurable logger, or uses directly the console object.

It would be nice :

  • to use log4javascript loggers instead,
  • to use an injection mecanism that would allow to configure the named loggers (appender / level) outside Focus in the application.

Here is how it's done on CI-T :

Setup

Logger directory

In the application root, I added a loggers directory :

  • app
    • loggers
      • index.js
      • Spa.Application.js
      • Spa.View.js

Declaration

Each logger is declared in its own module as a singleton :

var log = log4javascript.getLogger("Spa.Application");

module.exports = log;

Configuration

The main index.js script sets up the configuration of the loggers.

/* Chargement des singletons de logger */
var logSpaApplication = require('./Spa.Application');
var logSpaView = require('./Spa.View');

/* Définition des appenders */
var consoleAppender = new log4javascript.BrowserConsoleAppender();
consoleAppender.setLayout(new log4javascript.PatternLayout("%d{HH:mm:ss,SSS} %c %p %m"));

/* Configuration des loggers */
logSpaApplication.addAppender(consoleAppender);
logSpaView.addAppender(consoleAppender);

Initialization

In the application intialize script, the first thing to do is to call the loggers main script :

var initialize = {
    init: function init(params) {
        return $(function () {

            /* Init loggers. */
            require('./loggers/index');            

            /* ... */

            /* Initialize application. */
            application.initialize();
        });
    }
};

module.exports = initialize;

Usage

var log = require('./loggers/Spa.Application');
log.info("Démarrage SPA...");

Result in Chrome console :
capture

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions