[Python] How to add a handler to root logger that only handles events from a particular sub...

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

  1. Stack

    Stack Membro Participativo

    I'm trying to wrap my head around python logging module's hierarchical design. I have a particular logger A.B.C that needs a very specific handler. But it seems to me that attaching a handler directly to A.B.C is somewhat of an anti-pattern and even if I do that events are propagated all the way to the root logger anyway (unless I do logger.propagate = False). The official documentation says:


    A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.
    ​

    What is the best-practice to deal with this? If I'm using my package directly I'll define and configure the root logger myself and I can attach a handler for A.B.C to the root logger but how do I make it only handler events originating from A.B.C. And if I'm using my python package as a library in some other program I don't want to have a handler for A.B.C.

    Continue reading...

Compartilhe esta Página