Search code examples
pythonnginxlogparser

Parsing nginx log data with whitespace as delimiter


I am trying to parse the following format from a nginx log file:

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

My python script:

#!/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]

Basically all I want from the log file is the query that was sent, so in the above example, i'm looking to extract /api/123/test.json?stop=2012-09-29

My script doesn't seem to do this, what am I doing wrong?


Solution

  • Those are tabs, not spaces:

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