Skip to content

Logging should not be done with the root logger #123

@scarere

Description

@scarere

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')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions