I am trying to write a python script which will retrieve new log entries and save the token of the last/latest entry. This token will be used in the next run and logs after that token-entry will be retrieved and so on.
So far I have this:
from google.cloud.logging import Client
projectName = 'my_project'
myFilter = 'resource.type = "cloudsql_database" AND log_id("cloudsql.googleapis.com/mysql.err") AND timestamp>="2021-04-20T00:00:00Z"'
client = Client(project=projectName)
entries = client.list_entries(filter_=myFilter)
fd = open("log_entries.txt", "w")
for entry in entries:
print(entry)
fd.write(str(entry))
fd.close()
Sample output entry:
TextEntry(
log_name='projects/my_project/logs/cloudsql.googleapis.com%2Fmysql.err',
labels=None,
insert_id='s=90fe0e477e6e4e6e8e5017468exxxxxx;
i=1c31xxx;
b=c42a243698784ef88f55dfe5a4xxxxxx;
m=53c40845xxx;
t=5c03646e67xxx;
x=be3568664236fxxx-0-0@a1',
severity='INFO',
http_request=None,
timestamp=datetime.datetime(2021, 4, 20, 2, 50, 33, 830602, tzinfo=<UTC>),
resource=Resource(type='cloudsql_database',
labels={'region': 'us-east4', 'database_id': 'my_project:my_instance_id', 'project_id': 'my_project'}),
trace=None,
span_id=None,
trace_sampled=None,
source_location=None,
operation=None,
logger=<google.cloud.logging_v2.logger.Logger object at 0x0000025FA3XXXXXX>,
payload="2021-04-20T02:50:33.830224Z 63149802 [Note] Aborted connection 63149802 to db: 'my_db' user: 'my_user' host: 'ip_address' (Got an error reading communication packets)"
)
As you can see, there is no such value as a token number that can be passed to list_entries method. I'm not sure if list_entries is the correct method to use in my case. Can anyone help me with this?