How to implement logging for an imported module?

Richard Damon Richard at Damon-Family.org
Mon Mar 8 08:58:44 EST 2021


On 3/8/21 4:16 AM, Robert Latest via Python-list wrote:
> Joseph L. Casale wrote:
>>> I couldn't find any information on how to implement logging in a library that
>>> doesn't know the name of the application that uses it. How is that done?
>> That's not how it works, it is the opposite. You need to know the name of its
>> logger, and since you imported it, you do.
> [much snipping]
>
>> Last word of advice, don't fight it by hacking up or patching (somehow?), it
>> will simply not work right for any other case even slightly different than
>> the one you somehow beat into submission.
> I didn't waht to hack the logging system, it's just that I wasn't sure of its
> design principles. I had hoped that if I set up a logger (including levels and
> formatter) in my main app, the loggers in the imported modules would somwhow
> automagically follow suit. Now I understand that for each imported module I
> must import its logger, too, and decide how to deal with its messages.
>
>
Each instance of the logger inherents from a 'parent' logger, except for
the top level logger which has the name None (as in the singleton of
NoneType), and unless told otherwise will inherit it properties from its
parent.

Thus, if you get the root logger with logging.getLogger() you can set
properties there, and unless a child logger has specifical been told not
to inherit or has been specifically given a different value.

General convention is that modules will use their name as the name of
their logger, as that is generally unique.

-- 
Richard Damon



More information about the Python-list mailing list