python - 以空格作为分隔符解析 nginx 日志数据

标签 python nginx logparser

我正在尝试从 nginx 日志文件中解析以下格式:

10.0.0.1    [02/Oct/2012:10:21:46 +0000]    GET /api/123/test.json?stop=2012-09-29  502 0

我的 python 脚本:

#!/usr/bin/env python

f = file('slow-queries.txt', 'r')

# iterate over the lines in the file
for line in f:
    # split the line into a list of column values
    columns = line.split(' ')
    # clean any whitespace off the items
    # columns = [col.strip() for col in columns]

    # ensure the column has at least one value before printing
    if columns:
        print "first  =>", columns[0]  # print the first column
        print "second =>", columns[1]

基本上我想从日志文件中得到的只是发送的查询,所以在上面的示例中,我希望提取 /api/123/test.json?stop=2012-09-29

我的脚本似乎没有这样做,我做错了什么?

最佳答案

那些是制表符,不是空格:

ip, date, method, path, status, _ = line.split('\t')

关于python - 以空格作为分隔符解析 nginx 日志数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15023105/

相关文章:

Powershell/Logparsing 转换数据类型

syntax - 如何将多个参数传递给 logparser sql 查询?

docker - Nginx 反向代理不提供静态文件

nginx - ffmpeg udp 直播发布到 rtmp

python - 获取另一个类中的条目文本

python - 使用 Python 取消汇总数据集

ubuntu - Nginx 作为负载均衡器,具有 75% 和 25% 加权路由

enterprise-library - 使用logParser查询svc日志文件

python - 将新值添加到过滤后的 pandas DataFrame

python - Map对象转换为列表后清空自身