0

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

2
  • stackoverflow.com/questions/6469405/… Commented Mar 21, 2014 at 20:44
  • TheClair, I know how send errors to syslog, I want know how if is posible choose facility (local4 for example). I edit the question to explain this. Thanks. Commented Mar 22, 2014 at 6:31

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

The facility setting from openlog() seems to persist even after closelog(), so if one does not care about the tag/ident, closelog() may be called just after openlog(). Not sure if that matters as LOG_ODELAY is default. I just noticed a report on php.net/manual/en/function.syslog.php about a problem with apache-syslog messages being given the same tag as php-messages in the same thread when leaving syslog open.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.