logging messages in imported modules not showing up

Erick Bodine erick_bodine at comcast.net
Tue Mar 9 20:41:37 CET 2004


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")



More information about the Python-list mailing list