Changeset 7276 for anuga_core/source/anuga/utilities/log.py
- Timestamp:
- Jun 30, 2009, 2:07:41 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/log.py
r7036 r7276 7 7 Use it this way: 8 8 import anuga.utilities.log as log 9 log.console_logging_level = log.DEBUG 9 10 # configure my logging 11 log.console_logging_level = log.INFO 12 log.log_logging_level = log.DEBUG 13 log.log_filename('./my.log') 14 15 # log away! 10 16 log.debug('A message at DEBUG level') 11 17 log.info('Another message, INFO level') 12 18 13 19 This class uses the 'borg' pattern - there is never more than one instance 14 of log data. See 20 of log data. See the URL for the basic idea used here: modules *are* 21 singletons! 22 15 23 <http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html> 16 for the basic idea used here: modules *are* singletons!17 24 18 25 Until the first call to log() the user is free to play with the module data 19 26 to configure the logging. 20 27 21 Note that this module uses features of the logging package that were introduced 22 in python2.5. If running on earlier versions, these features are disabled: 28 Note that this module uses some features of the logging package that were 29 introduced in python2.5. If running on earlier versions, the following 30 features are disabled: 23 31 . Calling module name + line number 24 32 ''' … … 28 36 import traceback 29 37 import logging 38 39 DefaultConsoleLogLevel = logging.CRITICAL 40 DefaultFileLogLevel = logging.INFO 30 41 31 42 … … 37 48 # the console, but ensure that everything that goes to the console *will* also 38 49 # appear in the log file. There is code to ensure log <= console levels. 50 # 51 # If console logging level is set to CRITICAL+1 then nothing will print on the 52 # console. 39 53 ################################################################################ 40 54 … … 43 57 44 58 # logging level for the console 45 console_logging_level = logging.CRITICAL59 console_logging_level = DefaultConsoleLogLevel 46 60 47 61 # logging level for the logfile 48 log_logging_level = logging.INFO62 log_logging_level = DefaultFileLogLevel 49 63 50 64 # The default name of the file to log to. … … 59 73 NOTSET = logging.NOTSET 60 74 61 # set new_python to True if python version 2.5 or later 62 (version_major, version_minor, _, _, _) = sys.version_info 63 new_python = ((version_major == 2 and version_minor >= 5) or version_major > 2) 75 # set _new_python to True if python version 2.5 or later 76 _new_python = (sys.version_info >= 0x02050000) # 2.5.x.x 64 77 65 78 … … 68 81 ################################################################################ 69 82 70 ## 71 # @brief Log a message at a specified level. 72 # @param level The loglevel to log with (logging.DEBUG, etc). 73 # @param msg Message string to log. 74 # @note First call of this method initializes the logging system. 75 def log(level, msg): 83 def log(msg, level=None): 76 84 '''Log a message at a particular loglevel. 85 86 msg: The message string to log. 87 level: The logging level to log with (defaults to console level). 77 88 78 89 The first call to this method (by anybody) initializes logging and … … 89 100 90 101 # setup the file logging system 91 if new_python:102 if _new_python: 92 103 fmt = '%(asctime)s %(levelname)-8s %(mname)25s:%(lnum)-4d|%(message)s' 93 104 else: … … 112 123 logging.getLevelName(log_logging_level), 113 124 logging.getLevelName(console_logging_level))) 114 if new_python:125 if _new_python: 115 126 logging.log(logging.CRITICAL, start_msg, 116 127 extra={'mname': __name__, 'lnum': 0}) … … 120 131 # mark module as *setup* 121 132 _setup = True 133 134 # if logging level not supplied, assume console level 135 if level is None: 136 level = console_logging_level 122 137 123 138 # get caller information - look back for first module != <this module name> … … 133 148 break 134 149 135 if new_python: 150 # why are we here? ... Oh yes! Log the message! 151 if _new_python: 136 152 logging.log(level, msg, extra={'mname': fname, 'lnum': lnum}) 137 153 else: 138 154 logging.log(level, msg) 139 155 140 ## 141 # @brief Hook function to process uncaught exceptions. 142 # @param type 143 # @param value 144 # @param traceback 145 # @note Same interface as sys.excepthook() 156 146 157 def log_exception_hook(type, value, tb): 147 msg = ''.join(traceback.format_exception(type, value, tb)) 158 '''Hook function to process uncaught exceptions. 159 160 type: Type of exception. 161 value: The exception data. 162 tb: Traceback object. 163 164 This has the same interface as sys.excepthook(). 165 ''' 166 167 msg = '\n' + ''.join(traceback.format_exception(type, value, tb)) 148 168 critical(msg) 149 169 … … 153 173 ################################################################################ 154 174 155 ##156 # @brief Shortcut for log(DEBUG, msg).157 # @param msg Message string to log at logging.DEBUG level.158 175 def debug(msg=''): 159 log(logging.DEBUG, msg)160 161 ## 162 # @brief Shortcut for log(INFO, msg). 163 # @param msg Message string to log at logging.INFO level. 176 '''Shortcut for log(DEBUG, msg).''' 177 178 log(msg, logging.DEBUG) 179 180 164 181 def info(msg=''): 165 log(logging.INFO, msg)166 167 ## 168 # @brief Shortcut for log(WARNING, msg). 169 # @param msg Message string to log at logging.WARNING level. 182 '''Shortcut for log(INFO, msg).''' 183 184 log(msg, logging.INFO) 185 186 170 187 def warning(msg=''): 171 log(logging.WARNING, msg)172 173 ## 174 # @brief Shortcut for log(ERROR, msg). 175 # @param msg Message string to log at logging.ERROR level. 188 '''Shortcut for log(WARNING, msg).''' 189 190 log(msg, logging.WARNING) 191 192 176 193 def error(msg=''): 177 log(logging.ERROR, msg)178 179 ## 180 # @brief Shortcut for log(CRITICAL, msg). 181 # @param msg Message string to log at logging.CRITICAL level. 194 '''Shortcut for log(ERROR, msg).''' 195 196 log(msg, logging.ERROR) 197 198 182 199 def critical(msg=''): 183 log(logging.CRITICAL, msg) 184 185 ## 186 # @brief Log memory usage at time of call. 187 # @param level Override the default INFO logging level. 188 # @note From http://code.activestate.com/recipes/286222/. 200 '''Shortcut for log(CRITICAL, msg).''' 201 202 log(msg, logging.CRITICAL) 203 204 189 205 def resource_usage(level=logging.INFO): 190 206 '''Log memory usage at given log level.''' 191 207 208 _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024, 209 'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024} 210 192 211 if sys.platform != 'win32': 193 212 _proc_status = '/proc/%d/status' % os.getpid() 194 _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024, 195 'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024} 196 213 197 214 def _VmB(VmKey): 198 215 '''Get number of virtual bytes used.''' … … 233 250 % (memory()/_scale['MB'], resident()/_scale['MB'], 234 251 stacksize()/_scale['MB'])) 235 log( level, msg)252 log(msg, level) 236 253 else: 237 msg = ('Sorry, no memory statistics for Windows (yet).') 238 log(level, msg) 239 254 # Windows code from: http://code.activestate.com/recipes/511491/ 255 try: 256 import ctypes 257 import _winreg 258 except: 259 log(level, 'Windows resource usage not available') 260 return 261 262 kernel32 = ctypes.windll.kernel32 263 c_ulong = ctypes.c_ulong 264 c_ulonglong = ctypes.c_ulonglong 265 class MEMORYSTATUSEX(ctypes.Structure): 266 _fields_ = [('dwLength', c_ulong), 267 ('dwMemoryLoad', c_ulong), 268 ('ullTotalPhys', c_ulonglong), 269 ('ullAvailPhys', c_ulonglong), 270 ('ullTotalPageFile', c_ulonglong), 271 ('ullAvailPageFile', c_ulonglong), 272 ('ullTotalVirtual', c_ulonglong), 273 ('ullAvailVirtual', c_ulonglong), 274 ('ullAvailExtendedVirtual', c_ulonglong) 275 ] 276 277 memoryStatusEx = MEMORYSTATUSEX() 278 memoryStatusEx.dwLength = ctypes.sizeof(MEMORYSTATUSEX) 279 kernel32.GlobalMemoryStatusEx(ctypes.byref(memoryStatusEx)) 280 281 msg = ('Resource usage: total memory=%.1fMB free memory=%.1fMB' 282 % (memoryStatusEx.ullTotalPhys/_scale['MB'], 283 memoryStatusEx.ullAvailPhys/_scale['MB'])) 284 log(msg, level) 285 286 287 ################################################################################ 240 288 241 289 if __name__ == '__main__': 242 ## critical('Testing exception capturing') 290 critical('#' * 80) 291 warning('Test of logging...') 292 log('CRITICAL+1', CRITICAL+1) 293 log('CRITICAL', CRITICAL) 294 log('CRITICAL-1', CRITICAL-1) 295 log('CRITICAL-2', CRITICAL-2) 296 log('default - CRITICAL?') 297 243 298 def test_it(num=100): 244 299 if num > 0: … … 251 306 a = num.zeros((1000,1000), num.float) 252 307 308 info('sys.version_info=%s, _new_python=%s' 309 % (str(sys.version_info), str(_new_python))) 253 310 test_it()
Note: See TracChangeset
for help on using the changeset viewer.