1
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
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
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
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
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
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
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
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
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
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
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
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
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
211 r'@types: Throwable > list[str]'
212 causeMessages = []
213 while stacktrace:
214 try:
215
216
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
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
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
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
268
269
275
276
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
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
296 if (__getVersion(framework) >= 8.5):
297 framework.reportWarning(errorCode, params)
298 else:
299
300 if ((errmsg is None) or (len(errmsg) == 0)):
301 errmsg = "Unknown Warning"
302 framework.reportWarning(errmsg)
303
304
306 if (__getVersion(framework) >= 8.5):
307 framework.reportError(errorCode, params)
308 else:
309
310 if ((errmsg is None) or (len(errmsg) == 0)):
311 errmsg = "Unknown Error"
312 framework.reportError(errmsg)
313
314
333
334
336 ''' Framework -> double
337 @deprecated: use modeling.CmdbClassModel().version() instead
338 '''
339 return Version().getVersion(framework)
340