1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 06:06:40 +02:00

Merge branch '1.x'

This commit is contained in:
Jordi Boggiano
2019-11-11 18:34:16 +01:00
5 changed files with 135 additions and 39 deletions

View File

@@ -52,6 +52,10 @@
* Added support for the PHP 7.x `mongodb` extension in the MongoDBHandler * Added support for the PHP 7.x `mongodb` extension in the MongoDBHandler
* Fixed many minor issues in various handlers, and probably added a few regressions too * Fixed many minor issues in various handlers, and probably added a few regressions too
### 1.25.1 (2019-09-06)
* Fixed forward-compatible interfaces to be compatible with Monolog 1.x too.
### 1.25.0 (2019-09-06) ### 1.25.0 (2019-09-06)
* Deprecated SlackbotHandler, use SlackWebhookHandler or SlackHandler instead * Deprecated SlackbotHandler, use SlackWebhookHandler or SlackHandler instead

View File

@@ -137,7 +137,7 @@ class JsonFormatter extends NormalizerFormatter
return 'Over '.$this->maxNormalizeDepth.' levels deep, aborting normalization'; return 'Over '.$this->maxNormalizeDepth.' levels deep, aborting normalization';
} }
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data)) {
$normalized = []; $normalized = [];
$count = 1; $count = 1;

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\ResettableInterface; use Monolog\ResettableInterface;
use Monolog\Formatter\FormatterInterface;
/** /**
* Simple handler wrapper that filters records based on a list of levels * Simple handler wrapper that filters records based on a list of levels
@@ -22,7 +23,7 @@ use Monolog\ResettableInterface;
* @author Hennadiy Verkh * @author Hennadiy Verkh
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class FilterHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface class FilterHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface, FormattableHandlerInterface
{ {
use ProcessableHandlerTrait; use ProcessableHandlerTrait;
@@ -48,7 +49,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
protected $bubble; protected $bubble;
/** /**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $this). * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $filterHandler).
* @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided * @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
* @param int|string $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array * @param int|string $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
@@ -106,19 +107,11 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
return false; return false;
} }
// The same logic as in FingersCrossedHandler
if (!$this->handler instanceof HandlerInterface) {
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
if ($this->processors) { if ($this->processors) {
$record = $this->processRecord($record); $record = $this->processRecord($record);
} }
$this->handler->handle($record); $this->getHandler($record)->handle($record);
return false === $this->bubble; return false === $this->bubble;
} }
@@ -135,7 +128,44 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
} }
} }
$this->handler->handleBatch($filtered); $this->getHandler($filtered[count($filtered) - 1])->handleBatch($filtered);
}
/**
* Return the nested handler
*
* If the handler was provided as a factory callable, this will trigger the handler's instantiation.
*
* @return HandlerInterface
*/
public function getHandler(array $record = null)
{
if (!$this->handler instanceof HandlerInterface) {
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
return $this->handler;
}
/**
* {@inheritdoc}
*/
public function setFormatter(FormatterInterface $formatter): HandlerInterface
{
$this->getHandler()->setFormatter($formatter);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFormatter(): FormatterInterface
{
return $this->getHandler()->getFormatter();
} }
public function reset() public function reset()

View File

@@ -15,6 +15,7 @@ use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface; use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
use Monolog\Logger; use Monolog\Logger;
use Monolog\ResettableInterface; use Monolog\ResettableInterface;
use Monolog\Formatter\FormatterInterface;
/** /**
* Buffers all records until a certain level is reached * Buffers all records until a certain level is reached
@@ -32,7 +33,7 @@ use Monolog\ResettableInterface;
* *
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class FingersCrossedHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface class FingersCrossedHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface, FormattableHandlerInterface
{ {
use ProcessableHandlerTrait; use ProcessableHandlerTrait;
@@ -46,7 +47,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
protected $bubble; protected $bubble;
/** /**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler). * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $fingersCrossedHandler).
* @param int|string|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action, or a level name/value at which the handler is activated * @param int|string|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action, or a level name/value at which the handler is activated
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
@@ -95,15 +96,8 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
if ($this->stopBuffering) { if ($this->stopBuffering) {
$this->buffering = false; $this->buffering = false;
} }
if (!$this->handler instanceof HandlerInterface) {
$record = end($this->buffer) ?: null;
$this->handler = call_user_func($this->handler, $record, $this); $this->getHandler(end($this->buffer) ?: null)->handleBatch($this->buffer);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
$this->handler->handleBatch($this->buffer);
$this->buffer = []; $this->buffer = [];
} }
@@ -125,7 +119,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
$this->activate(); $this->activate();
} }
} else { } else {
$this->handler->handle($record); $this->getHandler($record)->handle($record);
} }
return false === $this->bubble; return false === $this->bubble;
@@ -147,8 +141,8 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
$this->resetProcessors(); $this->resetProcessors();
if ($this->handler instanceof ResettableInterface) { if ($this->getHandler() instanceof ResettableInterface) {
$this->handler->reset(); $this->getHandler()->reset();
} }
} }
@@ -174,11 +168,48 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
return $record['level'] >= $level; return $record['level'] >= $level;
}); });
if (count($this->buffer) > 0) { if (count($this->buffer) > 0) {
$this->handler->handleBatch($this->buffer); $this->getHandler(end($this->buffer) ?: null)->handleBatch($this->buffer);
} }
} }
$this->buffer = []; $this->buffer = [];
$this->buffering = true; $this->buffering = true;
} }
/**
* Return the nested handler
*
* If the handler was provided as a factory callable, this will trigger the handler's instantiation.
*
* @return HandlerInterface
*/
public function getHandler(array $record = null)
{
if (!$this->handler instanceof HandlerInterface) {
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
return $this->handler;
}
/**
* {@inheritdoc}
*/
public function setFormatter(FormatterInterface $formatter): HandlerInterface
{
$this->getHandler()->setFormatter($formatter);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFormatter(): FormatterInterface
{
return $this->getHandler()->getFormatter();
}
} }

View File

@@ -11,6 +11,8 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
/** /**
* Sampling handler * Sampling handler
* *
@@ -25,7 +27,7 @@ namespace Monolog\Handler;
* @author Bryan Davis <bd808@wikimedia.org> * @author Bryan Davis <bd808@wikimedia.org>
* @author Kunal Mehta <legoktm@gmail.com> * @author Kunal Mehta <legoktm@gmail.com>
*/ */
class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
{ {
use ProcessableHandlerTrait; use ProcessableHandlerTrait;
@@ -40,7 +42,7 @@ class SamplingHandler extends AbstractHandler implements ProcessableHandlerInter
protected $factor; protected $factor;
/** /**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler). * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $samplingHandler).
* @param int $factor Sample factor (e.g. 10 means every ~10th record is sampled) * @param int $factor Sample factor (e.g. 10 means every ~10th record is sampled)
*/ */
public function __construct($handler, int $factor) public function __construct($handler, int $factor)
@@ -56,27 +58,56 @@ class SamplingHandler extends AbstractHandler implements ProcessableHandlerInter
public function isHandling(array $record): bool public function isHandling(array $record): bool
{ {
return $this->handler->isHandling($record); return $this->getHandler($record)->isHandling($record);
} }
public function handle(array $record): bool public function handle(array $record): bool
{ {
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) { if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
// The same logic as in FingersCrossedHandler
if (!$this->handler instanceof HandlerInterface) {
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
if ($this->processors) { if ($this->processors) {
$record = $this->processRecord($record); $record = $this->processRecord($record);
} }
$this->handler->handle($record); $this->getHandler($record)->handle($record);
} }
return false === $this->bubble; return false === $this->bubble;
} }
/**
* Return the nested handler
*
* If the handler was provided as a factory callable, this will trigger the handler's instantiation.
*
* @return HandlerInterface
*/
public function getHandler(array $record = null)
{
if (!$this->handler instanceof HandlerInterface) {
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
return $this->handler;
}
/**
* {@inheritdoc}
*/
public function setFormatter(FormatterInterface $formatter): HandlerInterface
{
$this->getHandler()->setFormatter($formatter);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFormatter(): FormatterInterface
{
return $this->getHandler()->getFormatter();
}
} }