You are here: Home / Debian GNU/Linux / Servers / Syslog / Log PHP errors with Syslog

Log PHP errors with Syslog

by Pierre-Yves Landuré last modified Aug 16, 2018 05:46

Function of the server configuration, the PHP messages are not logged to HTTP server error log. In this case, it is necessary to setup PHP to log errors to a dedicated log file, or to Syslog. This howto propose a simple setup to log PHP errors in the file "/var/log/php.log"

This howto is tested on :

  • Debian 6.0 Squeeze
  • Debian 7.0 Wheezy

Prerequisites

This howto need a working PHP 5 installation, for example the one described in Install PHP-FPM on Debian.

Installation

Rsyslog

Setup RSyslog (if available) to log PHP messages in a dedicated file :

if [ -d '/etc/rsyslog.d' ]; then
  echo '# Log PHP messages to /var/log/php.log.
:msg,regex,"^[w: ]*PHP " /var/log/php.log
:msg,startswith," [wrapped:" /var/log/php.log
# Uncomment the following to stop logging anything that matches the last rule.
# Doing this will stop logging PHP log messages to syslog default file.
& ~' > '/etc/rsyslog.d/40-php.conf'
fi

Create the PHP log file :

if [ -d '/etc/rsyslog.d' ]; then
command touch '/var/log/php.log'
command chown "$(command grep '$FileOwner' '/etc/rsyslog.conf' | command cut --delimiter=' ' --fields=2)":adm '/var/log/php.log'
command chmod 640 '/var/log/php.log'
fi

Reload RSyslog configuration :

test -x /etc/init.d/rsyslog && /etc/init.d/rsyslog force-reload

Syslog-NG

Provide the Syslog-NG configuration :

SYSLOG_NG_CONF='
########################
# PHP messages
########################

destination d_php { file("/var/log/php.log"); };
filter f_php { match("^[w: ]*PHP " value("MESSAGE")) or match("^[w: ]*\\[wrapped:" value("MESSAGE")); };
log { source(s_src); filter(f_php); destination(d_php); };'

Setup Syslog-NG (if available) to log PHP messages in a dedicated file :

if [ -d '/etc/syslog-ng/conf.d' ]; then
  echo "${SYSLOG_NG_CONF}" > '/etc/syslog-ng/conf.d/php.conf'
elif [ -e '/etc/syslog-ng/syslog-ng.conf' \
-a -z "$(command grep 'PHP messages' '/etc/syslog-ng/syslog-ng.conf' &2>'/dev/null')" ]; then
echo "${SYSLOG_NG_CONF}" >> '/etc/syslog-ng/syslog-ng.conf'
fi

Create the PHP log file :

if [ -d '/etc/syslog-ng/' ]; then
command touch '/var/log/php.log'
command chown root:adm '/var/log/php.log'
command chmod 640 '/var/log/php.log'
fi

Reload Syslog-NG configuration :

test -x /etc/init.d/syslog-ng && /etc/init.d/syslog-ng reload

PHP

Detect PHP extension configuration path:

MODS_CONF_PATH='/etc/php5/conf.d'
test -d '/etc/php5/mods-available' \
  && MODS_CONF_PATH='/etc/php5/mods-available'

Setup PHP to log errors to Syslog :

command echo "; PHP errors logged to syslog:
error_log = syslog" \
    > "${MODS_CONF_PATH}/syslog.ini"
test -n "$(command -v php5enmod)" && command php5enmod 'syslog/50'

Reload PHP configuration :

test -x /etc/init.d/php5-fpm && /etc/init.d/php5-fpm restart
test -x /etc/init.d/apache2 && /etc/init.d/apache2 force-reload
test -x /etc/init.d/lighttpd && /etc/init.d/lighttpd force-reload
test -x /etc/init.d/nginx && /etc/init.d/nginx force-reload

Display PHP errors with :

command tail -f '/var/log/php.log'

In addition to this setup, it is possible to setup e-mails alerts for PHP errors.

Logrotate

Setup the PHP error logs rotation.

echo "/var/log/php.log {
weekly
missingok
rotate 4
compress
delaycompress
notifempty
create 640 root adm
}

/var/log/php.log.slow {
weekly
missingok
rotate 4
compress
delaycompress
notifempty
create 644 www-data adm
}" \
 > "/etc/logrotate.d/php"

Thanks