Parsing apache log files

前端 未结 6 910
你的背包
你的背包 2020-12-01 01:39

I just started learning Python and would like to read an Apache log file and put parts of each line into different lists.

line from the file

1

6条回答
  •  有刺的猬
    2020-12-01 02:14

    I have created a python library which does just that: apache-log-parser.

    >>> import apache_log_parser
     >>> line_parser = apache_log_parser.make_parser("%h <<%P>> %t %Dus \"%r\" %>s %b  \"%{Referer}i\" \"%{User-Agent}i\" %l %u")
    >>> log_line_data = line_parser('127.0.0.1 <<6113>> [16/Aug/2013:15:45:34 +0000] 1966093us "GET / HTTP/1.1" 200 3478  "https://example.com/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18)" - -')
    >>> pprint(log_line_data)
    {'pid': '6113',
     'remote_host': '127.0.0.1',
     'remote_logname': '-',
     'remote_user': '',
     'request_first_line': 'GET / HTTP/1.1',
     'request_header_referer': 'https://example.com/',
     'request_header_user_agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18)',
     'response_bytes_clf': '3478',
     'status': '200',
     'time_received': '[16/Aug/2013:15:45:34 +0000]',
     'time_us': '1966093'}
    

提交回复
热议问题