I'm trying to decide what functionality to use for logging to a custom file.
Background
We have several PHP processes, both running as Apaches (mod_php) and as Deamons (CLI, forked). I would like to be able to specify a log file per process/task to write to. For both the Apache processes as the Deamons, multiple processes will be writing to the same file.
Options
PHP offers both error_log()
and syslog()
. Both seem to offer more or less the same functionality.
My question
syslog() generates a log message that will be distributed by the system logger. For information on setting up a user defined log handler, see the syslog. conf (5) Unix manual page. More information on the syslog facilities and option can be found in the man pages for syslog (3) on Unix machines.
If the syslog is used, then all PHP errors will be sent directly to the default system log file—in Linux, this is typically /var/log/syslog.
An error log is a file that contains detailed records of error conditions a computer software encounters when it's running. The name is generic: sometimes, an application can log non-error type messages in its error log. However, error logs are generally meant to record only error messages generated by a program.
Log messages can be generated manually by calling error_log() or automatically when notices, warnings, or errors come up during execution. By default, the error log in PHP is disabled. You can enable the error log in one of two ways: by editing php. ini or by using ini_set.
By default, PHP doesn't log any errors, which means that this value must be explicitly set. To do so, open up the same PHP configuration file referenced above in your favorite editor and find the error_log directive. ; Log errors to specified file.
An open specification, syslog support can be written into any language or application, as has already been done with PHP. On a typical server, syslog-managed log files are stored in /var/log/. Keep in mind that you’re not required to follow this practice; in fact, syslog even allows you to write locally-generated messages on a remote server!
If the syslog is used, then all PHP errors will be sent directly to the default system log file—in Linux, this is typically /var/log/syslog. The more manageable method is to use a custom log file.
So for apache2/php to be able to write into syslog, you must add user www-data to group mysyslog: addgroup www-data mysyslog Thanks for contributing an answer to Stack Overflow!
syslog
sends the message to the OS logger, while error_log
has multiple options, either to the OS logger, to an email, to a file, or to the SAPI logging handler, as is stated in the documentation.
Since you say yo want to write on multiple logs, I'd recommend error_log
with $message_type = 3
, wich lets you add messages to the file set in the $destination
parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With