User Tools

Site Tools


linux:apachelogs

This is an old revision of the document!


Apache Logs

email unique php errors in apache logs:

phperrors.sh
#!/bin/bash
# Mail out PHP errors that are in the apache error log.
# Note PHP's log_errors must be turned on
# Ben Dowling - www.coderholic.com

errorLog=/var/log/apache2/error.log # Error log location
email=<email address> # Send report here

# Pull out the lines that mention PHP, and use AWK to get the column we're interested in
errors=$(cat $errorLog | grep PHP | awk -F'] ' '{print $5}')
# Remove referer information, sort, and remove duplicate entries
errors=$(echo "$errors" | awk -F', referer' '{print $1}' | sort | uniq)
# Check that we actually have some errors
if [ -n "$errors" ] 
then
    echo "$errors" | mail "$email" -s "Apps PHP Errors"
fi

Make executable

chmod 755 phperrors.sh

add to crontab

#PHP Errors
0 21 * * * /root/phperrors.sh >/dev/null 2>&1

OR

to apache logrotate in the prerotate section, ensuring the lines all terminate with backslash

    prerotate
        if [ -f /root/phperrors.sh ]; then \
            /root/phperrors.sh \
        fi; \
        ...other-prerotate-commands...
    endscript
linux/apachelogs.1596583705.txt.gz · Last modified: 2023/05/29 11:53 (external edit)