// Copyright (c) 2011, 2013, Oracle and/or its affiliates.
// All rights reserved.
/*jslint browser: true*/
/**
* @class
* @name oj.Logger
*
* @classdesc
* <h3>JET Logger</h3>
*
* <p>Logger object writes into the native browser console or a custom writer, if a custom writer is set as an option.
* To use a custom writer, implement the following writer methods: log(), info(), warn(), error()
*
* <p>When any of the logging methods is called, it compares the requested log level with the value of a log level option
* and logs the message if the log level is sufficient.
*
* <p>If the logging options are changed at a later point, the Logger will use the modified options for the subsequent log operations.
*
* <p>All the logging methods support string formatting, accept variable number of arguments and accept a function as a parameter.
* When a callback function is specified as a parameter the function will be called if the log level is sufficient.
*
* <h3> Usage : </h3>
* <pre class="prettyprint">
* <code>
* //optional calls, see defaults
* oj.Logger.option("level", oj.Logger.LEVEL_INFO);
* oj.Logger.option("writer", customWriter); //an object that implements the following methods: log(), info(), warn(), error()
*
* // logging a message
* oj.Logger.info("My log level is %d", oj.Logger.option("level")); // string formatting
* oj.Logger.warn("Beware of bugs", "in the above code"); // multiple parameters
*
* // using a callback function as a parameter
* oj.Logger.info(function(){
* var foo = "This ";
* var bar = "is ";
* var zing = "a function";
* return foo + bar + zing;
* });
* </code></pre>
*
* @desc
* oj.Logger cannot be instantiated
* @export
*/
oj.Logger = {};
/**
* Log level none
* @const
* @export
*/
oj.Logger.LEVEL_NONE = 0;
/**
* Log level error
* @const
* @export
*/
oj.Logger.LEVEL_ERROR = 1;
/**
* Log level warning
* @const
* @export
*/
oj.Logger.LEVEL_WARN = 2;
/**
* Log level info
* @const
* @export
*/
oj.Logger.LEVEL_INFO = 3;
/**
* Log level - general message
* @const
* @export
*/
oj.Logger.LEVEL_LOG = 4;
/* private constants*/
oj.Logger._METHOD_ERROR = "error";
oj.Logger._METHOD_WARN = "warn";
oj.Logger._METHOD_INFO = "info";
oj.Logger._METHOD_LOG = "log";
oj.Logger._defaultOptions = {'level': oj.Logger.LEVEL_ERROR, 'writer': null};
oj.Logger._options = oj.Logger._defaultOptions;
/*public members*/
/**
* Writes an error message.
* @param {...Object|string} args The method supports a variable number of arguments, string substitutions and accepts a function as a parameter.
* See examples in the overview section above.
* @export
*/
oj.Logger.error = function(args)
{
oj.Logger._write(oj.Logger.LEVEL_ERROR, oj.Logger._METHOD_ERROR, arguments);
};
/**
* Writes an informational message.
* @param {...Object|string} args The method supports a variable number of arguments, string substitutions and accepts a function as a parameter.
* See examples in the overview section above.
* @export
*/
oj.Logger.info = function(args)
{
oj.Logger._write(oj.Logger.LEVEL_INFO, oj.Logger._METHOD_INFO, arguments);
};
/**
* Writes a warning message.
* @param {...Object|string} args The method supports a variable number of arguments, string substitutions and accepts a function as a parameter.
* See examples in the overview section above.
* @export
*/
oj.Logger.warn = function(args)
{
oj.Logger._write(oj.Logger.LEVEL_WARN, oj.Logger._METHOD_WARN, arguments);
};
/**
* Writes a general message.
* @param {...Object|string} args The method supports a variable number of arguments, string substitutions and accepts a function as a parameter.
* See examples in the overview section above.
* @export
*/
oj.Logger.log = function(args)
{
oj.Logger._write(oj.Logger.LEVEL_LOG, oj.Logger._METHOD_LOG, arguments);
};
/**
* Method for setting and getting logger option/options
* <p>Sets/gets logger configuration - level and/or writer. Accepts variable number of arguments.
* <p><h5>Defaults:</h5>
* Default level: oj.Logger.LEVEL_ERROR<br/>
* Default writer: null; writes to the console
* <p><h5>Usages:</h5>
* <i>oj.Logger.option(optionName)</i> gets the value associated the the specified optionName<br/>
* <i>oj.Logger.option()</i> gets an object containing key/value pairs representing the logger options hash<br/>
* <i>oj.Logger.option(optionName, value)</i> sets the option value associated with optionName<br/>
* <i>oj.Logger.option(options)</i> sets one or more options for the logger
*
* @example <caption>Overriding default options</caption>
* oj.Logger.option("level", oj.Logger.LEVEL_INFO);
* oj.Logger.option("writer", customWriter); //an object that implements the following methods: log(), info(), warn(), error()
*
* @param {Object|string=} key
* @param {Object|string=} value
* @export
*/
oj.Logger.option = function (key, value)
{
//getters
var ret = {}, opt;
if (arguments.length == 0) {
for (opt in oj.Logger._options) {
if (oj.Logger._options.hasOwnProperty(opt)) {
ret[opt]=oj.Logger._options[opt];
}
}
return ret;
}
if (typeof key === "string" && value === undefined) {
return oj.Logger._options[key] === undefined ? null : oj.Logger._options[key];
}
//setters
if (typeof key === "string") {
oj.Logger._options[key] = value;
}
else { // case when all options are set in one call
var options = key;
for (opt in options) {
if (options.hasOwnProperty(opt)) {
oj.Logger.option(opt, options[opt]);
}
}
}
};
/* private members*/
/*
* Helper method - calls a specified method on the available writer (console or custom)
* if the logging level is sufficient
*/
oj.Logger._write = function(level, method, args)
{
if (oj.Logger.option("level") < level) {
return;
}
var writer = oj.Logger._getWriter();
if (writer != null) {
if (args.length == 1 && (args[0] instanceof Function)) {
var msg = args[0]();
args = [msg];
}
if (writer[method] && writer[method].apply) {
writer[method].apply(writer, args);
}
else if (writer[method]) {
writer[method] = Function.prototype.bind.call(writer[method], writer);
oj.Logger._write(level, method, args);
}
}
};
/*
* Helper method - returns available writer (console or custom)
*/
oj.Logger._getWriter = function()
{
var writer = null;
if (oj.Logger.option("writer")) {
writer = oj.Logger.option("writer");
}
else if (window !== undefined && window.console !== undefined) {
writer = window.console;
}
return writer;
};
/*
* Helper method - validates an option against default options
* Returns true if the option(key) is a default option
*/
oj.Logger._validateOption = function(key)
{
return oj.Logger._defaultOptions[key] !== undefined;
};
Source: src/main/javascript/oracle/oj/ojcore/Logger.js
Oracle® JavaScript Extension Toolkit (JET)
1.1.2
E65298-01