473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Log rolling question

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.

Oct 25 '05 #1
4 2067
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.

Oct 25 '05 #2
Is there a way to do this for a whole calendar month?

Oct 25 '05 #3
On 25 Oct 2005 10:22:24 -0700, "elake" <er******@gmail.com> 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
Oct 25 '05 #4
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.

Oct 26 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: traga_2_whiskys | last post by:
Weel i want do make a box with text rolling down to up, i've make-it wiht <marquee> in html, byt the text isn't always rolling. can anibody help me?
3
by: DB2 Convert | last post by:
Hi, Correct me if I am wrong? Why should I specify at the restore statement such as Restore Database ABC ... "WITHOUT ROLLING FOWARD"? My understand is when I am using circular logging,...
3
by: db2group88 | last post by:
we are using db2 udb v8.2 on windows. All our tables are created with "not logged initially" parameter. Our application with auto commit on. I would like to do an online backup and rolling forward...
0
by: PeteCresswell | last post by:
The one I would up was a pivot table presentation" Fund Year ReturnForYear ReturnForYearPlusPreceedingYear, ReturnForYearPlusTwoPreceedingYears.....and so-forth. But that design compromise...
6
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
3
by: none | last post by:
I am trying to write a query/report where I can come up with a rolling sum of the past five days for each line item that I have. For example: Transaction Date Amount 15-Jan-06 ($5.50)...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
3
by: farhadtarapore | last post by:
I have a very large C++ application that has been converted into a windows service. This application writes a lot of statements to the console i.e. cout and cerr. I have used std::ofstream...
4
by: brendan.wong | last post by:
hello. i'm trying to incorporate error handling into my application, but i've run into a dilemma. i've already performed 10 successful INSERTS, but on the 11th INSERT, the application fails for...
2
by: cowboyjeff05 | last post by:
the question is 1) build a Die class (6 sided). Then create a driver that tests your class. Start with one die in your driver then add a second die and notify the user and count when he or she rolls...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.