5

When error_log() is run from within phpunit it isn't written to the normal error log file. I'd like to stop this so that it writes to file as if I was hitting PHP via a browser.

<?php

class exampleTest extends PHPUnit_Framework_TestCase {

    public function testSomething() {
        error_log('This will not be written to the error log, but I wish it was!');
        $this->assertEquals(2, 1+1);
    }

}

I am currently using php version 5.5, phpunit version 3.7. This happens both on osx and ubunutu. This does not happen on Windows 7.

Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
  • Judging by what [the manual](http://us2.php.net/manual/en/function.error-log.php) says, if you don't pass any parameters other than the message into `error_log()`, it'll save it to the PHP system logger, probably ends up in the tmp folder of your PHP installation. If you want to save it somewhere else you can specify a path like `error_log('Your message', 3, '/path/to/log/file');` – scrowler Aug 08 '14 at 01:55
  • with `log_errors = On` and `error_log = "/var/log/php/php_errors.log"` in my php.ini, error_log() writes to that file. However in PHP unit it doesn't. I don't want to have to adjust all of our error_log calls just to work around phpunit. – Aaron Silverman Aug 08 '14 at 18:34
  • The above test works for me on Ubuntu 13.10 running PHP 5.5.3 and PHPUnit 4.2.0. You may want to upgrade PHPUnit. It does output buffering in some cases (which has changed over the years), but I don't see how that could interfere with writing to the log file. Also, `var_dump` (or assert) both `log_errors` and `error_log` in the test to make sure they are what you expect. – David Harkness Aug 09 '14 at 20:40
  • Tried using phpunit 4.2 above test will write to console but not to the error log file. – Aaron Silverman Aug 10 '14 at 05:06

2 Answers2

6

So this turned out to be a symptom of php run from command line not logging errors (discussed here) and was solved by ensuring that the user executing phpunit has write permissions to the error log.

Also at play was the fact when invoked from the command line php used a different ini file (discussed here).

Community
  • 1
  • 1
Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
1

When executed by PHPUnit, the error_log() function writes to the PHP STDERR rather than the config of the project itself. This is resulted by an error handler defined by PHPUnit. This default behavior can be altered using a custom error handler.

Panduka
  • 153
  • 1
  • 10