[Python] Filtering logging of class at runtime

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class in a module:

    # foo.py
    import logging

    class Foo:
    def __init__(self):
    self.logger = logging.getLogger(__name__)
    self.logger.info("Init successful")



    and a script that utilises this class, attempting to filter out the logger in Foo

    # bar.py
    import logging
    from foo import Foo

    logging.basicConfig(level=logging.Info)
    logger = logging.getLogger(__name__)
    logger.addFilter(logging.Filter(__name__))

    logger.info("Started script")
    foo = Foo()


    This doesn't work, is there a way to filter out the logger in Foo?

    Continue reading...

Compartilhe esta Página