Changeset 7091


Ignore:
Timestamp:
May 27, 2009, 8:00:10 AM (15 years ago)
Author:
rwilson
Message:

Additio0ns to the log module.

Location:
branches/numpy/anuga/utilities
Files:
1 added
1 edited

Legend:

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

    r7088 r7091  
    77Use it this way:
    88    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!
    1016    log.debug('A message at DEBUG level')
    1117    log.info('Another message, INFO level')
    1218
    1319This class uses the 'borg' pattern - there is never more than one instance
    14 of log data.  See
     20of log data.  See the URL for the basic idea used here: modules *are*
     21singletons!
     22
    1523<http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html>
    16 for the basic idea used here: modules *are* singletons!
    1724
    1825Until the first call to log() the user is free to play with the module data
    1926to configure the logging.
    2027
    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:
     28Note that this module uses some features of the logging package that were
     29introduced in python2.5.  If running on earlier versions, the following
     30features are disabled:
    2331    . Calling module name + line number
    2432'''
     
    5967NOTSET = logging.NOTSET
    6068
    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)
     69# set _new_python to True if python version 2.5 or later
     70_new_python = (sys.version_info >= 0x02050000)      # 2.5.x.x
    6471
    6572
     
    8996
    9097        # setup the file logging system
    91         if new_python:
     98        if _new_python:
    9299            fmt = '%(asctime)s %(levelname)-8s %(mname)25s:%(lnum)-4d|%(message)s'
    93100        else:
     
    112119                        logging.getLevelName(log_logging_level),
    113120                        logging.getLevelName(console_logging_level)))
    114         if new_python:
     121        if _new_python:
    115122            logging.log(logging.CRITICAL, start_msg,
    116123                        extra={'mname': __name__, 'lnum': 0})
     
    133140            break
    134141
    135     if new_python:
     142    # why are we here? ... Oh yes! Log the message!
     143    if _new_python:
    136144        logging.log(level, msg, extra={'mname': fname, 'lnum': lnum})
    137145    else:
     
    140148##
    141149# @brief Hook function to process uncaught exceptions.
    142 # @param type
    143 # @param value
    144 # @param traceback
     150# @param type Type of exception.
     151# @param value Exception data.
     152# @param traceback Traceback object.
    145153# @note Same interface as sys.excepthook()
    146154def log_exception_hook(type, value, tb):
     
    252260
    253261    test_it()
     262    info('sys.version_info=%s, _new_python=%s'
     263         % (str(sys.version_info), str(_new_python)))
Note: See TracChangeset for help on using the changeset viewer.