MIC-DKFZ / batchgenerators

A framework for data augmentation for 2D and 3D image classification and segmentation
Apache License 2.0
1.09k stars 221 forks source link

Logging should not be done with the root logger #123

Open scarere opened 3 months ago

scarere commented 3 months ago

Currently both the MultiThreadedAugmenter and NonDetMultiThreadedAugmenter classes use the root logger to print logs to the console. Calling logging.debug() implicitly creates a new stream handler for the root logger. This is problematic in cases where the user is already using their own logger with its own stream handler. Messages are printed once by the user's logger, and then echoed by the root logger. The solution is simply to create a new logger for the batchgenerators package, and then use that logger to print relevant messages to stdout. For example a logger.py file could contain:

import logging
logger = logging.getLogger('batchgenerators')
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
log = logger.log

Then to print logs to stdout in other files such as multi_threaded_augmenter.py

from logger import log
from logging import INFO, DEBUG

# Something happens in the code
log('A message about what happened')
log(INFO, 'This is an info message instead of default DEBUG message')