Logging help

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Jan 22 10:14:02 CET 2009

On Jan 22, 6:49 am, koranthala <koranth... at gmail.com> wrote:
> I understand Vinay. But my point is that I wanted a mechanism to
> rotate the log files based on data - i.e. today one log, tomorrow

Did you mean "based on date"?

> another. This is easier because when trouble tickets are raised, users
> mention that X failed at this time.
> Now, timedrotatingfilehandler does it - but only if the program is
> running the whole length of time.
> My tool is invoked by a cron job - so the program runs, then stops
> again and again.

If you just want a log file whose name is date-based, you don't need a
rotating file handler. Compute the file name from the date and use the
API to create a FileHandler specifying that file name, and add it to
your logger. For example:

import logging, time

logging.basicConfig(level=logging.DEBUG, filename=time.strftime("/path/
to/my/logs/myapp-%Y-%m-%d-%H%M.log", time.localtime()), filemode="w")
logging.debug("Hello, world!")

Hopefully you can adapt this snippet to your needs.


Vinay Sajip

More information about the Python-list mailing list