From b80352368cd79c978a805a76b724618ccb4ba465 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 4 Nov 2018 22:15:25 +0100 Subject: [PATCH] Add Logger::close and clarify what close and reset do plus --- src/Monolog/Handler/AbstractHandler.php | 2 - src/Monolog/Handler/BrowserConsoleHandler.php | 5 +++ src/Monolog/Handler/BufferHandler.php | 2 + src/Monolog/Handler/FingersCrossedHandler.php | 38 +++++++++++-------- src/Monolog/Handler/RollbarHandler.php | 12 ++++++ src/Monolog/Handler/RotatingFileHandler.php | 12 ++++++ src/Monolog/Logger.php | 29 ++++++++++++++ src/Monolog/ResettableInterface.php | 10 ++++- 8 files changed, 90 insertions(+), 20 deletions(-) diff --git a/src/Monolog/Handler/AbstractHandler.php b/src/Monolog/Handler/AbstractHandler.php index b286a01c..92b9d458 100644 --- a/src/Monolog/Handler/AbstractHandler.php +++ b/src/Monolog/Handler/AbstractHandler.php @@ -177,8 +177,6 @@ abstract class AbstractHandler implements HandlerInterface, ResettableInterface public function reset() { - $this->close(); - foreach ($this->processors as $processor) { if ($processor instanceof ResettableInterface) { $processor->reset(); diff --git a/src/Monolog/Handler/BrowserConsoleHandler.php b/src/Monolog/Handler/BrowserConsoleHandler.php index 14fa0dc7..23cf23ba 100644 --- a/src/Monolog/Handler/BrowserConsoleHandler.php +++ b/src/Monolog/Handler/BrowserConsoleHandler.php @@ -73,6 +73,11 @@ class BrowserConsoleHandler extends AbstractProcessingHandler } } + public function close() + { + self::resetStatic(); + } + public function reset() { self::resetStatic(); diff --git a/src/Monolog/Handler/BufferHandler.php b/src/Monolog/Handler/BufferHandler.php index ca2c7a79..61d1b50c 100644 --- a/src/Monolog/Handler/BufferHandler.php +++ b/src/Monolog/Handler/BufferHandler.php @@ -118,6 +118,8 @@ class BufferHandler extends AbstractHandler public function reset() { + $this->flush(); + parent::reset(); if ($this->handler instanceof ResettableInterface) { diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index a2d85dde..275fd513 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -131,27 +131,14 @@ class FingersCrossedHandler extends AbstractHandler */ public function close() { - if (null !== $this->passthruLevel) { - $level = $this->passthruLevel; - $this->buffer = array_filter($this->buffer, function ($record) use ($level) { - return $record['level'] >= $level; - }); - if (count($this->buffer) > 0) { - $this->handler->handleBatch($this->buffer); - $this->buffer = array(); - } - } + $this->flushBuffer(); } - /** - * Resets the state of the handler. Stops forwarding records to the wrapped handler. - */ public function reset() { - parent::reset(); + $this->flushBuffer(); - $this->buffer = array(); - $this->buffering = true; + parent::reset(); if ($this->handler instanceof ResettableInterface) { $this->handler->reset(); @@ -168,4 +155,23 @@ class FingersCrossedHandler extends AbstractHandler $this->buffer = array(); $this->reset(); } + + /** + * Resets the state of the handler. Stops forwarding records to the wrapped handler. + */ + private function flushBuffer() + { + if (null !== $this->passthruLevel) { + $level = $this->passthruLevel; + $this->buffer = array_filter($this->buffer, function ($record) use ($level) { + return $record['level'] >= $level; + }); + if (count($this->buffer) > 0) { + $this->handler->handleBatch($this->buffer); + } + } + + $this->buffer = array(); + $this->buffering = true; + } } diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 6c8a3e3e..65073ffe 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -129,4 +129,16 @@ class RollbarHandler extends AbstractProcessingHandler { $this->flush(); } + + /** + * {@inheritdoc} + */ + public function reset() + { + $this->flush(); + + parent::reset(); + } + + } diff --git a/src/Monolog/Handler/RotatingFileHandler.php b/src/Monolog/Handler/RotatingFileHandler.php index cc9fd4d3..ae2309f8 100644 --- a/src/Monolog/Handler/RotatingFileHandler.php +++ b/src/Monolog/Handler/RotatingFileHandler.php @@ -66,6 +66,18 @@ class RotatingFileHandler extends StreamHandler } } + /** + * {@inheritdoc} + */ + public function reset() + { + parent::reset(); + + if (true === $this->mustRotate) { + $this->rotate(); + } + } + public function setFilenameFormat($filenameFormat, $dateFormat) { if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) { diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index d998bb65..05dfc817 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -354,6 +354,35 @@ class Logger implements LoggerInterface, ResettableInterface return true; } + /** + * Ends a log cycle and frees all resources used by handlers. + * + * Closing a Handler means flushing all buffers and freeing any open resources/handles. + * Handlers that have been closed should be able to accept log records again and re-open + * themselves on demand, but this may not always be possible depending on implementation. + * + * This is useful at the end of a request and will be called automatically on every handler + * when they get destructed. + */ + public function close() + { + foreach ($this->handlers as $handler) { + if (method_exists($handler, 'close')) { + $handler->close(); + } + } + } + + /** + * Ends a log cycle and resets all handlers and processors to their initial state. + * + * Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal + * state, and getting it back to a state in which it can receive log records again. + * + * This is useful in case you want to avoid logs leaking between two requests or jobs when you + * have a long running process like a worker or an application server serving multiple requests + * in one process. + */ public function reset() { foreach ($this->handlers as $handler) { diff --git a/src/Monolog/ResettableInterface.php b/src/Monolog/ResettableInterface.php index 5e7bd6f3..635bc77d 100644 --- a/src/Monolog/ResettableInterface.php +++ b/src/Monolog/ResettableInterface.php @@ -14,8 +14,14 @@ namespace Monolog; /** * Handler or Processor implementing this interface will be reset when Logger::reset() is called. * - * Resetting an Handler or a Processor usually means cleaning all buffers or - * resetting in its internal state. + * Resetting ends a log cycle gets them back to their initial state. + * + * Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal + * state, and getting it back to a state in which it can receive log records again. + * + * This is useful in case you want to avoid logs leaking between two requests or jobs when you + * have a long running process like a worker or an application server serving multiple requests + * in one process. * * @author Grégoire Pineau */