python logging

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu May 19 05:27:52 EDT 2011


On May 18, 11:42 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> I was wrong, it's more complicated than that.
>
> >>>logging.getLogger('log').warning('test')
>
> No handlers could be found for logger "log">>>logging.warning('test')
> WARNING:root:test
> >>>logging.getLogger('log').warning('test')
>
> WARNING:log:test
>
> Apparently, getLogger() is unconfigured by default, but if you just
> use the root logger once, then they magically get configured.

The difference is that you called the module-level convenience
function -

logging.warning('test')

The module-level convenience functions call basicConfig(), which
configures a console handler on the root logger if no handlers are
present there. This is documented at

http://docs.python.org/library/logging.html#logging.log

(see para starting "PLEASE NOTE:")

and

http://docs.python.org/howto/logging.html#advanced-logging-tutorial

(search for "If you call the functions")

This is not a behaviour change - it's been like this since logging
appeared in Python, see

http://hg.python.org/cpython/annotate/f72b1f8684a2/Lib/logging/__init__.py#l1145

Regards,

Vinay Sajip



More information about the Python-list mailing list