Log rolling question

Discussion in 'Python' started by elake, Oct 25, 2005.

  1. elake

    elake Guest

    I have an application that creates a lot of large log files. I only
    want to keep logs for the previous 4 months. I am looking for a way to
    list the contents of the log dir and if the create date on the log is
    older than 4 months delete it. I have looked at the os.stat() function
    to get the created date but I can't figure out how to get the date to
    go back to a previos time.

    Any help would be appreciated.
     
    elake, Oct 25, 2005
    #1
    1. Advertisements

  2. elake

    Larry Bates Guest

    import time
    #
    # 120 days, 24 hours, 60 monutes, 60 seconds
    #
    fourmonthsago=time.time()-(120*24*60*60)

    Compare fourmonthsago value to modified date of files.
    Delete those that are less.

    Larry Bates


    elake wrote:
    > I have an application that creates a lot of large log files. I only
    > want to keep logs for the previous 4 months. I am looking for a way to
    > list the contents of the log dir and if the create date on the log is
    > older than 4 months delete it. I have looked at the os.stat() function
    > to get the created date but I can't figure out how to get the date to
    > go back to a previos time.
    >
    > Any help would be appreciated.
    >
     
    Larry Bates, Oct 25, 2005
    #2
    1. Advertisements

  3. elake

    elake Guest

    Is there a way to do this for a whole calendar month?
     
    elake, Oct 25, 2005
    #3
  4. On 25 Oct 2005 10:22:24 -0700, "elake" <> wrote:

    >Is there a way to do this for a whole calendar month?
    >

    Yes, if you can specify exactly what you want to do. E.g.,
    today is 2005-10-25. What date would you like as the earliest
    to keep for the four months? What if today's date was not
    available in the month of four months ago? Do you want to
    have cleansed logs always start on the first day of a month?
    if so, do you want to go back four prior months, or consider
    any current month days as being a month-log even if partial,
    and only include 3 prior months? You haven't defined your requirements ;-)

    if you wanted to go back to the first day of four months back, maybe
    you could call that earliest_time and delete all files where after
    import os, stat
    you remove files where
    os.stat(pathtofile)[stat.ST_MTIME] < earliest_time

    going back from current time might go something like

    >>> import time
    >>> y,m = time.localtime()[:2]
    >>> y,m

    (2005, 10)
    >>> y,m = divmod(y*12+m-1-4,12) # -1 for 0..11 months
    >>> m+=1 # back to 1..12
    >>> y,m

    (2005, 6)
    >>> time.strptime('%04d-%02d-01'%(y,m), '%Y-%m-%d')

    (2005, 6, 1, 0, 0, 0, 2, 152, -1)
    >>> earliest_time = time.mktime(time.strptime('%04d-%02d-01'%(y,m), '%Y-%m-%d'))
    >>> earliest_time

    1117609200.0
    >>> time.ctime(earliest_time)

    'Wed Jun 01 00:00:00 2005'

    Hm ...
    Jun, Jul, Aug, Sep, Oct
    -4 -3 -2 -1 -0
    I guess that guarantees 4 full prior months.

    I used strptime to build a complete 9-tuple rather than doing it by hand, which
    I'm not sure off hand how to do ;-)

    There's another module that does date interval addition/subtraction, but it didn't
    come with the batteries in my version.

    BTW, make sure all your date stuff is using the same epoch base date, in case you
    have some odd variant source of numerically encoded dates, e.g., look at

    >>> import time
    >>> time.localtime(0)

    (1969, 12, 31, 16, 0, 0, 2, 365, 0)
    >>> time.ctime(0)

    'Wed Dec 31 16:00:00 1969'
    >>> time.gmtime(0)

    (1970, 1, 1, 0, 0, 0, 3, 1, 0)

    Regards,
    Bengt Richter
     
    Bengt Richter, Oct 26, 2005
    #4
  5. elake

    NickC Guest

    If you're on Python 2.4, then consider whether or not you can use a
    TimedRotatingLogFileHandler from the logging module to handle this for
    you:
    http://www.python.org/doc/2.4.1/lib/node344.html

    Of course, that only works if defining a "month" as 30 days is
    acceptable. If you genuinely need calendar months, then you still need
    to do it manually.
     
    NickC, Oct 26, 2005
    #5
    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. Henrik_the_boss
    Replies:
    0
    Views:
    3,036
    Henrik_the_boss
    Nov 5, 2003
  2. Amratash
    Replies:
    0
    Views:
    721
    Amratash
    Apr 13, 2004
  3. =?Utf-8?B?VG9tIFdpbmdlcnQ=?=

    My.Log.Writeexception not writing to Application Event Log.

    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=, Jan 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,610
    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=
    Jan 20, 2006
  4. unomystEz
    Replies:
    0
    Views:
    738
    unomystEz
    Nov 19, 2006
  5. vj
    Replies:
    0
    Views:
    807
  6. ES
    Replies:
    3
    Views:
    795
  7. AAaron123
    Replies:
    1
    Views:
    417
    Cowboy \(Gregory A. Beamer\)
    Oct 14, 2008
  8. Tom Wingert
    Replies:
    0
    Views:
    560
    Tom Wingert
    Jan 12, 2006
Loading...