The log4js script

The log4js is a logger type script similar to log4j. It provides a lightweight but useful script to log events in a ITSMA Service Management script that you want to debug . It provides a means to trace information in the Message view while controlling the level of detail being logged. It allows developers to turn in-line debugging on and off.

The log4js script allows developers to set the following logging levels.

log4js log level Description
OFF Nothing is logged.
ALL Everything is logged.
DEBUG Debug information is logged.
INFO Information messages are logged (default).
WARN Warning messages are logged.
ERROR Error messages are logged.
FATAL Fatal error messages are logged.

API Details

The log4js should be initialized before it is used.

Constructor syntax:

var samplelog = new lib.log4js($loglevel, $prefix);

You can also use the following method to create an instance of the logger:

var myLogger=new lib log4js.Log(lib.log4js.Log.INFO);

Sample code snippets

myLogger.info("an info");
myLogger.warn("a warning");
myLogger.error("an error");

You can change the log level or turn off logging with:

myLogger.setLevel(lib.log4js.Log.WARN);
myLogger.setLevel(lib.log4js.Log.OFF);

You can also change the log level or turn off logging with this format:

myLogger.setLevel("WARN");
myLogger.setLevel("OFF");

The log4js script supports appending a prefix to each message. For example:

var myLogger=new lib.log4js.Log("INFO","my prefix");

Type of Logger:

Logger Description
alertLogger

Output information to Service Management Message view or the server log file (default: <SM server installation directory>\logs\sm.log), depending on whether a background or foreground process calls the log4j functions.

background: server log

foreground: Message view

API methods:

The following table lists the API methods for log4js.

Method Description
$log.debug Write a debug message.
$log.info Write an information message.
$log.warn Write a warning message.
$log.error Write an error message.
$log.fatal Write a fatal message.
$log.setLevel Set a log level.
$log.getLevel Retrieve the log level.
$log.setPrefix Set the prefix information.
$log.getPrefix Get the prefix information.
$log.getLogger Get logger information.
$log.setLogger Set the logger information.

Script name: log4js

Package name: null

Performance impact

The log4js uses the standard Service Management print method to write the Service Management server log file or the Messages view, which is useful for debugging your JavaScript, but keep in mind that when logging is turned on there is a performance impact so log4js should be used carefully.