Re: Getting a TimedRotatingFileHandler not to put two dates in thesame file?

Discussion in 'Python' started by Vinay Sajip, Oct 24, 2012.

  1. Vinay Sajip

    Vinay Sajip Guest

    David M Chess <chess <at>> writes:

    > >> But now the users have noticed that if the process isn't up at

    > midnight,
    > >> they can end up with lines from two (or I guess potentially more)

    > dates in
    > >> the same log file.
    > >>
    > >> Is there some way to fix this, either with cleverer arguments

    > into the
    > >> TimedRotatingFileHandler, or by some plausible subclassing of

    > it or its
    > >> superclass?

    Well, of course you can subclass and override it to do what you want - there's
    no magic there. The behaviour is as you would expect: the default behaviour of
    a TimedRotatingFileHandler is to append, and roll over at midnight. So if your
    program isn't running at midnight, it won't rotate:

    Day 1. Run your program, stop it before midnight. The log file contains dates
    from this day. No rotation occurred.

    Day 2. Run your program again, stop it before midnight. The log file contains
    dates from this day, and Day 1. No rotation occurred.

    That's the symptom you're seeing, right?

    You could do as Dave Angel suggested - just use a FileHandler with the name
    derived from the date. If you don't want to or can't actually rotate files at
    midnight, you're using the wrong tool for the job :)

    If you sometimes want to rotate at midnight (the process is running at that
    time) and at other times not (the process isn't running then), you might have
    to code startup logic in your program to deal with the vagaries of your
    environment, since only you would know what they are :)

    Work is afoot to make the actual rollover time configurable (i.e. not forced
    to be literally midnight) - see - but that's
    an enhancement request, not a bug, and so it'll see the light of day in Python
    3.4, if at all. An implementation is in my sandbox repo at

    in branch fix9556. If all you need to do is rollover at a different time daily
    (say 7 a.m.), you might be able to use this. Feel free to use that code as
    inspiration for your subclass.


    Vinay Sajip
    Vinay Sajip, Oct 24, 2012
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Rob Schieber
    Sep 30, 2005
  2. Chris Shenton
    Vinay Sajip
    Feb 23, 2007
  3. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Mark Schupp
    Aug 9, 2004
  4. laredotornado
    Rick DeNatale
    Jan 28, 2008
  5. Dave Angel
    Dave Angel
    Oct 24, 2012

Share This Page