Skip to content

Instantly share code, notes, and snippets.

@erlichmen
Created May 21, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erlichmen/31b6730523b664db1e0ca362694b47aa to your computer and use it in GitHub Desktop.
Save erlichmen/31b6730523b664db1e0ca362694b47aa to your computer and use it in GitHub Desktop.
Downloads Google cloud functions logs between two times
#!/usr/bin/env python
import argparse
import subprocess
import re
parser = argparse.ArgumentParser()
parser.add_argument('function')
parser.add_argument('--start-time')
parser.add_argument('--end-time')
parser.add_argument('--filter')
args = parser.parse_args()
log_line_regex = r".\s+(?P<function>.*?)\s+(?P<hash>.*?)\s+(?P<ts>.*?\ .*?)\s+(?P<msg>.*)"
total_calls = 0
start_time = args.start_time
while True:
gcloud_args = ["gcloud", "functions", "logs", "read", args.function, "--limit", "1000", "--start-time", start_time, "--end-time", args.end_time]
if args.filter:
gcloud_args.extend(['--filter', args.filter])
for i, line in enumerate(subprocess.check_output(gcloud_args).split('\n')):
if not line:
continue
if i == 0:
if total_calls == 0:
print(line)
continue
m = re.match(log_line_regex, line)
d = m.groupdict()
print(line)
start_time = d['ts']
if i == 2:
break
total_calls += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment