Module logger
[hide private]

Source Code for Module logger

  1  # coding=utf-8 
  2  """ 
  3  This library contains log utilities and helper functions for error reporting. 
  4  It contains APIs to log the at debug, info, and error levels. 
  5   
  6  The logs configuration is in the probeMgrLog4j.properties file. By default, the messages from the log level 
  7  and up are written to probeMgr-patternsDebug.log in the discovery probe log folder. The info and error messages 
  8  are also displayed on the console. 
  9   
 10  There are two sets of APIs: 
 11   - logger.<debug/info/warn/error> 
 12   - logger.<debugException/infoException/warnException/errorException> 
 13  The first set issues the concatenation of all its string arguments in the appropriate log level. 
 14  The second does the same along with the most recent thrown exception's stack trace. This facilitates 
 15  understanding the cause of the exception. 
 16  """ 
 17   
 18  import traceback 
 19  import sys 
 20  from org.apache.log4j import Category 
 21  from java.lang import Exception as JException 
 22  from com.hp.ucmdb.discovery.library.clients import ScriptsExecutionManager 
 23   
 24  cat = Category.getInstance('PATTERNS_DEBUG') 
 25   
 26   
27 -def _getFramework():
28 return ScriptsExecutionManager.getFramework()
29 30
31 -def addLog(level, msg, stackTrace=None):
32 ScriptsExecutionManager.addExecutionRecordLog(level, msg)
33 34
35 -def join(msg, *args):
36 coerce_ = lambda x: type(x) in [type(u''), type('')] and x or str(x) 37 msgUnicode = coerce_(msg) 38 if args: 39 msgUnicode = msgUnicode + u''.join(map(coerce_, args)) 40 return msgUnicode
41 42
43 -def debug(msg, *args):
44 """ 45 Logs a debug message if debug messages are enabled. 46 @param msg: the log message 47 @type msg: string 48 @param args: additional variables to log 49 """ 50 51 if isDebugEnabled(): 52 message = join(msg, *args) 53 cat.debug(message) 54 addLog('debug', message)
55 56
57 -def info(msg, *args):
58 """ 59 Logs an info level message if info messages are enabled. 60 @param msg: the log message 61 @type msg: string 62 @param args: additional variables to log 63 """ 64 65 if isInfoEnabled(): 66 message = join(msg, *args) 67 cat.info(message) 68 addLog('info', message)
69 70
71 -def warn(msg, *args):
72 """ 73 Logs a warning message if warn messages are enabled. 74 @param msg: the log message 75 @type msg: string 76 @param args: additional variables to log 77 """ 78 79 message = join(msg, *args) 80 cat.warn(message) 81 addLog('warn', message)
82 83
84 -def error(msg, *args):
85 """ 86 Logs an error message if debug messages. 87 @param msg: the log message 88 @type msg: string 89 @param args: additional variables to log 90 """ 91 92 message = join(msg, *args) 93 cat.error(message) 94 addLog('error', message)
95 96
97 -def fatal(msg, *args):
98 """ 99 Logs a fatal message. 100 @param msg: the log message 101 @type msg: string 102 @param args: additional variables to log 103 """ 104 105 message = join(msg, *args) 106 cat.fatal(message) 107 addLog('fatal', message)
108 109
110 -def isDebugEnabled():
111 """ 112 Checks whether DEBUG messages are configured to be written to the log file. 113 The method checks in thel log4j properties file. 114 @return: true if the debug messages should be written, false otherwise 115 @rtype: boolean 116 """ 117 118 return cat.isDebugEnabled()
119 120
121 -def isInfoEnabled():
122 """ 123 Checks whether INFO messages should be written to the log file, acording to 124 The method checks in thel log4j properties file. 125 @return: true if the info messages should be written, false otherwise 126 @rtype: boolean 127 """ 128 129 return cat.isInfoEnabled()
130 131
132 -def prepareJythonStackTrace(msg, *args):
133 exc_info = sys.exc_info() 134 stacktrace = traceback.format_exception(exc_info[0], exc_info[1], exc_info[2]) 135 resultMessage = join(msg, *args) + '\n' + ''.join(map(str, stacktrace)) 136 return resultMessage
137 138
139 -def prepareJavaStackTrace():
140 dataObj = sys.exc_info()[1] 141 if type(dataObj.__class__).__name__ == 'org.python.core.PyJavaClass': 142 if JException.getClass().isAssignableFrom(dataObj.__class__): 143 return dataObj 144 return None
145 146
147 -def prepareFullStackTrace(msg, *args):
148 """ 149 Returns the full exception stack trace from both Java and Jython. 150 @param msg: the log message 151 @type msg: string 152 @param args: additional variables to log 153 @return: full Stack Trace string (both java and jython) 154 @rtype: string 155 """ 156 157 exc_info = sys.exc_info() 158 stacktrace = traceback.format_exception(exc_info[0], exc_info[1], exc_info[2]) 159 resultMessage = join(msg, *args) + ''.join(map(str, stacktrace)) 160 javaStacktrace = prepareJavaStackTrace() 161 if (javaStacktrace != None): 162 resultMessage = resultMessage + '\nJava stackTrace:\n' + str(javaStacktrace) 163 return resultMessage
164 165
166 -def debugException(msg, *args):
167 """ 168 Writes the full exception stack trace from both Java and Jython to the log at the debug level. 169 @param msg: the log message 170 @type msg: string 171 @param args: additional variables to log 172 """ 173 174 if (cat.isDebugEnabled()): 175 message = prepareJythonStackTrace(msg, *args) 176 srtackTrace = prepareJavaStackTrace() 177 cat.debug(message, srtackTrace) 178 addLog('debug', message, srtackTrace)
179 180
181 -def infoException(msg, *args):
182 """ 183 Writes the full exception stack trace from both Java and Jython to the log at the info level. 184 @param msg: the log message 185 @type msg: string 186 @param args: additional variables to log 187 """ 188 189 if (cat.isInfoEnabled()): 190 message = prepareJythonStackTrace(msg, *args) 191 srtackTrace = prepareJavaStackTrace() 192 cat.info(message, srtackTrace) 193 addLog('info', message, srtackTrace)
194 195
196 -def warnException(msg, *args):
197 """ 198 Writes the full exception stack trace from both Java and Jython to the log at the warn level. 199 @param msg: the log message 200 @type msg: string 201 @param args: additional variables to log 202 """ 203 204 message = prepareJythonStackTrace(msg, *args) 205 srtackTrace = prepareJavaStackTrace() 206 cat.warn(message, srtackTrace) 207 addLog('warn', message, srtackTrace)
208 209
210 -def getCauseMessagesFromJavaStacktrace(stacktrace):
211 r'@types: Throwable > list[str]' 212 causeMessages = [] 213 while stacktrace: 214 try: 215 # some 3rd party Exception classes may 216 # raise exception on getMessage method 217 message = stacktrace.getMessage() 218 except: 219 causeMessages.append(message) 220 try: 221 stacktrace = stacktrace.getCause() 222 except: 223 stacktrace = None 224 return map(str, filter(None, causeMessages))
225 226
227 -def warnCompactException(msg, *args):
228 r'Warns message along with exception details that are messages of cases' 229 stacktrace = prepareJavaStackTrace() 230 causeMessages = stacktrace and getCauseMessagesFromJavaStacktrace(stacktrace) 231 shortenedTrace = causeMessages and "Caused by:\n%s" % ( 232 '\n'.join(causeMessages)) 233 addLog('warn', msg, shortenedTrace or '')
234 235
236 -def errorException(msg, *args):
237 """ 238 Writes the full exception stack trace from both Java and Jython to the log at the error level. 239 @param msg: the log message 240 @type msg: string 241 @param args: additional variables to log 242 """ 243 244 message = prepareJythonStackTrace(msg, *args) 245 stackTrace = prepareJavaStackTrace() 246 cat.error(message, stackTrace) 247 addLog('error', message, stackTrace)
248 249
250 -def fatalException(msg, *args):
251 """ 252 Writes the full exception stack trace from both Java and Jython to the log at the fatal level. 253 @param msg: the log message 254 @type msg: string 255 @param args: additional variables to log 256 """ 257 message = prepareJythonStackTrace(msg, *args) 258 stackTrace = prepareJavaStackTrace() 259 cat.fatal(message, stackTrace) 260 addLog('fatal', message, stackTrace)
261 262
263 -def reportWarning(warning=None):
264 if warning == None: 265 warning = str(sys.exc_info()[1]) 266 267 _getFramework().reportWarning(warning)
268 269
270 -def reportError(error=None):
271 if error == None: 272 error = str(sys.exc_info()[1]) 273 274 _getFramework().reportError(error)
275 276
277 -def reportWarningObject(warnobj):
278 if not warnobj.isEmpty(): 279 Framework = _getFramework() 280 __reportWarning(warnobj.errCode, warnobj.params, warnobj.errMsg, Framework) 281 if (__getVersion(Framework) >= 8.5): 282 debug("Reporting warning code " + str(warnobj.errCode) + " to framework.") 283 debug("Warning message is: " + warnobj.errMsg)
284 285
286 -def reportErrorObject(errobj):
287 if not errobj.isEmpty(): 288 Framework = _getFramework() 289 __reportError(errobj.errCode, errobj.params, errobj.errMsg, Framework) 290 if (__getVersion(Framework) >= 8.5): 291 debug("Reporting error code " + str(errobj.errCode) + " to framework.") 292 debug("Error message is: " + errobj.errMsg)
293 294
295 -def __reportWarning(errorCode, params, errmsg, framework):
296 if (__getVersion(framework) >= 8.5): 297 framework.reportWarning(errorCode, params) 298 else: 299 # Make sure errmsg is not None (in case logger fetched None from sys.exc_info()[1]) 300 if ((errmsg is None) or (len(errmsg) == 0)): 301 errmsg = "Unknown Warning" 302 framework.reportWarning(errmsg)
303 304
305 -def __reportError(errorCode, params, errmsg, framework):
306 if (__getVersion(framework) >= 8.5): 307 framework.reportError(errorCode, params) 308 else: 309 # Make sure errmsg is not None (in case logger fetched None from sys.exc_info()[1]) 310 if ((errmsg is None) or (len(errmsg) == 0)): 311 errmsg = "Unknown Error" 312 framework.reportError(errmsg)
313 314
315 -class Version:
316 foundVersion = None 317
318 - def getVersion(self, framework):
319 ''' Framework -> double 320 @deprecated: use modeling.CmdbClassModel().version() instead 321 ''' 322 if (Version.foundVersion is None): 323 versionAsDouble = 8.01 # v8.01 or lower... 324 try: 325 envInfo = framework.getEnvironmentInformation() 326 # The following method was impl. in v8.02 and therefore exception will be thrown on earlier versions 327 version = envInfo.getProbeVersion() 328 versionAsDouble = version.getVersion() 329 except: 330 pass 331 Version.foundVersion = versionAsDouble 332 return Version.foundVersion
333 334
335 -def __getVersion(framework):
336 ''' Framework -> double 337 @deprecated: use modeling.CmdbClassModel().version() instead 338 ''' 339 return Version().getVersion(framework)
340