15

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?
Jacco
  • 23,534
  • 17
  • 88
  • 105
  • One more option would be to set log filename as `error_log` php.ini directive (I think it worked if used as `ini_set('error_log', '/path/to/log/file')`) and using `trigger_error()` for generating error messages. This way, you would use PHP-level error logging (rather than OS-level error logging) and would not have to specify `$destination` as with `error_log()` function. – binaryLV Jun 20 '11 at 10:31
  • we use `trigger_error()` in combination with custom error handling functionality via `set_error_handler()`, we want this custom error handler to log to a file in the most efficient manner. – Jacco Jun 20 '11 at 10:38

1 Answers1

13

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.

Lumbendil
  • 2,906
  • 1
  • 19
  • 24
  • What is the SAPI logging handler? http://stackoverflow.com/questions/9948008/what-is-sapi-and-when-would-you-use-it might help. Do you have pros & cons as asked? The above is mostly a summary of http://php.net/manual/en/function.error-log.php . – Alastair Irvine May 10 '17 at 13:28