擅长:python、mysql、java
<p>我决定采纳<a href="https://stackoverflow.com/questions/44631961/log-parsing-in-python-alert-if-more-than-n-errors-in-a-given-period/44632609#comment76249570_44631961">depperm's comment</a>中的建议(我希望它是作为答案而不是评论提交的,这样我就可以将它标记为已接受)。你知道吗</p>
<p>它看起来像这样:</p>
<pre><code>error_queue = []
max_errors = 3
for log_line in log_lines:
log_ts = get_timestamp(log_line)
if contains_error(log_line):
error_queue.append(log_ts)
interval_start = log_ts - datetime.timedelta(minutes=5)
try:
threshold = error_queue[-max_errors]
except IndexError:
continue
if threshold and threshold >= interval_start:
raise Exception
</code></pre>