19 import SimpleITK
as sitk
26 Adapts SimpleITK messages to be handled by a Python Logger object.
28 Allows using the logging module to control the handling of messages coming
29 from ITK and SimpleTK. Messages such as debug and warnings are handled by
30 objects derived from sitk.LoggerBase.
32 The LoggerBase.SetAsGlobalITKLogger method must be called to enable
33 SimpleITK messages to use the logger.
35 The Python logger module adds a second layer of control for the logging
36 level in addition to the controls already in SimpleITK.
38 The "Debug" property of a SimpleITK object must be enabled (if
39 available) and the support from the Python "logging flow" hierarchy
40 to handle debug messages from a SimpleITK object.
42 Warning messages from SimpleITK are globally disabled with
43 ProcessObject:GlobalWarningDisplayOff.
47 self, logger: logging.Logger = logging.getLogger(
"SimpleITK")
50 Initializes with a Logger object to handle the messages emitted from
53 super(SimpleITKLogger, self).__init__()
61 def logger(self, logger):
65 self._old_logger = self.SetAsGlobalITKLogger()
68 def __exit__(self, exc_type, exc_val, exc_tb):
69 self._old_logger.SetAsGlobalITKLogger()
72 def DisplayText(self, s):
75 self._logger.info(s.rstrip())
77 def DisplayErrorText(self, s):
78 self._logger.error(s.rstrip())
80 def DisplayWarningText(self, s):
81 self._logger.warning(s.rstrip())
83 def DisplayGenericOutputText(self, s):
84 self._logger.info(s.rstrip())
86 def DisplayDebugText(self, s):
87 self._logger.debug(s.rstrip())
91 sitk.ProcessObject.GlobalDefaultDebugOn()
94 sitkLogger = SimpleITKLogger()
97 sitkLogger.SetAsGlobalITKLogger()
101 format=
"%(name)s (%(levelname)s): %(message)s", level=logging.DEBUG
107 with SimpleITKLogger(logging.getLogger(
"Show"))
as showLogger:
108 print(f
"Logger name: {showLogger.GetName()}")
110 if "SITK_NOSHOW" not in os.environ: