WL.Logger
Module that wraps various loggers in order to provide a unified interface for logging that works in every environment.
In most environments, console.debug
, console.log
, console.info
,
console.warn
and console.error
are wrapped. The Android environment uses a cordova
plugin that calls the native Android logger, log messages are shown in logcat with the appropriate colors.
In Xcode, output is shown in the Xcode console.
Provides various features such as:
//The logger can be configured inside initOption.js, but doing so is discouraged. An example configuration:
// ...
//logger : {enabled: true, level: 'debug', stringify: true, pretty: false,
// tag: {level: false, pkg: true}},
// ...
//Make sure the MyNamespace variable exists.
var MyNamespace = MyNamespace || {};
//Define a module using the Module Pattern
MyNamespace.MyModule = (function () {
//Create a logger "instance" specific to this module.
//This is the recommended approach for logging.
var logger = WL.Logger.create({pkg: 'mynamespace.mymodule'});
//Example usage:
logger.trace('trace', 'another mesage');
logger.debug('debug', [1,2,3], {hello: 'world'});
logger.log('log', 'another message');
logger.info('info', 1, 2, 3);
logger.warn('warn', undefined);
logger.error('error', new Error('oh no'));
logger.fatal('fatal', 'another message');
}());
//Alternatively, you can use the "static" version.
//You probably want to use variables instead of duplicating
//the package name and other good programming practices.
//This is the recommended approach if you don't have modules.
WL.Logger.ctx({pkg: 'wl.whatever'}).trace('trace', 'another message');
WL.Logger.ctx({pkg: 'wl.whatever'}).debug('debug', [1,2,3], {hello: 'world'});
WL.Logger.ctx({pkg: 'wl.whatever'}).log('log', 'another message');
WL.Logger.ctx({pkg: 'wl.whatever'}).info('info', 1, 2, 3);
WL.Logger.ctx({pkg: 'wl.whatever'}).warn('warn', undefined);
WL.Logger.ctx({pkg: 'wl.whatever'}).error('error', new Error('oh no'));
WL.Logger.ctx({pkg: 'wl.whatever'}).fatal('fatal', 'another message');
//Alternatively, you do not have to pass a context.
//This is the least recommended approach.
WL.Logger.trace('trace', 'another message');
WL.Logger.debug('debug', [1,2,3], {hello: 'world'});
WL.Logger.log('log', 'another message');
WL.Logger.info('info', 1, 2, 3);
WL.Logger.warn('warn', undefined);
WL.Logger.error('error', new Error('oh no'));
WL.Logger.fatal('fatal', 'another message');
Method Attributes | Method Name and Description |
---|---|
config(options)
Configures the logger globally.
|
|
create(options)
Creates an instance of a logger with its own context (also called status or state).
|
|
ctx(options)
Updates the state (also called context or status) of the logger.
|
|
debug(message)
Prints arguments to the console.
|
|
error(message)
Prints arguments to the console.
|
|
fatal(message)
Prints arguments to the console.
|
|
info(message)
Prints arguments to the console.
|
|
log(message)
Prints arguments to the console.
|
|
metadata(options)
Attach additional metadata to the next logger instance call.
|
|
off()
|
|
on(options)
|
|
send()
Send any logs collected up to this point to the IBM Worklight server.
|
|
setNativeOptions(options)
|
|
status()
Shows the status (current configuration) of the logger.
|
|
trace(message)
Prints arguments to the console.
|
|
Retrieves and applies any matching configuration profile from the IBM Worklight Server.
|
|
warn(message)
Prints arguments to the console.
|
true
) or leaves arguments as an array (false
). Default is true.
message (string or array)
The log message. String if stringify
is true, array otherwise.level (string)
Log level.
package (string)
Package that is associated with the log or empty string.level
and tag
.
level
tag to the string log message (true) or does nothing (false). Default is false.
package name
string message (true) or does nothing (false). Default is false.
WL.Logger.config()
Merge the passed object parameter with the current configuration, which can be retrieved using WL.Logger.status()
WL.Logger.config
for information about the object that can be passed.
WL.Logger
: .trace
, .debug
, .log
,
.info
, .warn
, .error
, and .fatal
.var logger = WL.Logger.create({pkg: 'mypackage'});
logger.trace('Hello world');
//[mypackage] Hello world
logger.debug('Hello world');
//[mypackage] Hello world
logger.log('Hello world');
//[mypackage] Hello world
logger.info('Hello world');
//[mypackage] Hello world
logger.warn('Hello world');
//[mypackage] Hello world
logger.error('Hello world');
//[mypackage] Hello world
logger.fatal('Hello world');
//[mypackage] Hello world
WL.Logger.config
for information about the object that can be passed.
WL.Logger.debug('Hello world'); //No context passed
//Hello world
WL.Logger.ctx({pkg: 'hello'}).debug('Hello world'); //Package name context passed
//[hello] Hello world
Caution: filters, maxFileSize, capture, and level returned from this call may not accurately reflect the current behavior
in hybrid applications if the native codebase has modified these settings in the native application layer.
500
and a level of DEBUG
.
WL.Logger.debug('Hello world');
//Hello world
100
and a level of ERROR
.
WL.Logger.error('Hello world');
//Hello world
50
and a level of FATAL
.
WL.Logger.fatal('Hello world');
//Hello world
300
and a level of INFO
.
WL.Logger.info('Hello world');
//Hello world
400
and a level of LOG
.
WL.Logger.log('Hello world');
//Hello world
WL.Logger.metadata(
{
userRealName : 'Robert Paulson'
}
);
WL.Logger.metadata( { hi : 'world' } ).info('hello');
WL.Logger.setNativeOptions(
{
maxFileSize : 100000, // allow persistent storage of up to 100k of log data
level : 'debug', // at debug (and above) level
capture : true, // capture data passed to log API calls into persistent storage
filters : { jsonstore : 'debug' } // establish whitelist filters at native
}
);
WL.Logger.status()
.then(function (state) {
//{ enabled : true, stringify: true, filters : {},
// level : 'info', pkg : '', tag: {level: false, pkg: true} }
})
.fail(function (errMsg) {
//errMsg = error message
});
600
and a level of TRACE
.
WL.Logger.trace('Hello world');
//Hello world
200
and a level of WARN
.
WL.Logger.warn('Hello world');
//Hello world