logging module usage

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Mar 31 18:07:00 EDT 2011


On Mar 30, 3:49 pm, mennis <michaelian.en... at gmail.com> wrote:
> I am working on a library for controlling various appliances in which
> I use theloggingmodule.  I'd like some input on the basic structure
> of what I've done.  Specifically theloggingaspect but more general
> comments are welcome.  I'm convinced I mis-understand something but
> I'm not sure what.  I've posted a version of the library at github.
>
> g... at github.com:mennis/otto.githttp://github.com/mennis/otto
> Ian

>From a quick glance over your code, looking only at the logging
perspective:

It's fine to use the Django-like approach to provide better
compatibility with Python versions < 2.5.

Your use of an extra level is also OK, but other applications and
tools won't know about your extra level, which could limit
interoperability. If your application is completely self contained,
however, that should be fine.

You don't really need to hold loggers as instance attributes in
objects - they are effectively singletons.

The convention is to use logger = getLogger(__name__), that way you
don't have to change your code to rename loggers if you move modules
around in a package.

Not sure why you are doing logging.disable() in code, this means that
you can't change the verbosity using configuration files.

You don't appear to be using the logger.exception() method in
exception handlers, thereby not putting tracebacks in the log.

You don't add a NullHandler to the root logger of your top-level
package, which you should.

I see you're using Python 2.x, but you may nevertheless find it useful
to look at the logging docs for Python 3.2. These have been split into
reference docs and HOWTOs, rather than the somewhat monolithic
approach taken in the 2.x docs.

Regards,

Vinay Sajip



More information about the Python-list mailing list