Module EDLoggingPyLogging
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from __future__ import with_statement
28 """
29 EDLogging uses the standard python logging facility for providing
30 convenient methods for logging for all EDNA classes.
31 """
32
33 __authors__ = ["Olof Svensson", "Mark Basham"]
34 __contact__ = "svensson@esrf.eu"
35 __license__ = "LGPLv3+"
36 __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
37
38 import logging, sys, traceback, os
39 from EDThreading import Semaphore
40 from EDObject import EDObject
41
42
44 """
45 This class is meant to be used for all logging
46 purposes in the EDNA framework.
47 """
48 __semaphoreLogging = Semaphore()
49 __dictLoggers = {}
50 __logLevel = logging.INFO
51 __bInitisalised = False
52
53 UNIT_TEST_LEVEL = 25
54 UNIT_TEST_NAME = "UNIT_TEST"
55
56 ASSERT_LEVEL = 55
57 ASSERT_NAME = "ASSERT"
58
59
82
83
85 self.logger.setLevel(_logLevel)
86
87
93
94
96 """
97 turn on the test mode: all assertions become verbose (->screen)
98 """
99 self.__bIsTest = True
100
101
103 """
104 turn off the test mode: all assertions become silent (->screen)
105 """
106 self.__bIsTest = False
107
108
110 """
111 This method turns on verbose logging to standard output (stdout)
112 """
113 self.setAllLogLevels(logging.INFO)
114
115
117 """
118 This method turns off verbose logging to standard output (stdout)
119 """
120 self.setAllLogLevels(max([i for i in logging._levelNames if isinstance(i, int)]) + 1)
121
122
124 """
125 This method turns on debug messages to standard output and log file
126 """
127 self.__bIsVerboseDebug = True
128 self.setAllLogLevels(logging.DEBUG)
129
130
132 """
133 This method turns off debug messages to standard output and log file
134 """
135 self.__bIsVerboseDebug = False
136 self.setAllLogLevels(logging.INFO)
137
138
140 """
141 This method returns the current status of debugging
142
143 @return: if debug output to standard output and log file is enabled.
144 @type: boolean
145 """
146 return self.__bIsVerboseDebug
147
148
149 - def log(self, _strMessage=""):
150 """
151 This method writes a message only to the log file.
152
153 @param _strMessage: The string to be written to the log file
154 @type _strMessage: python string
155 """
156 self.screen(_strMessage)
157
158
159 - def screen(self, _strMessage=""):
160 """
161 This method writes a message to standard output and to the log file.
162
163 @param _strMessage: The string to be written to the log file
164 @type _strMessage: python string
165 """
166 self.logger.info(_strMessage)
167
168
169 - def DEBUG(self, _strDebugMessage=""):
170 """
171 This method writes a debug message to standard output and to the log file
172 if debugging is enabled. The message will be written with the prefix [DEBUG]
173
174 @param _strDebugMessage: The debug message to be written to standard output and log file
175 @type _strDebugMessage: python string
176 """
177 self.logger.debug(_strDebugMessage)
178
179
181 """
182 This method is meant to be used by the testing framework. The message will be written
183 to standard output and the log file with the prefix [UnitTest]
184
185 @param _strMessage: The message to be written to standard output and log file
186 @type _strMessage: python string
187 """
188 self.logger.log(EDLoggingPyLogging.UNIT_TEST_LEVEL, _strMessage)
189
190
191 - def ERROR(self, _strMessage=""):
192 """
193 This method writes a message to standard error and the log file with the prefix [ERROR].
194
195 @param _strMessage: The error message to be written to standard output and log file
196 @type _strMessage: python string
197 """
198 self.logger.error(_strMessage)
199
200
201 - def error(self, _strMessage=""):
202 """
203 This method writes a message to standard error and the log file with the prefix [ERROR].
204
205 @param _strMessage: The error message to be written to standard output and log file
206 @type _strMessage: python string
207 """
208 self.ERROR(_strMessage)
209
210
211 - def WARNING(self, _strMessage=""):
212 """
213 This method writes a warning message to standard output and the log file with the prefix [Warning].
214
215 @param _strMessage: The error message to be written to standard output and log file
216 @type _strMessage: python string
217 """
218 self.logger.warning(_strMessage)
219
220
221 - def warning(self, _strMessage=""):
222 """
223 This method writes a warning message to standard output and the log file with the prefix [Warning].
224
225 @param _strMessage: The error message to be written to standard output and log file
226 @type _strMessage: python string
227 """
228 self.WARNING(_strMessage)
229
230
231 - def ASSERT(self, _strMessage):
232 """
233 This method writes an assert message to standard output and the log file with the prefix [ASSERT].
234
235 @param _strMessage: The error message to be written to standard output and log file
236 @type _strMessage: python string
237 """
238 self.logger.log(EDLoggingPyLogging.ASSERT_LEVEL, _strMessage)
239
240
242 """
243 This method writes an error trace to standard output and the log file. The error trace has
244 the same formatting as normal Python error traces.
245
246 @param _strPrefix: A prefix which can be customized, e.g. the testing framework uses ' [UnitTest]'
247 @type _strPrefix: python string
248 """
249 (exc_type, exc_value, exc_traceback) = sys.exc_info()
250 pyListTrace = traceback.extract_tb(exc_traceback)
251 self.logger.error(_strPrefix + "Traceback (most recent call last):\n")
252 for pyListLine in pyListTrace:
253 self.logger.error(_strPrefix + " File \"%s\", line %d, in %s\n" % (pyListLine[0],
254 pyListLine[1],
255 pyListLine[2]))
256 self.logger.error(_strPrefix + " " + pyListLine[3] + os.linesep)
257 strErrorMessage = traceback.format_exception_only(exc_type, exc_value)[0][:-1]
258 self.logger.error(_strPrefix + strErrorMessage + os.linesep)
259
260
262 """
263 This method can be used for customising the file name of the log file.
264
265 @param _strLogFileName: A file name for the log file.
266 @type _strLogFileName: python string
267 """
268 self.__strLogFileName = _strLogFileName
269
270
272 """
273 This method truns off output to the log file.
274 """
275 self.__bIsLogFile = False
276