Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How to speed up this Apache log parsing?

I'm parsing big Apache logs like:

example.com:80 1.2.3.4 - - [01/Jul/2021:06:12:12 +0000] "GET /test/example/index.php?a=b&c=d HTTP/1.1" 302 486 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.3945.117 Safari/537.36"

with:

import apache_log_parser, shlex
parser = apache_log_parser.make_parser("%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"")
with open("access.log") as f:
    for l in enumerate(f):
        x = parser(l)
  • For each line, it takes ~0.1 ms (i5 laptop) / ~0.9ms (low-end Atom CPU N2800 1.86GHz)
    This is quite slow: nearly one millisecond for each line!

  • So I decided to do my own parsing with shlex (which deals nicely with quotes such asfirst "second block" "third block" fourth).
    It's worse! I get, per line, ~0.3 ms (i5 laptop) / ~1.6ms low-end server

    with open("access.log") as f:
        for l in enumerate(f):
            x = shlex.split(l)
    
  • Question: Which faster method (maybe with direct regex?) could allow the parsing of such logs? I only need server port ip datetime url status bytes referer useragent.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I finally found a solution that does a x10 speed improvement: pure regex.

import re
r = re.compile(r'(?P<server>.*?):(?P<port>.*?) (?P<ip>.*?) (?P<remote_log_name>.*?) (?P<userid>.*?) \[(?P<date>.*?)\] \"(?P<request>.*?)\" (?P<status>.*?) (?P<length>.*?) \"(?P<referer>.*?)\" \"(?P<useragent>.*?)\"')

with open("access.log") as f:
    for l in enumerate(f):
        d = next(r.finditer(l)).groupdict()
        d['url'] = d['request'].split()[1] if ' ' in d['request'] else '-'
        # d['date'] = datetime.datetime.strptime(d['date'], '%d/%b/%Y:%H:%M:%S %z').isoformat()  # optional

~ 0.01 ms per line on my i5 laptop.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!