icalvete
icalvete

Reputation: 1159

Using syslog facilities with php error_log

Is posible use syslog facilities in php error_log directive or in other way at system/server side?

Something like...

error_log = syslog:local4
error_log = syslog(LOG_LOCAL4)

Using php fpm I can set this with syslog.facility directive in fpm conf, but what about of php cli?

Thanks

Upvotes: 0

Views: 4499

Answers (2)

Daniel Bradley
Daniel Bradley

Reputation: 106

You can configure error_log content to go to syslog by configuring it in either the CLI or Apache php.ini file; or on a per site basis in your Apache configuration files.

Warning, the following will likely cause your 'error_log' messages to be logged to '/var/log/syslog'. To prevent this, edit '/etc/rsyslog.d/50-defaults.conf', and change:

*.*;auth,authpriv.none      -/var/log/syslog
*.*;auth,authpriv.none;local6.none      -/var/log/syslog

You may then log your error_log messages to another file by adding a new rule such as:

local6.*                          /var/log/local6.log

or

:syslogtag, isequal, "YOUR_IDENTIFIER:" /var/log/YOUR_IDENTIFIER.log

PHP site wide

For example, to configure all websites to log to syslog, edit the following file:

/etc/php/7.4/apache2/php.ini

Search for, then edit the following values that will be commented out by default:

error_log       = syslog
syslog.ident    = YOUR_IDENTIFIER
syslog.facility = local6

The above will cause error_log() calls to be logged to "YOUR_IDENTIFIER.log".

Alternatively, for PHP command line programs you can edit:

/etc/php/7.4/php/php.ini

Apache website specific logging

However, when using Apache, you probably want to log to separate files. In that case, you only need to update your site specific Apache config file.

Add an appropriate Directory section, and then use the 'php_value' and 'php_admin_value' directives to set these values. For example:

<VirtualHost *:80>
...
   <Directory /path/to/website/>
   php_value       error_log       syslog
   php_admin_value syslog.ident    YOUR_IDENTIFIER
   php_admin_value syslog.facility local6
   </Directory>
...
</VirtualHost>

Alternatively, you can use the 'PHPINIDir' directive to make the website use a completely different 'php.ini' file. For example:

<VirtualHost *.80>
...
   PHPINIDir /etc/apache/my_website
...
</VirtualHost>

Where the 'my_website' directory contains a copy of 'php.ini' that has been appropriately edited.

References

  1. Log Files - Apache HTTP Server Version 2.4

  2. How to change configuration settings (PHP Manual)

  3. List of php.ini directives (PHP Manaul)

  4. How to specify a customer php.ini for a website

Upvotes: 0

icalvete
icalvete

Reputation: 1159

Php default syslog's facility is "user" (and cannot be changed)

I use directive auto_prepend_file in phi.ini (this script must be under include_path)

auto_prepend_file = log.php

and

root@sp:/etc/php5/cli# cat /usr/share/php5/log.php
<?php

openlog('php-cli', 0, LOG_LOCAL4);

Upvotes: 3

Related Questions