0

I've got some logs gathered on a monitoring server using Syslog-NG which are :

  • copied into a local daily-rotated file,
  • stored into a MySQL database.

Unfortunately, no error is raised since they are evaluated as PHP notices (see this post).

Hence, I am wondering what's the right way to use Syslog-NG to handle PHP errors so that they are actually recognized as errors.

David
  • 2,603
  • 4
  • 18
  • 28

2 Answers2

0

Several things/options here. That article doesn't mention configuring php.ini to use syslog for error logging inside of php. You'll need to set

error_log = syslog

I'm assuming that was alluded to in the article where it says

PHP has a annoying habit of logging everything with the NOTICE error level.

You could turn on error logging to syslog that way and follow the syslog voodoo suggested in that article, or you could do it the PHP way, and by that I mean using more of PHP to implement the mapping between PHP errors & syslog error levels.

Define a custom error handler via set_error_handler, then within the handler, map a particular PHP error to some particular syslog error level eg

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    switch ($errno) {
    case E_USER_ERROR:
        $str = "<b>My ERROR</b> [$errno] $errstr<br />\n" .
               "  Fatal error on line $errline in file $errfile" .
               ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n" .
               "Aborting...<br />\n";
        syslog(LOG_CRIT, $errstr)
        exit(1);
        break;
    case E_USER_WARNING:
        // ...
}

It might also be a neat idea to do the same with set_exception_handler.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
0

I've noticed this post via my referral logs on my blog you linked. The problem with syslog is that you don't have any guarantee that your message will actually arrive to the log store. Granted, PHP could be patched to give you an error message, but if you need transactioned logs, you are a lot better off storing them into the database directly.

If you insist on doing it this way, here's a library I've helped develop that will replace the native syslog interface of PHP. It's written entirely in PHP so you don't need to compile anything.

I hope this helps.

Janos Pasztor
  • 1,265
  • 8
  • 16