Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's error_log() vs syslog()

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

  • What are the pros and cons of those functions?
  • Which one to choose? (and why?)
  • What if I drop the requirement of multiple files?
like image 752
Jacco Avatar asked Jun 20 '11 10:06

Jacco


People also ask

What is PHP syslog?

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.

Where is PHP syslog?

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.

What is Error_log?

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.

How do I log messages in PHP?

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.

How do I enable error logging in PHP?

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.

What is syslog and how do I use it?

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!

Where are PHP error logs stored in Linux?

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.

How to write to syslog in apache2/php?

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!


1 Answers

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.

like image 111
Lumbendil Avatar answered Oct 05 '22 03:10

Lumbendil