Changeset 7140
- Timestamp:
- Jun 1, 2009, 8:52:39 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy/anuga/utilities/log.py
r7095 r7140 36 36 import traceback 37 37 import logging 38 39 DefaultConsoleLogLevel = logging.CRITICAL 40 DefaultFileLogLevel = logging.INFO 38 41 39 42 … … 45 48 # the console, but ensure that everything that goes to the console *will* also 46 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. 47 53 ################################################################################ 48 54 … … 51 57 52 58 # logging level for the console 53 console_logging_level = logging.CRITICAL59 console_logging_level = DefaultConsoleLogLevel 54 60 55 61 # logging level for the logfile 56 log_logging_level = logging.INFO62 log_logging_level = DefaultFileLogLevel 57 63 58 64 # The default name of the file to log to. … … 75 81 ################################################################################ 76 82 77 ## 78 # @brief Log a message at a specified level. 79 # @param level The loglevel to log with (logging.DEBUG, etc). 80 # @param msg Message string to log. 81 # @note First call of this method initializes the logging system. 82 def log(level, msg): 83 def log(msg, level=None): 83 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). 84 88 85 89 The first call to this method (by anybody) initializes logging and … … 128 132 _setup = True 129 133 134 # if logging level not supplied, assume console level 135 if level is None: 136 level = console_logging_level 137 130 138 # get caller information - look back for first module != <this module name> 131 139 frames = traceback.extract_stack() … … 146 154 logging.log(level, msg) 147 155 148 ## 149 # @brief Hook function to process uncaught exceptions. 150 # @param type Type of exception. 151 # @param value Exception data. 152 # @param traceback Traceback object. 153 # @note Same interface as sys.excepthook() 156 154 157 def log_exception_hook(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 155 167 msg = '\n' + ''.join(traceback.format_exception(type, value, tb)) 156 168 critical(msg) … … 161 173 ################################################################################ 162 174 163 ##164 # @brief Shortcut for log(DEBUG, msg).165 # @param msg Message string to log at logging.DEBUG level.166 175 def debug(msg=''): 167 log(logging.DEBUG, msg)168 169 ## 170 # @brief Shortcut for log(INFO, msg). 171 # @param msg Message string to log at logging.INFO level. 176 '''Shortcut for log(DEBUG, msg).''' 177 178 log(msg, logging.DEBUG) 179 180 172 181 def info(msg=''): 173 log(logging.INFO, msg)174 175 ## 176 # @brief Shortcut for log(WARNING, msg). 177 # @param msg Message string to log at logging.WARNING level. 182 '''Shortcut for log(INFO, msg).''' 183 184 log(msg, logging.INFO) 185 186 178 187 def warning(msg=''): 179 log(logging.WARNING, msg)180 181 ## 182 # @brief Shortcut for log(ERROR, msg). 183 # @param msg Message string to log at logging.ERROR level. 188 '''Shortcut for log(WARNING, msg).''' 189 190 log(msg, logging.WARNING) 191 192 184 193 def error(msg=''): 185 log(logging.ERROR, msg)186 187 ## 188 # @brief Shortcut for log(CRITICAL, msg). 189 # @param msg Message string to log at logging.CRITICAL level. 194 '''Shortcut for log(ERROR, msg).''' 195 196 log(msg, logging.ERROR) 197 198 190 199 def critical(msg=''): 191 log(logging.CRITICAL, msg) 192 193 ## 194 # @brief Log memory usage at time of call. 195 # @param level Override the default INFO logging level. 196 # @note From http://code.activestate.com/recipes/286222/. 200 '''Shortcut for log(CRITICAL, msg).''' 201 202 log(msg, logging.CRITICAL) 203 204 197 205 def resource_usage(level=logging.INFO): 198 206 '''Log memory usage at given log level.''' … … 242 250 % (memory()/_scale['MB'], resident()/_scale['MB'], 243 251 stacksize()/_scale['MB'])) 244 log( level, msg)252 log(msg, level) 245 253 else: 246 254 # Windows code from: http://code.activestate.com/recipes/511491/ … … 274 282 % (memoryStatusEx.ullTotalPhys/_scale['MB'], 275 283 memoryStatusEx.ullAvailPhys/_scale['MB'])) 276 log(level, msg) 277 284 log(msg, level) 285 286 287 ################################################################################ 278 288 279 289 if __name__ == '__main__': 280 290 critical('#' * 80) 281 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 282 298 def test_it(num=100): 283 299 if num > 0:
Note: See TracChangeset
for help on using the changeset viewer.