INNER CODE UNIT · Python
parse_caddy_log
lebinh/ngxtop · ngxtop/ngxtop.py:301
def parse_caddy_log(lines):
"""Parse Caddy JSON log format and convert to ngxtop's expected format."""
for line in lines:
try:
# Extract the JSON part of the line
# Caddy logs have format: timestamp INFO http.log.access.log2 handled request {json}
# Find "handled request" first, then find the JSON after it
handled_pos = line.find('handled request')
if handled_pos == -1:
# Try to parse as pure JSON if no "handled request" prefix
json_start = line.find('{')
else:
# Find the first { after "handled request"
json_start = line.find('{', handled_pos + len('handled request'))
if json_start == -1:
continue