Day 11

A Python Server Log Analyzer

Try it!

You may need to wait or refresh to let it load



Description


This is a Python program that fetches a server log file over HTTP and lets you search through it interactively. It uses regex named capture groups to parse each log line into fields (IP, date, time, method, endpoint, status code, response size), then filters entries by whichever field you choose.

View the source code


import re
import requests
pattern = r'(?P<ip>\S+) - - \[(?P<date>\S+) (?P<time>\S+)\] "(?P<method>\S+) (?P<endpoint>\S+) (?P<protocol>\S+)" (?P<status>\d{3}) (?P<size>\d+)'
url = "https://pymite6941.github.io/portfolio-website/assets/test-data/server.log"
response = requests.get(url)
def check_pattern(key:str,value:str):
    for line in response.text.splitlines():
        match = re.search(pattern, line)
        if match:
            if match.group(key) == value:
                print(line)
while True:
    if response.status_code != 200:
        print("Failed to retrieve log data")
        break
    print("What is the desired action?...")
    choice = int(input("> "))
    if choice == 1:
        error_code = input("What error code are you looking for?\n> ")
        check_pattern("status", error_code)
    elif choice == 2:
        ip_address = input("What IP address are you looking for?\n> ")
        check_pattern("ip", ip_address)
    elif choice == 3:
        start_time = input("What is the start time? (format: dd/Mon/yyyy:HH:mm:ss)\n> ")
        end_time = input("What is the end time? (format: dd/Mon/yyyy:HH:mm:ss)\n> ")
        for line in response.text.splitlines():
            match = re.search(pattern, line)
            if match:
                log_time = match.group("date") + " " + match.group("time")
                if start_time <= log_time <= end_time:
                    print(line)
    elif choice == 4:
        method = input("What method are you looking for? (e.g. GET, POST)\n> ")
        check_pattern("method", method)
    elif choice == 5:
        break

Previous Day
Next Day