|
|
@@ -15,6 +15,7 @@ use Monolog\Handler\HandlerInterface;
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
use Psr\Log\InvalidArgumentException;
|
|
|
+use Exception;
|
|
|
|
|
|
/**
|
|
|
* Monolog log channel
|
|
|
@@ -133,6 +134,11 @@ class Logger implements LoggerInterface
|
|
|
*/
|
|
|
protected $microsecondTimestamps = true;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var callable
|
|
|
+ */
|
|
|
+ protected $exceptionHandler;
|
|
|
+
|
|
|
/**
|
|
|
* @param string $name The logging channel
|
|
|
* @param HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first, etc.
|
|
|
@@ -141,7 +147,7 @@ class Logger implements LoggerInterface
|
|
|
public function __construct($name, array $handlers = array(), array $processors = array())
|
|
|
{
|
|
|
$this->name = $name;
|
|
|
- $this->handlers = $handlers;
|
|
|
+ $this->setHandlers($handlers);
|
|
|
$this->processors = $processors;
|
|
|
}
|
|
|
|
|
|
@@ -281,7 +287,7 @@ class Logger implements LoggerInterface
|
|
|
* @param int $level The logging level
|
|
|
* @param string $message The log message
|
|
|
* @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addRecord($level, $message, array $context = array())
|
|
|
{
|
|
|
@@ -329,16 +335,20 @@ class Logger implements LoggerInterface
|
|
|
'extra' => array(),
|
|
|
);
|
|
|
|
|
|
- foreach ($this->processors as $processor) {
|
|
|
- $record = call_user_func($processor, $record);
|
|
|
- }
|
|
|
-
|
|
|
- while ($handler = current($this->handlers)) {
|
|
|
- if (true === $handler->handle($record)) {
|
|
|
- break;
|
|
|
+ try {
|
|
|
+ foreach ($this->processors as $processor) {
|
|
|
+ $record = call_user_func($processor, $record);
|
|
|
}
|
|
|
|
|
|
- next($this->handlers);
|
|
|
+ while ($handler = current($this->handlers)) {
|
|
|
+ if (true === $handler->handle($record)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ next($this->handlers);
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->handleException($e, $record);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
@@ -347,9 +357,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the DEBUG level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addDebug($message, array $context = array())
|
|
|
{
|
|
|
@@ -359,9 +369,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the INFO level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addInfo($message, array $context = array())
|
|
|
{
|
|
|
@@ -371,9 +381,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the NOTICE level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addNotice($message, array $context = array())
|
|
|
{
|
|
|
@@ -383,9 +393,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the WARNING level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addWarning($message, array $context = array())
|
|
|
{
|
|
|
@@ -395,9 +405,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the ERROR level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addError($message, array $context = array())
|
|
|
{
|
|
|
@@ -407,9 +417,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the CRITICAL level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addCritical($message, array $context = array())
|
|
|
{
|
|
|
@@ -419,9 +429,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the ALERT level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addAlert($message, array $context = array())
|
|
|
{
|
|
|
@@ -431,9 +441,9 @@ class Logger implements LoggerInterface
|
|
|
/**
|
|
|
* Adds a log record at the EMERGENCY level.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function addEmergency($message, array $context = array())
|
|
|
{
|
|
|
@@ -484,7 +494,7 @@ class Logger implements LoggerInterface
|
|
|
* Checks whether the Logger has a handler that listens on the given level
|
|
|
*
|
|
|
* @param int $level
|
|
|
- * @return Boolean
|
|
|
+ * @return bool
|
|
|
*/
|
|
|
public function isHandling($level)
|
|
|
{
|
|
|
@@ -501,15 +511,52 @@ class Logger implements LoggerInterface
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Set a custom exception handler
|
|
|
+ *
|
|
|
+ * @param callable $callback
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function setExceptionHandler($callback)
|
|
|
+ {
|
|
|
+ if (!is_callable($callback)) {
|
|
|
+ throw new \InvalidArgumentException('Exception handler must be valid callable (callback or object with an __invoke method), '.var_export($callback, true).' given');
|
|
|
+ }
|
|
|
+ $this->exceptionHandler = $callback;
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return callable
|
|
|
+ */
|
|
|
+ public function getExceptionHandler()
|
|
|
+ {
|
|
|
+ return $this->exceptionHandler;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Delegates exception management to the custom exception handler,
|
|
|
+ * or throws the exception if no custom handler is set.
|
|
|
+ */
|
|
|
+ protected function handleException(Exception $e, array $record)
|
|
|
+ {
|
|
|
+ if (!$this->exceptionHandler) {
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+
|
|
|
+ call_user_func($this->exceptionHandler, $e, $record);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Adds a log record at an arbitrary level.
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
* @param mixed $level The log level
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function log($level, $message, array $context = array())
|
|
|
{
|
|
|
@@ -523,9 +570,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function debug($message, array $context = array())
|
|
|
{
|
|
|
@@ -537,9 +584,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function info($message, array $context = array())
|
|
|
{
|
|
|
@@ -551,9 +598,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function notice($message, array $context = array())
|
|
|
{
|
|
|
@@ -565,9 +612,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function warn($message, array $context = array())
|
|
|
{
|
|
|
@@ -579,9 +626,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function warning($message, array $context = array())
|
|
|
{
|
|
|
@@ -593,9 +640,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function err($message, array $context = array())
|
|
|
{
|
|
|
@@ -607,9 +654,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function error($message, array $context = array())
|
|
|
{
|
|
|
@@ -621,9 +668,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function crit($message, array $context = array())
|
|
|
{
|
|
|
@@ -635,9 +682,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function critical($message, array $context = array())
|
|
|
{
|
|
|
@@ -649,9 +696,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function alert($message, array $context = array())
|
|
|
{
|
|
|
@@ -663,9 +710,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function emerg($message, array $context = array())
|
|
|
{
|
|
|
@@ -677,9 +724,9 @@ class Logger implements LoggerInterface
|
|
|
*
|
|
|
* This method allows for compatibility with common interfaces.
|
|
|
*
|
|
|
- * @param string $message The log message
|
|
|
- * @param array $context The log context
|
|
|
- * @return Boolean Whether the record has been processed
|
|
|
+ * @param string $message The log message
|
|
|
+ * @param array $context The log context
|
|
|
+ * @return bool Whether the record has been processed
|
|
|
*/
|
|
|
public function emergency($message, array $context = array())
|
|
|
{
|