forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs_api.py
More file actions
99 lines (79 loc) · 3 KB
/
logs_api.py
File metadata and controls
99 lines (79 loc) · 3 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
#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
#
# 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.
import argparse
from gcloud import logging
from oauth2client.client import GoogleCredentials
def write_entry(client, args):
# [START write]
print('Writing log entry for logger '.format(args.logger_name))
mylogger = client.logger(args.logger_name)
mylogger.log_text(args.entry)
# [END write]
def list_entries(client, args):
"""Lists all entries for a logger"""
# [START list]
logger = client.logger(args.logger_name)
print('Listing all log entries for logger {}'.format(logger.name))
entries = []
token = None
while True:
new_entries, token = client.list_entries(
filter_='logName="{}"'.format(logger.full_name),
page_token=token)
entries += new_entries
if token is None:
break
for entry in entries:
timestamp = entry.timestamp.isoformat()
print('{}: {}'.format
(timestamp, entry.payload))
# [END list]
return entries
def delete_logger(client, args):
"""Deletes a logger and all its entries.
Note that a deletion can take several minutes to take effect.
"""
# [START delete]
logger = client.logger(args.logger_name)
print('Deleting all logging entries for {}'.format(logger.name))
logger.delete()
# [END delete]
def get_client(project_id):
"""Builds an http client authenticated with the service account
credentials."""
# [START auth]
credentials = GoogleCredentials.get_application_default()
return logging.Client(project=project_id, credentials=credentials)
# [END auth]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--project_id', help='Project ID you want to access.', required=True)
parser.add_argument(
'--logger_name', help='Logger name', default='mylogger')
subparsers = parser.add_subparsers()
write_parser = subparsers.add_parser('write_entry')
write_parser.add_argument('entry')
write_parser.set_defaults(func=write_entry)
list_parser = subparsers.add_parser('list_entries')
list_parser.set_defaults(func=list_entries)
delete_parser = subparsers.add_parser('delete_logger')
delete_parser.set_defaults(func=delete_logger)
args = parser.parse_args()
client = get_client(args.project_id)
args.func(client, args)