Search log for string and display hourly/daily report

spek06 at gmail.com spek06 at gmail.com
Thu Jan 24 01:57:50 CET 2013


On Wednesday, January 23, 2013 4:25:59 PM UTC-5, John Gordon wrote:
> In <4f952e77-258d-47ae-9d76-a86daa8acaa2 at googlegroups.com> spek06 writes:
> 
> 
> 
> > I need to search a log file for a specific string (Successfully Sent) and
> 
> > report the number of instances in the last hour (from when executed) and
> 
> > total for the day so far (midnight till the time executed). Can anyone
> 
> > provide any examples of such a program or get me started?
> 
> 
> 
> from datetime import datetime, timedelta
> 
> from time import mktime, strptime
> 
> 
> 
> now = datetime.now()
> 
> midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
> 
> one_hour_ago = now - timedelta(hours=1)
> 
> 
> 
> daily_instances = 0
> 
> hourly_instances = 0
> 
> 
> 
> with open('myfile.log') as logfile:
> 
>     for line in logfile:
> 
>         if 'Successfully Sent' in line:
> 
>             time_string = line[0:19]
> 
>             struct = strptime(time_string, "%Y-%m-%dT%H:%M:%S")
> 
>             log_time = datetime.fromtimestamp(mktime(struct))
> 
> 
> 
>             if log_time > midnight:
> 
>                 daily_instances += 1
> 
> 
> 
>             if log_time > one_hour_ago:
> 
>                 hourly_instances += 1
> 
> 
> 
> print "Instances in the last hour: ", hourly_instances
> 
> print "Instances since midnight: ", daily_instances
> 
> 
> 
> 
> 
> This code assumes that log lines begin with a timestamp similar to
> 
> "2013-01-23T09:27:01".  If the timestamp is in a different format, or
> 
> occurs elsewhere in the line, you'll have to adjust for that.
> 
> 
> 
> -- 
> 
> John Gordon                   A is for Amy, who fell down the stairs
> 
>              B is for Basil, assaulted by bears
> 
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

Thanks John, I think this will definitely help get me started!



More information about the Python-list mailing list