Confused about logger config from within Python (3)

Steven D'Aprano steve+comp.lang.python at
Sat Dec 29 05:31:46 CET 2012

On Fri, 28 Dec 2012 11:57:29 -0800, andrew cooke wrote:

> When I use a config file things seem to work (in other projects), but
> for my current code I hoped to configure logging from Python.
> I distilled my problem down to the following test, which does not print
> anything.  Please can someone explain why?  I was expecting the module's
> logger to delegate to root, which has the DEBUG level set.
>     from logging import DEBUG, root, getLogger 
>     from unittest import TestCase
>     class LoggingTest(TestCase):
>         def test_direct(self):
>             root.setLevel(DEBUG)
>             getLogger(__name__).debug("hello world")

Nothing gets printed because you don't do anything except define a class. 
Try instantiating the class, then calling the test_direct method. The 
most convenient way to do so is with the unittest module:

# after defining the class above
import unittest

which then prints:

py> unittest.main()
No handlers could be found for logger "__main__"
Ran 1 test in 0.045s


So you need a handler.


More information about the Python-list mailing list