Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5875 from youngguns-nl/LoggerRegisterShutDown

Logger register shut down

Conflicts:
	library/Zend/Log/Logger.php
  • Loading branch information...
commit f0f89971cf42f9842f697bb41223674014d525d3 2 parents 667ea76 + af3a04a
@weierophinney weierophinney authored
Showing with 69 additions and 0 deletions.
  1. +46 −0 library/Zend/Log/Logger.php
  2. +23 −0 tests/ZendTest/Log/LoggerTest.php
View
46 library/Zend/Log/Logger.php
@@ -61,6 +61,13 @@ class Logger implements LoggerInterface
protected static $registeredErrorHandler = false;
/**
+ * Registered shutdown error handler
+ *
+ * @var bool
+ */
+ protected static $registeredFatalErrorShutdownFunction = false;
+
+ /**
* Registered exception handler
*
* @var bool
@@ -173,6 +180,10 @@ public function __construct($options = null)
if (isset($options['errorhandler']) && $options['errorhandler'] === true) {
static::registerErrorHandler($this);
}
+
+ if (isset($options['fatal_error_shutdownfunction']) && $options['fatal_error_shutdownfunction'] === true) {
+ static::registerFatalErrorShutdownFunction($this);
+ }
}
/**
@@ -568,6 +579,41 @@ public static function unregisterErrorHandler()
static::$registeredErrorHandler = false;
}
+
+ /**
+ * Register a shutdown handler to log fatal errors
+ *
+ * @link http://www.php.net/manual/function.register-shutdown-function.php
+ * @param Logger $logger
+ * @return bool
+ */
+ public static function registerFatalErrorShutdownFunction(Logger $logger)
+ {
+ // Only register once per instance
+ if (static::$registeredFatalErrorShutdownFunction) {
+ return false;
+ }
+
+ $errorPriorityMap = static::$errorPriorityMap;
+
+ register_shutdown_function(function () use ($logger, $errorPriorityMap) {
+ $error = error_get_last();
+ if (null !== $error && $error['type'] === E_ERROR) {
+ $logger->log($errorPriorityMap[E_ERROR],
+ $error['message'],
+ array(
+ 'file' => $error['file'],
+ 'line' => $error['line']
+ )
+ );
+ }
+ });
+
+ static::$registeredFatalErrorShutdownFunction = true;
+ return true;
+ }
+
+
/**
* Register logging system as an exception handler to log PHP exceptions
*
View
23 tests/ZendTest/Log/LoggerTest.php
@@ -430,4 +430,27 @@ public function testErrorHandlerWithStreamWriter()
$this->assertContains('test', $contents);
$this->assertContains('second', $contents);
}
+
+ /**
+ * @runInSeparateProcess
+ */
+ public function testRegisterFatalShutdownFunction()
+ {
+ $writer = new MockWriter;
+ $this->logger->addWriter($writer);
+
+ $result = Logger::registerFatalErrorShutdownFunction($this->logger);
+ $this->assertTrue($result);
+
+ // check for single error handler instance
+ $this->assertFalse(Logger::registerFatalErrorShutdownFunction($this->logger));
+
+ $self = $this;
+ register_shutdown_function(function () use ($writer, $self) {
+ $self->assertEquals($writer->events[0]['message'], 'Call to undefined method ZendTest\Log\LoggerTest::callToNonExistingMethod()');
+ });
+
+ // Temporary hide errors, because we don't want the fatal error to fail the test
+ @$this->callToNonExistingMethod();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.