Changeset 7140


Ignore:
Timestamp:
Jun 1, 2009, 8:52:39 AM (15 years ago)
Author:
rwilson
Message:

Slight change to logging to shorten common usage code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/utilities/log.py

    r7095 r7140  
    3636import traceback
    3737import logging
     38
     39DefaultConsoleLogLevel = logging.CRITICAL
     40DefaultFileLogLevel = logging.INFO
    3841
    3942
     
    4548# the console, but ensure that everything that goes to the console *will* also
    4649# 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.
    4753################################################################################
    4854
     
    5157
    5258# logging level for the console
    53 console_logging_level = logging.CRITICAL
     59console_logging_level = DefaultConsoleLogLevel
    5460
    5561# logging level for the logfile
    56 log_logging_level = logging.INFO
     62log_logging_level = DefaultFileLogLevel
    5763
    5864# The default name of the file to log to.
     
    7581################################################################################
    7682
    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):
     83def log(msg, level=None):
    8384    '''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).
    8488
    8589    The first call to this method (by anybody) initializes logging and
     
    128132        _setup = True
    129133
     134    # if logging level not supplied, assume console level
     135    if level is None:
     136        level = console_logging_level
     137
    130138    # get caller information - look back for first module != <this module name>
    131139    frames = traceback.extract_stack()
     
    146154        logging.log(level, msg)
    147155
    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
    154157def 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
    155167    msg = '\n' + ''.join(traceback.format_exception(type, value, tb))
    156168    critical(msg)
     
    161173################################################################################
    162174
    163 ##
    164 # @brief Shortcut for log(DEBUG, msg).
    165 # @param msg Message string to log at logging.DEBUG level.
    166175def 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
    172181def 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
    178187def 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
    184193def 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
    190199def 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
    197205def resource_usage(level=logging.INFO):
    198206    '''Log memory usage at given log level.'''
     
    242250               % (memory()/_scale['MB'], resident()/_scale['MB'],
    243251                  stacksize()/_scale['MB']))
    244         log(level, msg)
     252        log(msg, level)
    245253    else:
    246254        # Windows code from: http://code.activestate.com/recipes/511491/
     
    274282               % (memoryStatusEx.ullTotalPhys/_scale['MB'],
    275283                  memoryStatusEx.ullAvailPhys/_scale['MB']))
    276         log(level, msg)
    277 
     284        log(msg, level)
     285
     286
     287################################################################################
    278288
    279289if __name__ == '__main__':
    280290    critical('#' * 80)
    281291    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
    282298    def test_it(num=100):
    283299        if num > 0:
Note: See TracChangeset for help on using the changeset viewer.