Module EDLoggingPyLogging
[hide private]
[frames] | no frames]

Source Code for Module EDLoggingPyLogging

  1  # 
  2  #    Project: The EDNA Kernel 
  3  #             http://www.edna-site.org 
  4  # 
  5  #    File: "$Id: EDImportLib.py 1453 2010-04-28 16:20:46Z svensson $" 
  6  # 
  7  #    Copyright (C) 2010-2010 European Synchrotron Radiation Facility 
  8  #                            Grenoble, France 
  9  # 
 10  #    Principal authors: Olof Svensson (svensson@esrf.fr)  
 11  #                       Mark Basham (Mark.Basham@diamon.ac.uk) 
 12  # 
 13  #    This program is free software: you can redistribute it and/or modify 
 14  #    it under the terms of the GNU Lesser General Public License as published 
 15  #    by the Free Software Foundation, either version 3 of the License, or 
 16  #    (at your option) any later version. 
 17  # 
 18  #    This program is distributed in the hope that it will be useful, 
 19  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21  #    GNU Lesser General Public License for more details. 
 22  # 
 23  #    You should have received a copy of the GNU General Public License 
 24  #    and the GNU Lesser General Public License  along with this program.   
 25  #    If not, see <http://www.gnu.org/licenses/>. 
 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   
43 -class EDLoggingPyLogging(EDObject):
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
60 - def __init__(self, _strName=None):
61 EDObject.__init__(self) 62 if _strName is None: 63 self.__loggingId = self.getClassName() 64 else: 65 self.__loggingId = _strName 66 with EDLoggingPyLogging.__semaphoreLogging: 67 if not EDLoggingPyLogging.__bInitisalised: 68 logging.basicConfig(level=EDLoggingPyLogging.__logLevel, stream=sys.stdout) 69 logging.addLevelName(EDLoggingPyLogging.UNIT_TEST_LEVEL, EDLoggingPyLogging.UNIT_TEST_NAME) 70 logging.addLevelName(EDLoggingPyLogging.ASSERT_LEVEL, EDLoggingPyLogging.ASSERT_NAME) 71 EDLoggingPyLogging.__bInitisalised = True 72 if not self.__loggingId in EDLoggingPyLogging.__dictLoggers: 73 self.logger = logging.getLogger(self.__loggingId) 74 self.logger.setLevel(EDLoggingPyLogging.__logLevel) 75 EDLoggingPyLogging.__dictLoggers[self.__loggingId] = self.logger 76 else: 77 self.logger = EDLoggingPyLogging.__dictLoggers[self.__loggingId] 78 self.__bIsTest = False 79 self.__bIsVerboseDebug = False 80 self.__strLogFileName = None 81 self.__bIsLogFile = False
82 83
84 - def setLogLevel(self, _logLevel):
85 self.logger.setLevel(_logLevel)
86 87
88 - def setAllLogLevels(self, _logLevel):
89 with EDLoggingPyLogging.__semaphoreLogging: 90 EDLoggingPyLogging.__logLevel = _logLevel 91 for logger in EDLoggingPyLogging.__dictLoggers.values(): 92 logger.setLevel(_logLevel)
93 94
95 - def setTestOn(self):
96 """ 97 turn on the test mode: all assertions become verbose (->screen) 98 """ 99 self.__bIsTest = True
100 101
102 - def setTestOff(self):
103 """ 104 turn off the test mode: all assertions become silent (->screen) 105 """ 106 self.__bIsTest = False
107 108
109 - def setVerboseOn(self):
110 """ 111 This method turns on verbose logging to standard output (stdout) 112 """ 113 self.setAllLogLevels(logging.INFO)
114 115
116 - def setVerboseOff(self):
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
123 - def setVerboseDebugOn(self):
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
131 - def setVerboseDebugOff(self):
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
139 - def isVerboseDebug(self):
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
180 - def unitTest(self, _strMessage=""):
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
241 - def writeErrorTrace(self, _strPrefix=" "):
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
261 - def setLogFileName(self, _strLogFileName):
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
271 - def setLogFileOff(self):
272 """ 273 This method truns off output to the log file. 274 """ 275 self.__bIsLogFile = False
276