logging - how to use in a library?

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Aug 26 18:47:52 EDT 2008


Thomas Heller <theller at python.net> writes:

> If the script using the library does NOT configure logging, and
> somewhere the library calls logger.error(...) or
> logger.critical(...) then he gets a message on stderr saying:
> 
>   No handlers could be found for logger "foo"

Right. So, part of the requirements for using your library now is that
the caller is responsible for configuring logging before using the
library.

> I assume it would not be a good idea to configure logging in the
> library itself, possibly overwriting explicit configuration that the
> user has done... I don't get it.

The logging system needs to be configured in a way that makes sense
for the application, so it's the application (somewhere near the top
level) that's responsible for configuring it. You're right that this
can't be done without that context; there's no sane default.

You need to document that assumption of your library for those who
will use it.

-- 
 \           “The cost of education is trivial compared to the cost of |
  `\                                     ignorance.” —Thomas Jefferson |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list