Reputation: 170
I am trying to get a python program to continuously run until a certain aws log is registered and printed. It is supposed to:
the python command looks like this: python3 watch_logs.py <log-source> --start=15:00:00
The logs are working fine, and the python script can print them out between certain time frames as long as they already exist. The program works by taking a continuously running task which prints events to the log file, and the python script should filter out events I am looking for and print them.
But, when I run the script, it wont print the event even if I can see the log entry appear in the file. If i kill the process and run it again using the same timestamp, it will find the log entry and end the script like it should.
the code is fairly short:
logs = get_log_events(
log_group=log_group,
start_time=start_time,
end_time=end_time
)
while True:
for event in logs:
print(event['message'].rstrip())
sys.exit("Task complete")
Any insight why this is happening would help a lot. I am fairly new to python
Upvotes: 0
Views: 1605
Reputation: 170
I was overthinking the problem. I put the log variable inside the loop so it was being defined at each cycle
while True:
logs = get_log_events(
log_group=log_group,
start_time=start_time,
end_time=end_time
)
for event in logs:
print(event['message'].rstrip())
sys.exit("Task complete")
Upvotes: 0
Reputation: 856
If you don't want to read the file each time through the loop, you should have a look at pygtail (https://pypi.org/project/pygtail/).
Upvotes: 0
Reputation: 496
The value in the variable logs
is old when the file is updated. You need to update this variable. For example if you were to use logs = myfile.read()
at start of your script, the value in the logs
variable would be a snapshot of that file at that time.
Upvotes: 1
Reputation: 376
Try storing event['message'].rstrip()
in a variable and checking with an if statement if it corresponds to the log you want to find
Upvotes: 0