[Tutor] Python Logging Module

Kent Johnson kent37 at tds.net
Wed Apr 8 04:15:28 CEST 2009


On Tue, Apr 7, 2009 at 6:34 PM,  <chase.mp at gmail.com> wrote:
> Hello everyone...
>
> My intention is to create a generic logging function I can use across
> scripts. I have a few things I would like this function to be able to
> accomplish:
>
> 1. log info and above to console
> 2. log debug and above to file
> 3. format the output for each differently
> 4. allow areas of a program to use a different "logger", basically putting a
> different name in the event depending on which of my bad ideas broke the
> system
> 5. control log rotation within the script itself

You are on the right track to be looking at the logging module.

> import logging.handlers

> # Add the log message handler to the logger
> handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20,

> I cannot for the life of me seem to combine these two functions, this seems
> odd to me, but I think I do not understand what logging.basicConfig does in
> the previous example. If I try to add:
>
> handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20,
> backupCount=5)
> logging.getLogger('').addHandler(handler)
>
> to the first piece of code I get all kinds of errors, but mainly:
>
> AttributeError: 'module' object has no attribute handlers

Notice the
  import logging.handlers
line in the code that works.

Kent


More information about the Tutor mailing list