logging messages in imported modules not showing up
Vinay Sajip
vinay_sajip at yahoo.co.uk
Wed Mar 17 16:51:15 EST 2004
erick_bodine at comcast.net (Erick Bodine) wrote in message news:<8de75d53.0403091141.e488ec at posting.google.com>...
> I have a script that sets up logging at startup, imports a module
> (module1) which itself imports another module (module2).
>
> The problem is that if I run script.py, I see the following. I never
> see the 'MyClass2 initialized' log message from module2??
>
> 2004-03-09 12:15:18,348 DEBUG [script:15] logging enabled
> 2004-03-09 12:15:18,348 DEBUG [module1:12] MyClass initialized
> 2004-03-09 12:15:18,348 DEBUG [module2:14] MyClass2 do_something
>
> --------------
> script.py
> -------------
> import logging, module1
>
> def main():
> # setup logging
> log = logging.getLogger('myapp')
> handler = logging.StreamHandler()
> log.setLevel(logging.DEBUG)
> format = logging.Formatter("%(asctime)s %(levelname)-3s " \
> "[%(module)s:%(lineno)d] %(message)s")
> handler.setFormatter(format)
> log.addHandler(handler)
>
> log.debug("logging enabled")
>
> my = module1.MyClass()
>
> if __name__ == '__main__':
> main()
>
> --------------
> module1.py
> --------------
> import logging, module2
> log = logging.getLogger('myapp')
>
> class MyClass:
> def __init__(self):
> log.debug("MyClass initialized")
>
> def something(self):
> my = module2.MyClass2()
> my.do_something()
>
> -------------
> module2.py
> -------------
> import logging
> log = logging.getLogger('myapp')
> class MyClass2:
> def __init__(self):
> log.debug("MyClass2 initialized")
>
> def do_something(self):
> log.debug("In MyClass2 do_something")
Perhaps your script is not working as you expect because you forgot to
call my.something() from main(). Hence a MyClass2 instance was never
instantiated and do_something() was never called on it.
Vinay Sajip
More information about the Python-list
mailing list