Logger(optionsopt) → {object}

new Logger(optionsopt) → {object}

JavaScript logger singleton to handle errors the same way

The Logger class provides a singleton object to allow brain dead logging for frontend JavaScript code. Errors can be raised from JavaScript errors (new Error()), or using a custom error format, with a severity, title and message. It is also possible to pass a notification manager object to handle those error either in console and in UI. The recommended manager to use for notification can be found at Notification.js. You can otherwise implement you system, but it as to take a type (severity), a title and a message ; for further information, refer to the _logErrorToNotification documentation. For source code, please go to Logger.js
Parameters:
Name Type Attributes Default Description
options object <optional>
{}

The Logger object, not mandatory but it is recommended to provide one for full features

Properties
Name Type Attributes Default Description
errors object <optional>
{}

The custom errors, JSON style, with key being the error name and value being an object with a severity, a title and a message property (all strings)

notification object <optional>
null

The notification manager (to create new notifications when logging)

log boolean <optional>
true

Allow console logging (turn to false in prod environment)

Returns:
object -
  • The Logger singleton instance
Since:
  • June 2020
Author:
  • Arthur Beaulieu

Members

private _errors :object

The error messages to use in Logger

private _log :boolean

Internal logging flag from constructor options, allow to output each event action

private _notification :object

The custom notification handler, must be able to take type, title and message (at least)

version :string

Component version

Methods

private, static _buildErrorInfo(error) → {object}

This method will be the error properties according to its type. A custom error will take values defined at construction of this singleton. JavaScrip error are parsed to extract title and message properties from stack, with specific handling for Chrome and Firefox.
Parameters:
Name Type Description
error object

The error to build info from. Can be a custom error or a standard JavaScript error

Returns:
object -
  • The error properties ; severity, title and message

private, static _getCallerName(browsers) → {string}

This method will build the caller name as a string, formatted to be easy to read and display in the log output.
Parameters:
Name Type Description
browsers object

An object with booleans values for current browser used by session

Returns:
string -
  • The Logger standard caller name regardless the browser

private, static _logErrorToConsole(errorParameters)

This method will send error to console if logging has been allowed to this singleton constructor. It takes a Logger standard error (severity, title and message) as argument. It will build a unified output regardless the Chrome or Firefox browser. It enhance console.log and console.info to also display the stack trace in a console.group.
Parameters:
Name Type Description
errorParameters object

The error with Logger standard properties (severity, title and message)

private, static _logErrorToNotification(errorParameters)

This method will call for a new notification if a component has been given to this singleton constructor. The component must expose a new() methods that takes as arguments the Logger standard properties ; severity, title and message. If no component has be provided, this method won't do anything. One can find such component here.
Parameters:
Name Type Description
errorParameters object

The error with Logger standard properties (severity, title and message)

static destroy()

Logger destructor. Will delete singleton instance and its properties.

static raise(error)

The raise method will build, according to argument sent to this singleton constructor, a console output and/or a notification for the given error. The input error can be a standard JavaScript error, raised like new Error(), but can also be build using the custom format, using the key of the error as input string. See constructor and example for demonstration.
Parameters:
Name Type Description
error object

The error to handle. Can be a custom error or a standard JavaScript error