forked from uphold/docker-dash-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-logger
53 lines (39 loc) · 1.67 KB
/
google-logger
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
#!/usr/bin/env python
# adapted from: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/logging/cloud-client/snippets.py
import argparse
from google.cloud import logging
def write_entry(logger_name, message):
"""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(message)
# Simple text log with severity.
# logger.log_text('Goodbye, world!', severity='ERROR')
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))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--logger_name', help='Logger name', default='bitcoind-walletnotify')
parser.add_argument(
'--message', help='Message to log')
subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('list', help=list_entries.__doc__)
subparsers.add_parser('write', help=write_entry.__doc__)
args = parser.parse_args()
if args.command == 'write':
write_entry(args.logger_name, args.message)
elif args.command == 'list':
list_entries(args.logger_name)