Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please remove hard coded usage of the root logger from the logging module #17

Open
etherealite opened this issue Dec 19, 2023 · 2 comments

Comments

@etherealite
Copy link

etherealite commented Dec 19, 2023

Hi there,

Just want to begin by saying I'm eternally grateful for the release of this module as a standalone package. This is a super clean general purpose implementation of JAB.

That being said, I'd really appreciate it if the calls to the root logger could be removed in a future release. At the moment I have to do something pretty hacky to stop the JABWrapper module from adding unwanted handlers and formatters to the root logger.

def monkey_patch_logging():
    """Initial Load of JABWrapper here to prevent it from using the root logger"""
    class MetaReturnsCLS(type):
        def __getattr__(cls, name):
            return lambda *args, **kwargs: cls
    class LoggingFacade(metaclass=MetaReturnsCLS):
        jab_logger = logging.getLogger("JABWrapper")

        debug = jab_logger.debug
        info = jab_logger.info
        DEBUG = logging.DEBUG
        INFO = logging.INFO

    sys.modules[logging.__name__] = LoggingFacade
    import JABWrapper
    from JABWrapper.jab_wrapper import JavaAccessBridgeWrapper
    from JABWrapper.jab_types import JavaObject
    sys.modules[logging.__name__] = logging

This is the only way I was able to both keep my logger hierarchy clean and prevent jab_wrapper.log files showing up in my project directory.

It'd be really simple to fix this issue, it'd only require that the method calls to the logging module itself be substituted with calls to a package local logger. Then, an importing user like myself could chose to enable/disable the logging therein.

import logging

# not this
logging.basicConfig(level=logging.DEBUG, handlers=[logging_file_handler, logging_stream_handler])

logging.debug("my debug message")

# do this
logger = logging.getLogger("JABWrapper")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging_file_handler)
logger.addHandler(logging_stream_handler)

logger.debug("my debug message")

Is this something you'd be willing accept pull requests on?

Thanks!

@mmokko
Copy link
Collaborator

mmokko commented Dec 20, 2023

Hi, definitely would be willing to accept a PR for this.

@kariharju
Copy link

Hi @etherealite,
Definitely PR's are welcome, I have swag bags ready-to-go so don't forget to ping us 😉
..and Happy Holidays!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants