-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs2.py
146 lines (125 loc) · 4.93 KB
/
logs2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import base64
import cgi
import datetime
import logging
import os
import time
#from datetime import datetime, date, time
import urllib
import wsgiref.handlers
import string
from google.appengine.api import users
from google.appengine.api.logservice import logservice
from google.appengine.ext import db
#from google.appengine.ext import webapp
import webapp2 as webapp
# This sample gets the app request logs up to the current time, displays 5 logs
# at a time, including all AppLogs, with a Next link to let the user "page"
# through the results, using the RequestLog offset property.
class MainPage(webapp.RequestHandler):
def get(self):
logging.info('Starting Main handler')
# Get the incoming offset param from the Next link to advance through
# the logs. (The first time the page is loaded, there won't be any offset.)
start_time_set=False
try:
offset = self.request.get('offset') or None
if offset:
offset = base64.urlsafe_b64decode(str(offset))
except TypeError:
offset = None
try:
start_time = self.request.get('start_time') or None
if start_time:
start_time = float(base64.urlsafe_b64decode(str(start_time)))
start_time_set=True
except TypeError:
start_time = None
start_time_set=False
try:
filter = str(self.request.get('filter')) or None
except TypeError:
filter = None
# Set up end time for our query.
# Count specifies the max number of RequestLogs shown at one time.
# Use a boolean to initially turn off visiblity of the "Next" link.
count = 1000
show_next = True
last_offset = 5000
dt=datetime.datetime.now()
tt=dt.timetuple()
year=tt[0]
month=tt[1]
ttt=time.strptime((("01 %s %s") % (month,year)), "%d %m %Y")
if not start_time_set:
end_time = time.time()
start_time = time.mktime(ttt)
else:
dt2=datetime.datetime.utcfromtimestamp(float(start_time))
tt2=dt2.timetuple()
year2=tt2[0]
month2=tt2[1]
month2=month2+1
if month2==13:
month2=1
year2=year2+1
ttt2=time.strptime((("01 %s %s") % (month2,year2)), "%d %m %Y")
end_time=time.mktime(ttt2)
dt3=datetime.datetime.utcfromtimestamp(float(start_time))
tt3=dt3.timetuple()
year3=tt3[0]
month3=tt3[1]
month3=month3-1
if month3==0:
month3=12
year3=year3-1
ttt3=time.strptime((("01 %s %s") % (month3,year3)), "%d %m %Y")
start_time_next=time.mktime(ttt3)
# Iterate through all the RequestLog objects, displaying some fields and
# iterate through all AppLogs beloging to each RequestLog count times.
# In each iteration, save the offset to last_offset; the last one when
# count is reached will be used for the link.
i = 0
for req_log in logservice.fetch(start_time=start_time,end_time=end_time, offset=offset,
minimum_log_level=logservice.LOG_LEVEL_INFO,
include_app_logs=False):
referrer=req_log.referrer
ip=req_log.ip
if filter and referrer and not string.find(referrer, filter) == -1:
# self.response.out.write("<br /> REQUEST LOG <br />")
# self.respons
self.response.out.write("""<a href=\"/logs3?filter=%s\">IP</a> %s <br />""" % (ip, req_log.combined))
i += 1
else:
if not filter:
self.response.out.write("""<a href=\"/logs3?filter=%s\">IP</a> %s <br />""" % (ip, req_log.combined))
i += 1
# self.response.out.write("""IP: %s <br /> Method: %s <br />
# Resource: %s <br />""" % (req_log.ip,
# req_log.method, req_log.resource))
# self.response.out.write("Date: "+datetime.datetime.fromtimestamp(req_log.end_time).strftime('%D %T UTC') +"<br />")
last_offset= req_log.offset
for app_log in req_log.app_logs:
self.response.out.write("<br />APP LOG<br />")
self.response.out.write("Date: "+datetime.datetime.fromtimestamp(app_log.time).strftime('%Y-%m-%d %H:%M:%S UTC') +"<br />")
self.response.out.write("<br />Message: "+app_log.message+"<br />")
if i >= count:
show_next = True
break
# Prepare the offset URL parameters, if any.
if show_next:
query = self.request.GET
query['offset'] = base64.urlsafe_b64encode(last_offset)
query['start_time'] = base64.urlsafe_b64encode(("%s")%(start_time_next))
next_link = urllib.urlencode(query)
self.response.out.write("<a href=\"/logs2?"+next_link+"\">Next</a>")
self.response.out.write("<br />")
#def main():
logging.getLogger().setLevel(logging.DEBUG)
app = webapp.WSGIApplication([
('/logs2', MainPage),
], debug=True)
# wsgiref.handlers.CGIHandler().run(application)
#if __name__ == '__main__':
# main()