1 /** Licensed Materials - Property of IBM, 5724-E76 and 5724-E77, (C) Copyright IBM Corp. 2011, 2012 - All Rights reserved. **/ 2 (function(){ 3 /** 4 * Support for frames 5 */ 6 var i$ = window.i$; 7 8 /** 9 * Entry point for accessing functions that provide logging and tracing capabilities. 10 * @name i$.log 11 * @namespace Contains static functions and constants related to logging capabilities 12 */ 13 var __log = i$.log = {}; 14 15 var _loggers = []; 16 17 // helper functions 18 var _substitute = function(template, map){ 19 return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, 20 function(match, key, format){ // format is not supported 21 var value = map[key];//lang.getObject(key, false, map); 22 return value; 23 }); // String 24 }; 25 26 /** 27 * A Logger object is used to log messages for a specific system or application component. Loggers are 28 * normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, 29 * but they should normally be based on the package name or class name of the logged component, 30 * such as <tt>com.ibm.wps.component</tt> or <tt>com.ibm.wps.component.Class</tt>. 31 * <br/><br/> 32 * Logger objects may be obtained by calls to i$.getLogger factory methods. 33 * These will either create a new Logger or return a suitable existing Logger. 34 * <br/><br/> 35 * Logging messages will be forwarded to registered Handler objects, which can forward the messages to 36 * a variety of destinations, including consoles, files, OS logs, etc. 37 * <br/><br/> 38 * Each Logger keeps track of a "parent" Logger, which is its nearest existing ancestor in the 39 * Logger namespace. The default Enabler logging framework uses only one parent logger, the "root Logger" 40 * <br/><br/> 41 * On each logging call the Logger initially performs a cheap check of the request level 42 * (e.g. <tt>SEVERE</tt> or <tt>TRACE</tt>) against the effective global log level for the logger. 43 * If the request level is lower than the log level, the logging call returns immediately. 44 * <br/><br/> 45 * After passing this initial (cheap) test, the Logger will publish a the specified paramters to its 46 * output Handlers. By default, if the logger is not the root logger, it also publishes to it's parent's Handlers. 47 * <br/><br/> 48 * Most of the logger output methods take a "message" argument. 49 * This message argument may be either a raw value or a format string. During formatting, the format 50 * string will be filled in with the specified parameters. Otherwise the original message string is used. 51 * Formatting parameters: A format string "<tt>${0} ${1}</tt>" would format two parameters as strings. 52 * <br/><br/> 53 * A global JS object can be used to configure the loggers that are considered for tracing. <br /> 54 * This object is named <code>traceConfig</code> and can be accessed through the 55 * {@link com.ibm.mashups.enabler.services.ConfigService ConfigService} or initialized by <tt>ConfigService.properties</tt><br/><br/> 56 * <pre>[ 57 * "com.ibm.mashups.*",</br> 58 * "com.ibm.mm.*" 59 * ]</pre><br />Additional to the classes that should be logged the <code>isDebug</code> Flag must be set to <code>true</code>. 60 * 61 * <p><b>Please note: For faster access this object has been made available directly on i$ as i$.Logger.</b></p> 62 * 63 * @name i$.log.Logger 64 * @class Class representing a Logger 65 * 66 * @constructor 67 */ 68 i$.Logger = function(name) { 69 // make sure those two are really null 70 this.name = name?name:null; 71 }; 72 __log.Logger = i$.Logger; 73 74 i$.mash(__log, /** @lends i$.log*/{ 75 /** 76 * TRACE is a message level providing tracing information.<br/><br/> 77 * TRACE is intended for relatively detailed tracing. The exact meaning of the this levels will vary 78 * between subsystems. 79 * In general the TRACE level should be used for information that will be broadly interesting to developers 80 * who do not have a specialized interest in the specific subsystem.<br/><br/> 81 * TRACE messages might include things like minor (recoverable) failures. Issues indicating potential 82 * performance problems are also worth logging as FINE. This level is initialized to <code>500</code>. 83 * @type {int} 84 */ 85 LEVEL_TRACE: 500, 86 87 /** 88 * INFO is a message level for informational messages.<br/><br/> 89 * Typically INFO messages will be written to the console or its equivalent (like the system messages widget). 90 * So the INFO level should only be used for reasonably significant messages that will make sense 91 * to end users and system admins. This level is initialized to <code>800</code>. 92 * @type {int} 93 */ 94 LEVEL_INFO: 800, 95 96 /** 97 * WARNING is a message level indicating a potential problem.<br/><br/> 98 * In general WARNING messages should describe events that will be of interest to end users or system 99 * managers, or which indicate potential problems. This level is initialized to <code>900</code>. 100 * @type {int} 101 */ 102 LEVEL_WARNING: 900, 103 104 /** 105 * SEVERE is a message level indicating a serious failure.<br/><br/> 106 * In general SEVERE messages should describe events that are of considerable importance and which 107 * will prevent normal program execution. They should be reasonably intelligible to end users and to 108 * system administrators. This level is initialized to <code>1000</code>. 109 * @type {int} 110 */ 111 LEVEL_SEVERE: 1000 112 113 }); 114 115 var __LEVEL_INFO = __log.LEVEL_INFO; 116 var __LEVEL_WARNING = __log.LEVEL_WARNING; 117 var __LEVEL_SEVERE = __log.LEVEL_SEVERE; 118 119 i$.mash(__log, /** @lends i$*/ { 120 121 /** 122 * Find or create a logger for a named subsystem. If a logger has already been created with the given name 123 * it is returned. Otherwise a new logger is created. 124 * 125 * <p><b>Please note: For faster access this object has been made available directly on i$ as i$.getLogger.</b></p> 126 * 127 * @memberOf i$.log 128 * @name getLogger 129 * @function 130 * @param {String} loggerName A name for the logger. This should be a dot-separated name and should 131 * normally be based on the package name or class name of the subsystem, 132 * such as com.ibm.wps.component or com.ibm.wps.component.Class 133 * @return {i$.log.Logger} a suitable Logger 134 */ 135 getLogger: function(loggerName) { 136 // since we are here we have no parent and need to handle everything by ourself 137 if (!_loggers[loggerName]) { 138 _loggers[loggerName] = new i$.Logger(loggerName); 139 } 140 return _loggers[loggerName]; 141 }, 142 setTraceConfig: function(traceConfig, setAsCookie) { 143 console.log("IMPORTANT: In order to enable tracing you need to configure module wp_client_tracing to be downloaded."); 144 } 145 }); 146 i$.getLogger = i$.log.getLogger; 147 i$.setTraceConfig = i$.log.setTraceConfig; 148 149 i$.Logger.prototype = /** @lends i$.Logger.prototype*/{ 150 151 /** 152 * This method allows components to log an <tt>INFO</tt> level event. 153 * @param {String} methodName the name of the method . 154 * @param {String} message the message that you want to log. 155 * @param {Object[]} args array of arguments that's passed into the method. Those arguments will be filled into the wildcards in your message. 156 * @type {void} 157 */ 158 info: function(methodName, message, args) { 159 this.log(__LEVEL_INFO, methodName, message, args); 160 }, 161 /** 162 * This method allows components to log an <tt>WARNING</tt> level event. 163 * @param {String} methodName the name of the method . 164 * @param {String} message the message that you want to log. 165 * @param {Object[]} args array of arguments that's passed into the method. Those arguments will be filled into the wildcards in your message. 166 * @type {void} 167 */ 168 warning: function(methodName, message, args) { 169 this.log(__LEVEL_WARNING, methodName, message, args); 170 }, 171 /** 172 * This method allows components to log an <tt>SEVERE</tt> level event. 173 * @param {String} methodName the name of the method . 174 * @param {String} message the message that you want to log. 175 * @param {Object[]} args array of arguments that's passed into the method. Those arguments will be filled into the wildcards in your message. 176 * @type {void} 177 */ 178 severe: function(methodName, message, args) { 179 this.log(__LEVEL_SEVERE, methodName, message, args); 180 }, 181 /** 182 * This method allows components to log a logging event for the specified {@link com.ibm.mashups.enabler.logging.LogLevel LogLevel} 183 * @param {int} logLevel the {@link com.ibm.mashups.enabler.logging.LogLevel LogLevel} to use for logging. 184 * @param {String} methodName the name of the method. 185 * @param {String} message the message that you want to log. 186 * @param {Object[]} args array of arguments that's passed into the method. Those arguments will be filled into the wildcards in your message. 187 * @type {void} 188 */ 189 log: function(logLevel, methodName, message, args) { 190 // for convinience wrap single objects to an object (that user doesn't need to). 191 if ((args && !i$.isArray(args)) || args === false) { 192 args = [args]; 193 } 194 var loggerName = this.name; 195 var formattedMessage = args ? _substitute(message.toString(), args) : message; 196 var __statusBox = "ibmStatusBox"; 197 var __TOPIC = "/portal/status"; 198 var __fireEvent = i$.fireEvent; 199 var __StatusMessage = com.ibm.widgets.StatusMessage; 200 var levelString = null; 201 if (logLevel == __LEVEL_SEVERE) { 202 levelString = "error"; 203 } 204 else if (logLevel == __LEVEL_WARNING) { 205 levelString = "warning"; 206 } 207 else if (logLevel == __LEVEL_INFO) { 208 levelString = "info"; 209 } 210 if (levelString) { 211 __fireEvent(__TOPIC, [{message: new __StatusMessage(levelString, loggerName + " " + methodName + ": " + formattedMessage, ""), uid: __statusBox}]); 212 } 213 } 214 }; 215 })();