ATLAS Offline Software
Control/AthenaCommon/python/Logging.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2 
3 # File: AthenaCommon/python/Logging.py
4 # Author: Wim Lavrijsen (WLavrijsen@lbl.gov)
5 
6 """Athena Python-side logging."""
7 
8 from AthenaCommon import Constants
9 
10 
11 
12 __version__ = '1.0.2'
13 __author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
14 
15 __all__ = [ 'log', 'logging', 'AthError' ]
16 
17 
18 
19 
20 import logging
21 
22 
23 
24 logging.VERBOSE = logging.DEBUG - 1
25 logging.ALL = logging.DEBUG - 2
26 logging.addLevelName( logging.VERBOSE, 'VERBOSE' )
27 logging.addLevelName( logging.ALL, 'ALL' )
28 
29 
30 
31 class AthenaLogger( logging.getLoggerClass(), object ):
32  __levelmap = { Constants.ALL : logging.ALL,
33  Constants.VERBOSE : logging.VERBOSE,
34  Constants.DEBUG : logging.DEBUG,
35  Constants.INFO : logging.INFO,
36  Constants.WARNING : logging.WARNING,
37  Constants.ERROR : logging.ERROR,
38  Constants.FATAL : logging.FATAL }
39 
40  def setLevel( self, level ):
41  try:
42  level = self.__levelmap[ level ]
43  except KeyError:
44  pass
45 
46  super( AthenaLogger, self ).setLevel( level )
47 
48  def setFormat( self, fmt = None, datefmt = None ):
49  for h in log.handlers :
50  if not fmt:
51  fmt = h.formatter.format
52  if not datefmt:
53  datefmt = h.formatter.datefmt
54 
55  h.setFormatter( logging.Formatter( fmt = fmt, datefmt = datefmt ) )
56 
57  @classmethod
58  def mapLevelLoggingToGaudi( self, level ):
59  for k,v in self.__levelmap.items():
60  if v == level:
61  return k
62  return level
63 
64  @classmethod
65  def mapLevelGaudiToLogging( self, level ):
66  try:
67  return self.__levelmap[ level ]
68  except KeyError:
69  return level
70 
71  def all( self, msg, *args, **kwargs):
72  if self.manager.disable >= logging.ALL:
73  return
74  if logging.ALL >= self.getEffectiveLevel():
75  self._log( logging.ALL, msg, args, **kwargs )
76 
77  def verbose( self, msg, *args, **kwargs):
78  if self.manager.disable >= logging.VERBOSE:
79  return
80  if logging.VERBOSE >= self.getEffectiveLevel():
81  self._log( logging.VERBOSE, msg, args, **kwargs )
82 
83 
84 # now change the Logger class and make sure to reset the 'root' logger (which
85 # is created on logging import and will otherwise have the old class)
86 logging.setLoggerClass( AthenaLogger )
87 logging.root = AthenaLogger( 'root', logging.INFO )
88 logging.Logger.root = logging.root
89 logging.Logger.manager = logging.Manager( logging.Logger.root )
90 
91 
92 
93 log = logging.getLogger()
94 def _setupLogger( log ):
95  import sys
96  log.name = "Athena"
97  hdlr = logging.StreamHandler( sys.stdout )
98  frmt = logging.Formatter( "Py:%(name)-14s%(levelname)8s %(message)s" )
99  hdlr.setFormatter( frmt )
100  for h in log.handlers: # basicConfig handler already installed?
101  if h.formatter._fmt == logging.BASIC_FORMAT:
102  log.removeHandler( h )
103  break
104  log.addHandler( hdlr )
105 _setupLogger( log )
106 del _setupLogger
107 
108 def AthError( descr ):
109  raise RuntimeError( descr ) # <== Note exception!
python.Logging.AthenaLogger.all
def all(self, msg, *args, **kwargs)
Definition: Control/AthenaCommon/python/Logging.py:71
python.Logging.AthenaLogger.mapLevelGaudiToLogging
def mapLevelGaudiToLogging(self, level)
Definition: Control/AthenaCommon/python/Logging.py:65
python.Logging.AthenaLogger.setFormat
def setFormat(self, fmt=None, datefmt=None)
Definition: Control/AthenaCommon/python/Logging.py:48
python.Logging.AthenaLogger.mapLevelLoggingToGaudi
def mapLevelLoggingToGaudi(self, level)
Definition: Control/AthenaCommon/python/Logging.py:58
python.Logging.AthenaLogger
specialized logger to allow setting with Gaudi message levels ============
Definition: Control/AthenaCommon/python/Logging.py:31
python.Logging.AthenaLogger.__levelmap
dictionary __levelmap
Definition: Control/AthenaCommon/python/Logging.py:32
python.Logging._setupLogger
def _setupLogger(log)
Definition: Control/AthenaCommon/python/Logging.py:94
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
python.Logging.AthError
def AthError(descr)
Definition: Control/AthenaCommon/python/Logging.py:108
pickleTool.object
object
Definition: pickleTool.py:30
python.Logging.AthenaLogger.verbose
def verbose(self, msg, *args, **kwargs)
Definition: Control/AthenaCommon/python/Logging.py:77
python.Logging.AthenaLogger.setLevel
def setLevel(self, level)
Definition: Control/AthenaCommon/python/Logging.py:40