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

Add Logger::close and clarify what close and reset do plus

This commit is contained in:
Jordi Boggiano
2018-11-04 22:15:25 +01:00
parent 712c5dacf6
commit b80352368c
8 changed files with 90 additions and 20 deletions

View File

@@ -177,8 +177,6 @@ abstract class AbstractHandler implements HandlerInterface, ResettableInterface
public function reset() public function reset()
{ {
$this->close();
foreach ($this->processors as $processor) { foreach ($this->processors as $processor) {
if ($processor instanceof ResettableInterface) { if ($processor instanceof ResettableInterface) {
$processor->reset(); $processor->reset();

View File

@@ -73,6 +73,11 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
} }
} }
public function close()
{
self::resetStatic();
}
public function reset() public function reset()
{ {
self::resetStatic(); self::resetStatic();

View File

@@ -118,6 +118,8 @@ class BufferHandler extends AbstractHandler
public function reset() public function reset()
{ {
$this->flush();
parent::reset(); parent::reset();
if ($this->handler instanceof ResettableInterface) { if ($this->handler instanceof ResettableInterface) {

View File

@@ -131,27 +131,14 @@ class FingersCrossedHandler extends AbstractHandler
*/ */
public function close() public function close()
{ {
if (null !== $this->passthruLevel) { $this->flushBuffer();
$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();
}
}
} }
/**
* Resets the state of the handler. Stops forwarding records to the wrapped handler.
*/
public function reset() public function reset()
{ {
parent::reset(); $this->flushBuffer();
$this->buffer = array(); parent::reset();
$this->buffering = true;
if ($this->handler instanceof ResettableInterface) { if ($this->handler instanceof ResettableInterface) {
$this->handler->reset(); $this->handler->reset();
@@ -168,4 +155,23 @@ class FingersCrossedHandler extends AbstractHandler
$this->buffer = array(); $this->buffer = array();
$this->reset(); $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;
}
} }

View File

@@ -129,4 +129,16 @@ class RollbarHandler extends AbstractProcessingHandler
{ {
$this->flush(); $this->flush();
} }
/**
* {@inheritdoc}
*/
public function reset()
{
$this->flush();
parent::reset();
}
} }

View File

@@ -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) public function setFilenameFormat($filenameFormat, $dateFormat)
{ {
if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) { if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) {

View File

@@ -354,6 +354,35 @@ class Logger implements LoggerInterface, ResettableInterface
return true; 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() public function reset()
{ {
foreach ($this->handlers as $handler) { foreach ($this->handlers as $handler) {

View File

@@ -14,8 +14,14 @@ namespace Monolog;
/** /**
* Handler or Processor implementing this interface will be reset when Logger::reset() is called. * 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 ends a log cycle gets them back to their initial state.
* resetting in its internal 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 <lyrixx@lyrixx.info> * @author Grégoire Pineau <lyrixx@lyrixx.info>
*/ */