-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsnippets.py
108 lines (77 loc) · 3.17 KB
/
snippets.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
#!/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.
"""This application demonstrates how to perform basic operations on logs and
log entries with Cloud Logging.
For more information, see the README.md under /logging and the
documentation at https://cloud.google.com/logging/docs.
"""
import argparse
from google.cloud import logging
# [START logging_write_log_entry]
def write_entry(logger_name):
"""Writes log entries to the given logger."""
logging_client = logging.Client()
# This log can be found in the Cloud Logging console under 'Custom Logs'.
logger = logging_client.logger(logger_name)
# Make a simple text log
logger.log_text("Hello, world!")
# Simple text log with severity.
logger.log_text("Goodbye, world!", severity="ERROR")
# Struct log. The struct can be any JSON-serializable dictionary.
logger.log_struct(
{
"name": "King Arthur",
"quest": "Find the Holy Grail",
"favorite_color": "Blue",
}
)
print("Wrote logs to {}.".format(logger.name))
# [END logging_write_log_entry]
# [START logging_list_log_entries]
def list_entries(logger_name):
"""Lists the most recent entries for a given logger."""
logging_client = logging.Client()
logger = logging_client.logger(logger_name)
print("Listing entries for logger {}:".format(logger.name))
for entry in logger.list_entries():
timestamp = entry.timestamp.isoformat()
print("* {}: {}".format(timestamp, entry.payload))
# [END logging_list_log_entries]
# [START logging_delete_log]
def delete_logger(logger_name):
"""Deletes a logger and all its entries.
Note that a deletion can take several minutes to take effect.
"""
logging_client = logging.Client()
logger = logging_client.logger(logger_name)
logger.delete()
print("Deleted all logging entries for {}".format(logger.name))
# [END logging_delete_log]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("logger_name", help="Logger name", default="example_log")
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("list", help=list_entries.__doc__)
subparsers.add_parser("write", help=write_entry.__doc__)
subparsers.add_parser("delete", help=delete_logger.__doc__)
args = parser.parse_args()
if args.command == "list":
list_entries(args.logger_name)
elif args.command == "write":
write_entry(args.logger_name)
elif args.command == "delete":
delete_logger(args.logger_name)