Skip to content

Commit

Permalink
Merge pull request #997 from lyrixx/resetable
Browse files Browse the repository at this point in the history
Added a new ResettableInterface and implemented it where possible.
  • Loading branch information
Seldaek authored Nov 4, 2018
2 parents d64fd10 + 531d05a commit a03084c
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Fixed table row styling issues in HtmlFormatter
* Fixed RavenHandler dropping the message when logging exception
* Fixed WhatFailureGroupHandler skipping processors when using handleBatch
* Added a `ResettableInterface` in order to reset/reset/clear/flush handlers and processors
and implement it where possible

### 1.23.0 (2017-06-19)

Expand Down
16 changes: 14 additions & 2 deletions src/Monolog/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

namespace Monolog\Handler;

use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\ResettableInterface;

/**
* Base Handler class providing the Handler structure
*
* @author Jordi Boggiano <[email protected]>
*/
abstract class AbstractHandler implements HandlerInterface
abstract class AbstractHandler implements HandlerInterface, ResettableInterface
{
protected $level = Logger::DEBUG;
protected $bubble = true;
Expand Down Expand Up @@ -174,6 +175,17 @@ public function __destruct()
}
}

public function reset()
{
$this->close();

foreach ($this->processors as $processor) {
if ($processor instanceof ResettableInterface) {
$processor->reset();
}
}
}

/**
* Gets the default formatter.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Monolog/Handler/AbstractProcessingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Monolog\Handler;

use Monolog\ResettableInterface;

/**
* Base Handler class providing the Handler structure
*
Expand Down
9 changes: 7 additions & 2 deletions src/Monolog/Handler/BrowserConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ public static function send()
} elseif ($format === 'js') {
static::writeOutput(static::generateScript());
}
static::reset();
static::resetStatic();
}
}

public function reset()
{
self::resetStatic();
}

/**
* Forget all logged records
*/
public static function reset()
public static function resetStatic()
{
static::$records = array();
}
Expand Down
10 changes: 10 additions & 0 deletions src/Monolog/Handler/BufferHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Monolog\Handler;

use Monolog\Logger;
use Monolog\ResettableInterface;

/**
* Buffers all records until closing the handler and then pass them as batch.
Expand Down Expand Up @@ -114,4 +115,13 @@ public function clear()
$this->bufferSize = 0;
$this->buffer = array();
}

public function reset()
{
parent::reset();

if ($this->handler instanceof ResettableInterface) {
$this->handler->reset();
}
}
}
8 changes: 8 additions & 0 deletions src/Monolog/Handler/FingersCrossedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
use Monolog\Logger;
use Monolog\ResettableInterface;

/**
* Buffers all records until a certain level is reached
Expand Down Expand Up @@ -147,7 +148,14 @@ public function close()
*/
public function reset()
{
parent::reset();

$this->buffer = array();
$this->buffering = true;

if ($this->handler instanceof ResettableInterface) {
$this->handler->reset();
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Monolog/Handler/GroupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Monolog\Handler;

use Monolog\Formatter\FormatterInterface;
use Monolog\ResettableInterface;

/**
* Forwards records to multiple handlers
Expand Down Expand Up @@ -90,6 +91,17 @@ public function handleBatch(array $records)
}
}

public function reset()
{
parent::reset();

foreach ($this->handlers as $handler) {
if ($handler instanceof ResettableInterface) {
$handler->reset();
}
}
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 9 additions & 1 deletion src/Monolog/Handler/HandlerWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Monolog\Handler;

use Monolog\ResettableInterface;
use Monolog\Formatter\FormatterInterface;

/**
Expand All @@ -30,7 +31,7 @@
*
* @author Alexey Karapetov <[email protected]>
*/
class HandlerWrapper implements HandlerInterface
class HandlerWrapper implements HandlerInterface, ResettableInterface
{
/**
* @var HandlerInterface
Expand Down Expand Up @@ -105,4 +106,11 @@ public function getFormatter()
{
return $this->handler->getFormatter();
}

public function reset()
{
if ($this->handler instanceof ResettableInterface) {
return $this->handler->reset();
}
}
}
17 changes: 16 additions & 1 deletion src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author Jordi Boggiano <[email protected]>
*/
class Logger implements LoggerInterface
class Logger implements LoggerInterface, ResettableInterface
{
/**
* Detailed debug information
Expand Down Expand Up @@ -354,6 +354,21 @@ public function addRecord($level, $message, array $context = array())
return true;
}

public function reset()
{
foreach ($this->handlers as $handler) {
if ($handler instanceof ResettableInterface) {
$handler->reset();
}
}

foreach ($this->processors as $processor) {
if ($processor instanceof ResettableInterface) {
$processor->reset();
}
}
}

/**
* Adds a log record at the DEBUG level.
*
Expand Down
17 changes: 15 additions & 2 deletions src/Monolog/Processor/UidProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

namespace Monolog\Processor;

use Monolog\ResettableInterface;

/**
* Adds a unique identifier into records
*
* @author Simon Mönch <[email protected]>
*/
class UidProcessor implements ProcessorInterface
class UidProcessor implements ProcessorInterface, ResettableInterface
{
private $uid;

Expand All @@ -26,7 +28,8 @@ public function __construct($length = 7)
throw new \InvalidArgumentException('The uid length must be an integer between 1 and 32');
}

$this->uid = substr(hash('md5', uniqid('', true)), 0, $length);

$this->uid = $this->generateUid($length);
}

public function __invoke(array $record)
Expand All @@ -43,4 +46,14 @@ public function getUid()
{
return $this->uid;
}

public function reset()
{
$this->uid = $this->generateUid(strlen($this->uid));
}

private function generateUid($length)
{
return substr(hash('md5', uniqid('', true)), 0, $length);
}
}
25 changes: 25 additions & 0 deletions src/Monolog/ResettableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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.
*
* @author Grégoire Pineau <[email protected]>
*/
interface ResettableInterface
{
public function reset();
}
2 changes: 1 addition & 1 deletion tests/Monolog/Handler/BrowserConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BrowserConsoleHandlerTest extends TestCase
{
protected function setUp()
{
BrowserConsoleHandler::reset();
BrowserConsoleHandler::resetStatic();
}

protected function generateScript()
Expand Down
4 changes: 2 additions & 2 deletions tests/Monolog/Handler/ChromePHPHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ChromePHPHandlerTest extends TestCase
{
protected function setUp()
{
TestChromePHPHandler::reset();
TestChromePHPHandler::resetStatic();
$_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
}

Expand Down Expand Up @@ -136,7 +136,7 @@ class TestChromePHPHandler extends ChromePHPHandler
{
protected $headers = array();

public static function reset()
public static function resetStatic()
{
self::$initialized = false;
self::$overflowed = false;
Expand Down
4 changes: 2 additions & 2 deletions tests/Monolog/Handler/FingersCrossedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testHandleStopsBufferingAfterTrigger()
* @covers Monolog\Handler\FingersCrossedHandler::activate
* @covers Monolog\Handler\FingersCrossedHandler::reset
*/
public function testHandleRestartBufferingAfterReset()
public function testHandleResetBufferingAfterReset()
{
$test = new TestHandler();
$handler = new FingersCrossedHandler($test);
Expand All @@ -76,7 +76,7 @@ public function testHandleRestartBufferingAfterReset()
* @covers Monolog\Handler\FingersCrossedHandler::handle
* @covers Monolog\Handler\FingersCrossedHandler::activate
*/
public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
public function testHandleResetBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
{
$test = new TestHandler();
$handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false);
Expand Down
4 changes: 2 additions & 2 deletions tests/Monolog/Handler/FirePHPHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FirePHPHandlerTest extends TestCase
{
public function setUp()
{
TestFirePHPHandler::reset();
TestFirePHPHandler::resetStatic();
$_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; FirePHP/1.0';
}

Expand Down Expand Up @@ -77,7 +77,7 @@ class TestFirePHPHandler extends FirePHPHandler
{
protected $headers = array();

public static function reset()
public static function resetStatic()
{
self::$initialized = false;
self::$sendHeaders = true;
Expand Down
Loading

0 comments on commit a03084c

Please sign in to comment.