Skip to content

Instantly share code, notes, and snippets.

@gjuric
Created December 11, 2014 14:13
Show Gist options
  • Save gjuric/e0c9e45efb3d15e3b949 to your computer and use it in GitHub Desktop.
Save gjuric/e0c9e45efb3d15e3b949 to your computer and use it in GitHub Desktop.
PHP-FPM log to syslog
/etc/php5/fpm/php-fpm.conf
error_log = syslog
syslog.ident = php-fpm
This will log to syslog PHP-FPM stuff that usually gets logged to /var/log/php5-fpm.log and is not that interesting:
... php-fpm[3537]: [NOTICE] fpm is running, pid 3537
... php-fpm[3537]: [NOTICE] ready to handle connections
... php-fpm[3537]: [NOTICE] Finishing ...
... php-fpm[3537]: [NOTICE] exiting, bye-bye!
... php-fpm[3573]: [NOTICE] fpm is running, pid 3573
... php-fpm[3573]: [NOTICE] ready to handle connections
... php-fpm[3573]: [NOTICE] Finishing ...
... php-fpm[3573]: [NOTICE] exiting, bye-bye!
... php-fpm[3607]: [NOTICE] fpm is running, pid 3607
... php-fpm[3607]: [NOTICE] ready to handle connections
What we want is to log all of the errors (and warnings and notices) to syslog.
For CLI scripts we do that by creating /etc/php5/cli/conf.d/99-custom.ini with:
error_log = syslog
PHP scripts ran from CLI will now log to syslog but will use "php" as syslog ident.
By setting "error_log = syslog" in /etc/php5/fpm/conf.d/99-custom.ini errors that happen during processing of php scripts via PHP-FPM will end up in syslog but the ident will be "ool <NAME-OF-THE-POOL>". So if you use a pool named www you will get messages into syslog with an ident of "ool www".
To change the syslog ident you have to run:
openlog('desired-ident', LOG_ODELAY, LOG_USER)
somewhere in the beginning of your scripts. The other option is to auto prepend the file with the following content:
```php
<?php
openlog('desired-ident', LOG_ODELAY, LOG_USER)
```
Save it under /etc/php5/fpm/syslog_prepend.php and at the end of your pool definition (eg. /etc/php5/fpm/pool.d/www.conf) add:
php_admin_value[auto_prepend_file] = /etc/php5/fpm/syslog_prepend.php
This way this file will be automatically prepended to every request that comed to PHP-FPM.
Links:
* https://bugs.php.net/bug.php?id=67764
* http://php.net/manual/en/errorfunc.configuration.php#116235
* http://stackoverflow.com/a/22580875
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment