0

Using monolog to log my exceptions:

try {
    // php code
} catch (\Exception $e) {
    $log->log(100, $e->getMessage());
}

I had problems when two parallel processes logging an error. Sometimes it worked correctly, but sometimes an error occurred saying that monolog log file “error.txt” could not be opened.

Using the native php error logging:

 ini_set('error_log', "error_log.txt");
    error_reporting(-1);

I had no problems so far.

Is it safe to make error logs with two parallel processes with this method ?

EDIT

$log refers to monolog Logger object:

$log = new Logger('error');
$log->pushHandler(new StreamHandler('error.txt', Logger::DEBUG));
Grigory Ilizirov
  • 1,030
  • 1
  • 8
  • 26
  • 1
    I don't know what `$log->log()` does but PHP has a built-in function for writing to its error log conveniently named [`error_log()`](http://php.net/manual/en/function.error-log.php) and https://stackoverflow.com/q/6375809/2191572 says it is safe for concurrency. – MonkeyZeus Sep 24 '18 at 15:11
  • yes, error_log() function logs to a file defined by ini_set('error_log', "error_log.txt") when no log file name is given in the parameters... – Grigory Ilizirov Sep 24 '18 at 15:18
  • There are two problems with trying to assess this issue. First, we don't really know what `$log` refers to; second, we don't know the exact wording of the error message, so it's not clear quite what's happening. Maybe there should be a `monolog` tag(?) – Chris Lear Sep 24 '18 at 15:19
  • Unless you expose the code behind the `log()` method we can only guess as to why your code is failing. – MonkeyZeus Sep 24 '18 at 15:21

0 Answers0