Confused about logger config from within Python (3)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Dec 29 00:05:17 EST 2012


On Fri, 28 Dec 2012 16:41:20 -0800, andrew cooke wrote:

> similarly, if i run the following, i see only "done":
> 
>   from logging import DEBUG, root, getLogger
> 
>   if __name__ == '__main__':
>       root.setLevel(DEBUG)
>       getLogger(__name__).debug("hello world")
>       print('done')


In Python 2.7, the above prints:

py> from logging import DEBUG, root, getLogger
py> root.setLevel(DEBUG)
py> getLogger(__name__).debug("hello world"); print("done")
No handlers could be found for logger "__main__"
done


In Python 3.2 and 3.3, the message about no handlers is not printed, 
which is an interesting difference. (Somebody who knows more about the 
logging package than I do might be able to state why that difference.) So 
it would help if you told us what version of Python you're running.

This works as expected:


py> lg = getLogger(__name__)
py> lg.level = logging.DEBUG
py> lg.debug("hello world"); print("done")
DEBUG:__main__:hello world
done

since the default logging level is WARNING, as the tutorial explains:

[quote]
The default level is WARNING, which means that only events of this level 
and above will be tracked, unless the logging package is configured to do 
otherwise.
[end quote]

http://docs.python.org/dev/howto/logging.html



-- 
Steven



More information about the Python-list mailing list