简体   繁体   English

Python-从日志文件读取10分钟

[英]Python - read 10min from log file

I need some tool to read latest 10 minutes entry in my log file, and if some words are logged then print some text. 我需要一些工具来读取我的日志文件中最近的10分钟条目,并且如果记录了一些单词,则输出一些文本。

log file: 日志文件:

23.07.2014  09:22:11   INFO Logging.LogEvent   0  Failed login test@test.com
23.07.2014  09:29:02   INFO Logging.LogEvent   0  login test@test.com
23.07.2014  09:31:55   INFO Logging.LogEvent   0  login test@test.com
23.07.2014  09:44:14   INFO Logging.LogEvent   0  Failed login test@test.com

if during last 10min some entry = Failed -print ALARM. 如果在最后10分钟内某项=失败-打印警报。

All what i did is find 'Failed' match but i have no idea how to check last 10min in my log file ;/ -any idea?? 我所做的只是找到“失败”的匹配项,但是我不知道如何在日志文件中检查最后10分钟; /-任何想法?

from sys import argv
from datetime import datetime, timedelta

with open('log_test.log', 'r') as f:
    for line in f:
        try:
            e = line.index("Failed")
        except:
            pass
        else:
            print(line)

Your format %d.%m.%Y is worse than %Y:%m:%d which can be used in string comparison. 您的格式%d.%m.%Y比可用于字符串比较的%Y:%m:%d差。

We also do not know if log is big and if it is sorted. 我们还不知道日志是否很大以及是否已排序。 If it is not sorted (it is common for multithreaded applications) you will have to analyze each line and convert it into datetime: 如果未排序(在多线程应用程序中很常见),则必须分析每一行并将其转换为日期时间:

def get_dt_from_line(s):
    return datetime.datetime.strptime(s[:20], '%d.%m.%Y  %H:%M:%S')

Then use it as filter (for small files): 然后将其用作过滤器(用于小文件):

    MAX_CHECK_TIMEDELTA = datetime.timedelta(minutes=10)
    LOG_START_ANALYZE_DATETIME = (datetime.datetime.today() - MAX_CHECK_TIMEDELTA)
    lines = [s for s in TXT.split('\n') if 'Failed' in s and get_dt_from_line(s) >= LOG_START_ANALYZE_DATETIME]
    print('\n'.join(lines))

For big files you can read file line by line. 对于大文件,您可以逐行读取文件。

If your log file is just for one day you can use string comparison instead of datetime comparison: 如果您的日志文件只有一天的时间,则可以使用字符串比较而不是日期时间比较:

LOG_START_ANALYZE_DATETIME = (datetime.datetime.today() - datetime.timedelta(minutes=10)).strftime('%d.%m.%Y  %H:%M:%S')
lines = [s for s in TXT.split('\n') if 'Failed' in s and s >= LOG_START_ANALYZE_DATETIME]

If I were you, I would lookup line by line, get the timestamp of the first line and then iterate until the difference between the first date and the current one is more than 10 minutes, while counting occurences of the word "Failed". 如果您是我,我将逐行查找,获取第一行的时间戳,然后进行迭代直到第一个日期与当前日期之间的差超过10分钟,同时计算出现“失败”一词的次数。

I think that you'll sort something out with splitting your line following spaces. 我认为您可以通过在空格后分割行来解决问题。 But be careful as if someday, your log format changes, your script is likely not gonna be working too. 但是要小心,好像某天您的日志格式改变了,您的脚本可能也无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 google appengine Python 中使用 taskqueue.add() 增加 10 分钟的 http 请求 - how to increase 10min http request using taskqueue.add() in google appengine Python Selenium-Python-相同的代码适用于firefox,但适用于chrome则慢10分钟 - Selenium - Python - same code works well with firefox but 10min slower with chrome 时间 Dataframe 结束开始列到 10 分钟范围 Dataframe - Time Dataframe with end start columns to 10min range Dataframe Pandas 按时间间隔(5 分钟、10 分钟、1 天、1 年)和条目计数分组 - Pandas group by time interval (5min, 10min, 1day, 1year) and count amount of entries 如何在python中读取.log文件? - How to read .log file in python? python - 读取日志文件的各个部分 - python - Read sections of a log file 从使用 python 写入的日志文件中读取 - Read from a log file as it's being written using python 连续读取python中日志文件的最后一行 - Continuously read the last line of log file in python 如何每10分钟将数据从Web写入CSV文件 - How to write data from the web to a CSV file every 10 min Python,大型csv文件上的pandas.read_csv,具有来自Google云端硬盘文件的1000万行 - Python, pandas.read_csv on large csv file with 10 Million rows from Google Drive file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM