Trouble propagating logging configuration

Peter J. Holzer hjp-python at hjp.at
Sat Sep 4 16:31:12 EDT 2021


On 2021-09-02 09:06:53 +0200, Loris Bennett wrote:
> Thanks Peter and Dieter for all the help.  I have finally figured out
> what my problem was.  If in a module 'mylibs.mylib' I have
> 
>   import logging
> 
>   logger = logging.getLogger(__name__)
> 
> and in my main script have
> 
>   import logger
>   import logger.config
> 
>   import mylibs.mylib
> 
>   logging.config.fileConfig("/home/loris/config")
>   logger = logging.getLogger(__name__)
>  
> then 'logger' in 'mylibs.mylib' is defined before
> 'logging.config.fileConfig' is called and the logger in the module is
> not configured.
> 
> If change the order and write
> 
>   import logger
>   import logger.config
> 
>   logging.config.fileConfig("/home/loris/config")
>   logger = logging.getLogger(__name__)
> 
>   import mylibs.mylib
> 
> then the 'logger' in 'mylibs.mylibs' does get configured properly.

That normally shouldn't make any difference, since any functions in
mylibs.mylib would be called only after the logger is initialized.

Does your mylibs.mylib contain code which is run on import (other than
class, function and variable definitions)?

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20210904/ce914956/attachment.sig>


More information about the Python-list mailing list