19 import SimpleITK
as sitk
25 Adapts SimpleITK messages to be handled by a Python Logger object.
27 Allows using the logging module to control the handling of messages coming from ITK and SimpleTK. Messages
28 such as debug and warnings are handled by objects derived from sitk.LoggerBase.
30 The LoggerBase.SetAsGlobalITKLogger method must be called to enable SimpleITK messages to use the logger.
32 The Python logger module adds a second layer of control for the logging level in addition to the controls already
35 The "Debug" property of a SimpleITK object must be enabled (if available) and the support from the Python "logging
36 flow" hierarchy to handle debug messages from a SimpleITK object.
38 Warning messages from SimpleITK are globally disabled with ProcessObject:GlobalWarningDisplayOff.
41 def __init__(self, logger: logging.Logger = logging.getLogger(
"SimpleITK")):
43 Initializes with a Logger object to handle the messages emitted from SimpleITK/ITK.
45 super(SimpleITKLogger, self).__init__()
53 def logger(self, logger):
57 self._old_logger = self.SetAsGlobalITKLogger()
60 def __exit__(self, exc_type, exc_val, exc_tb):
61 self._old_logger.SetAsGlobalITKLogger()
64 def DisplayText(self, s):
66 self._logger.info(s.rstrip())
68 def DisplayErrorText(self, s):
69 self._logger.error(s.rstrip())
71 def DisplayWarningText(self, s):
72 self._logger.warning(s.rstrip())
74 def DisplayGenericOutputText(self, s):
75 self._logger.info(s.rstrip())
77 def DisplayDebugText(self, s):
78 self._logger.debug(s.rstrip())
82 sitk.ProcessObject.GlobalDefaultDebugOn()
85 sitkLogger = SimpleITKLogger()
88 sitkLogger.SetAsGlobalITKLogger()
91 logging.basicConfig(format=
'%(name)s (%(levelname)s): %(message)s', level=logging.DEBUG)
96 with SimpleITKLogger(logging.getLogger(
"Show"))
as showLogger:
97 print(f
"Logger name: {showLogger.GetName()}")
99 if "SITK_NOSHOW" not in os.environ: