-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (79 loc) · 2.75 KB
/
main.py
File metadata and controls
103 lines (79 loc) · 2.75 KB
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
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Sample Google App Engine application that demonstrates how to use the App
Engine Log Service API to read application logs.
"""
# [START all]
import base64
import datetime
from itertools import islice
from textwrap import dedent
import time
from google.appengine.api.logservice import logservice
import webapp2
def get_logs(offset=None):
# Logs are read backwards from the given end time. This specifies to read
# all logs up until now.
end_time = time.time()
logs = logservice.fetch(
end_time=end_time,
offset=offset,
minimum_log_level=logservice.LOG_LEVEL_INFO,
include_app_logs=True)
return logs
def format_log_entry(entry):
# Format any application logs that happened during this request.
logs = []
for log in entry.app_logs:
date = datetime.datetime.fromtimestamp(
log.time).strftime('%D %T UTC')
logs.append('Date: {}, Message: {}'.format(
date, log.message))
# Format the request log and include the application logs.
date = datetime.datetime.fromtimestamp(
entry.end_time).strftime('%D %T UTC')
output = dedent("""
Date: {}
IP: {}
Method: {}
Resource: {}
Logs:
""".format(date, entry.ip, entry.method, entry.resource))
output += '\n'.join(logs)
return output
class MainPage(webapp2.RequestHandler):
def get(self):
offset = self.request.get('offset', None)
if offset:
offset = base64.urlsafe_b64decode(str(offset))
# Get the logs given the specified offset.
logs = get_logs(offset=offset)
# Output the first 10 logs.
log = None
for log in islice(logs, 10):
self.response.write(
'<pre>{}</pre>'.format(format_log_entry(log)))
offset = log.offset
if not log:
self.response.write('No log entries found.')
# Add a link to view more log entries.
elif offset:
self.response.write(
'<a href="/?offset={}"">More</a'.format(
base64.urlsafe_b64encode(offset)))
app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True)
# [END all]