Just wanted to pass on a quick script to parse caddy’s JSON logs and get a daily dump of common logs daily into Telegram.
It is assumed that you know how to create a Telegram bot and retrieve token & chat ID:
#!/bin/bash # Initialise variables day=$(date +%Y-%m-%d) ytday=$(date -d "yesterday 13:00" '+%d/%b/%Y') logfile=/var/log/caddy/caddy.log filename=/var/log/caddy/$day-caddy.txt token=<TELEGRAM_TOKEN> chat_id=<TELERGAM_CHAT_ID> # Extraction of JSON info touch $filename chmod 666 $filename cat $logfile | jq -r " .. | .common_log? | select(. !=null and contains ("\"$ytday\"") and (contains ("\"<IP_TO_EXCLUDE_FROM_LOG>\"") | not))" >> $filename sed -i '1i\ ### Caddy log ###\ Range processed: yesterday\ Period: day\ Level of Output: INFO\ Type of Output: txt\ Host: $HOSTNAME' $filename # Telegram commands curl -F chat_id="$chat_id" -F text="$(head /var/log/caddy/$day-caddy.txt -n 6)" https://api.telegram.org/bot$token/sendMessage >/dev/null 2>&1 curl -F chat_id="$chat_id" -F document=@"/var/log/caddy/$day-caddy.txt" https://api.telegram.org/bot$token/sendDocument >/dev/null 2>&1
Hoping this can be helpful for some. I was lazy enough to parse and filter through the common_log but jq can help filter any field from the JSON file.