Cropping log files

Colin Brown cbrown at metservice.com
Tue Dec 16 14:44:14 EST 2003


"Kamus of Kadizhar" <yan at NsOeSiPnAeMr.com> wrote in message
news:brmp3g$b4ar$1 at news3.infoave.net...
...
> I'm tyring to figure out how to best crop a log file.  I have a file
> that grows infinitely in length.  I want to keep only the last n entries
> in the file.
...

An alternative solution may be to write date-based files with deleting of
files older than some limit.
Here is an example (class HourlyLog) of date-based hourly logging with
day-based cleanup:

------------------------------------------------------
import glob, os, time

def Now():    # UTC time now as a string
    return time.asctime(time.gmtime())

def DD_HH():   # 2 digit utc day & hour
    return time.strftime("%d_%H",time.gmtime())

def Hhmmss(): # "hh:mm:ss" string
    return time.strftime("%H:%M:%S",time.gmtime())

def DaysAgo(number): # time days ago
 return time.time() - (86400 * number)

class Hour:
    def __init__(self):
        self.hour = time.gmtime()[3]

    def change(self):
        hour = time.gmtime()[3]
        if hour != self.hour:
            self.hour = hour
            return 1
        else:
            return 0

# filename must contain '##_##' for the 2 digit day and hour fields
def FileHourLog(f,s):     # append string to daily logfile (nb: win32 does
lf -> crlf)
    try:
        ff=open(DD_HH().join(f.split('##_##',1)),'aU')
    except:
        ff=open(DD_HH().join(f.split('##_##',1)),'wU')
    ff.write(''.join(['-----< ',Now(),'
>-----','\n',s.replace('\r\n','\r').replace('\r','\n'),'\n']))
    ff.close()

def FileBefore(f,time):     # return true if file modification date is
before time()-secs
    try:
        if os.stat(f)[8] < time:
            return 1
        else:
            return 0
    except:
        return 0 # may not exist in multi-threaded app.

def FileTidy(list,at):    # delete files older than time
    for file in list:
        try:
            if FileBefore(file,at):
                os.remove(file)
        except:
            pass # may not exist in multi-threaded app.

def FilesMatching(path,pattern): # return a list of files matching pattern.
    return glob.glob(os.path.join(path,pattern))

class HourlyLog:  # filename as per FileLog; number of days to keep
    def __init__(self,f,n):
        self.hour = Hour()
        self.f = f
        self.n = n

    def log(self,s):
        FileHourLog(self.f,s)
        if self.hour.change():
            [path, name] = os.path.split(self.f)
            pattern = name.replace('#','?')
            folder = FilesMatching(path,pattern)
            FileTidy(folder,DaysAgo(self.n))
-------------------------------------------------------------

Colin Brown
PyNZ






More information about the Python-list mailing list