Skip to content

Instantly share code, notes, and snippets.

@malandro-sv
Created November 11, 2025 01:51
Show Gist options
  • Select an option

  • Save malandro-sv/27915a6946566865a45312515728ca27 to your computer and use it in GitHub Desktop.

Select an option

Save malandro-sv/27915a6946566865a45312515728ca27 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import sys
import time
#from random import randint
from datetime import datetime
from google.cloud import logging
# Instantiates logging client
client = logging.Client()
# logName as shown in logEntry body; see
# docs.cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
log_name = sys.argv[1]
def send_stream(entry, count, total): # Writes log entry
logger = client.logger(log_name)
logger.log_text(entry)
print(f"[{time.ctime()}]\nEntry {count}/{total} successfully written to '{log_name}'. ")
def fake_log_stream():
""" a whole stream of log entries (arg = $2)
with uniform wait time gap between one another ($3)
plus an interrupt > 5min ($4)
"""
# sys args definitions/time intervals:
# wrapper for 'int(sys.argv[2])':
to_int = lambda x: int(sys.argv[x])
total_mssgs = to_int(2)
mssg_gap = to_int(3)
ingest_interrupt = to_int(4)
disrupt = lambda x, y: x == (total_mssgs / 3) * y
# first message is #1:
count = 1
while count < total_mssgs:
try:
# data to write:
log_entry = f"Entry {count}/{total_mssgs} to '{log_name}' {str(datetime.now())} " #+ str(datetime.now())
send_stream(log_entry, count, total_mssgs)
count += 1
# console output + wait time before next message:
print(f"{mssg_gap}-sec to send next entry [Ctrl+C to stop]")
print(f"{ingest_interrupt}-sec interrupt by 33% and 66% progress.\n")
time.sleep(mssg_gap)
# >5min interrupt kicks in when reaching 33% or 66% of total logs sent.
if disrupt(count, 1) or disrupt(count, 2):
print(f"reached {ingest_interrupt}sec interrupt. ")
interrupt_mssg = f"{ingest_interrupt}-sec interrupt kicking in at {str(datetime.now())}. "
print(interruptt_mssg)
time.sleep(ingest_interrupt)
print(f"Resuming logging to {log_name}")
send_stream(interrupt_mssg, count)
count += 1
except KeyboardInterrupt:
print("\nLogging stopped by user.")
break
# so far, auth error comes up (need to re-auth in CLI):
except Exception as e:
print(f"Error occurred: {e}")
break
def save_sequence(seq_file):
""" reads sequence from file, fetches sequence#
then it writes sequence+1 to file. """
sequence = None
with open(seq_file, "r+", encoding="utf-8") as file:
sequence = int(file.read().strip())
file.seek(0)
file.truncate()
print(f"Old sequence is {sequence}")
new_sequence = sequence + 1
print(f"New sequence: {new_sequence}.")
file.write(str(new_sequence) + "\n")
#file.close()
#save_sequence(argv[1])
#with open("this.example", "w", encoding="utf-8") as file:
# sequence = file.readline()
# print(sequence.strip())
fake_log_stream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment