/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Logging.py
Go to the documentation of this file.
00001 # Stolen from:
00002 # File: AthenaCommon/python/Logging.py
00003 # Author: Wim Lavrijsen (WLavrijsen@lbl.gov)
00004 # Incorporated to Daya Bay by bv@bnl.gov on Thu Sep 18 12:16:49 2008
00005 
00006 """NuWa Python-side logging."""
00007 
00008 import sys
00009 import Constants as Constants
00010 
00011 
00012 ### data _____________________________________________________________________
00013 __version__ = '1.0.2'
00014 __author__  = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
00015 
00016 __all__ = [ 'log', 'logging', 'NuWaError' ]
00017 
00018 
00019 
00020 ### basic logging and messages -----------------------------------------------
00021 import logging
00022 
00023 
00024 ### configure logger module --------------------------------------------------
00025 logging.VERBOSE = logging.DEBUG - 1
00026 logging.ALL     = logging.DEBUG - 2
00027 logging.addLevelName( logging.VERBOSE, 'VERBOSE' )
00028 logging.addLevelName( logging.ALL, 'ALL' )
00029 
00030 
00031 ### specialized logger to allow setting with Gaudi message levels ============
00032 class NuWaLogger( logging.getLoggerClass(), object ):
00033    __levelmap = { Constants.ALL      : logging.ALL,
00034                   Constants.VERBOSE  : logging.VERBOSE,
00035                   Constants.DEBUG    : logging.DEBUG,
00036                   Constants.INFO     : logging.INFO,
00037                   Constants.WARNING  : logging.WARNING,
00038                   Constants.ERROR    : logging.ERROR,
00039                   Constants.FATAL    : logging.FATAL }
00040 
00041    def setLevel( self, level ):
00042       try:
00043          level = self.__levelmap[ level ]
00044       except KeyError:
00045          pass
00046 
00047       super( NuWaLogger, self ).setLevel( level )
00048 
00049    def setFormat( self, fmt = None, datefmt = None ):
00050       for h in log.handlers :
00051          if not fmt:
00052             fmt = h.formatter.format
00053          if not datefmt:
00054             datefmt = h.formatter.datefmt
00055 
00056          h.setFormatter( logging.Formatter( fmt = fmt, datefmt = datefmt ) )
00057 
00058    @classmethod
00059    def mapLevelLoggingToGaudi( self, level ):
00060       for k,v in self.__levelmap.items():
00061          if v == level:
00062             return k
00063       return level
00064 
00065    @classmethod
00066    def mapLevelGaudiToLogging( self, level ):
00067       try:
00068          return self.__levelmap[ level ]
00069       except KeyError:
00070          return level
00071 
00072    def all( self, msg, *args, **kwargs):
00073       if self.manager.disable >= logging.ALL:
00074          return
00075       if logging.ALL >= self.getEffectiveLevel():
00076          apply(self._log, (logging.ALL, msg, args), kwargs)
00077 
00078    def verbose( self, msg, *args, **kwargs):
00079       if self.manager.disable >= logging.VERBOSE:
00080          return
00081       if logging.VERBOSE >= self.getEffectiveLevel():
00082          apply(self._log, (logging.VERBOSE, msg, args), kwargs)
00083 
00084 
00085 # now change the Logger class and make sure to reset the 'root' logger (which
00086 # is created on logging import and will otherwise have the old class)
00087 logging.setLoggerClass( NuWaLogger )
00088 logging.root = NuWaLogger( 'root', logging.WARNING )
00089 logging.Logger.root = logging.root
00090 logging.Logger.manager = logging.Manager( logging.Logger.root )
00091 
00092 
00093 ### configure logger ---------------------------------------------------------
00094 log = logging.getLogger()
00095 log.name = "NuWa"
00096 hdlr = logging.StreamHandler( sys.stdout )
00097 frmt = logging.Formatter( "Py:%(name)-14s%(levelname)8s %(message)s" )
00098 hdlr.setFormatter( frmt )
00099 for h in log.handlers:                 # basicConfig handler already installed?
00100    if h.formatter._fmt == logging.BASIC_FORMAT:
00101       log.removeHandler( h )
00102       break
00103 log.addHandler( hdlr )
00104 
00105 def NuWaError( descr ):
00106    raise RuntimeError( descr )               # <== Note exception!
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:55:40 for DybPython by doxygen 1.7.4