Skip to content

Commit a03084c

Browse files
authored
Merge pull request #997 from lyrixx/resetable
Added a new ResettableInterface and implemented it where possible.
2 parents d64fd10 + 531d05a commit a03084c

16 files changed

+200
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Fixed table row styling issues in HtmlFormatter
2020
* Fixed RavenHandler dropping the message when logging exception
2121
* Fixed WhatFailureGroupHandler skipping processors when using handleBatch
22+
* Added a `ResettableInterface` in order to reset/reset/clear/flush handlers and processors
23+
and implement it where possible
2224

2325
### 1.23.0 (2017-06-19)
2426

src/Monolog/Handler/AbstractHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace Monolog\Handler;
1313

14-
use Monolog\Logger;
1514
use Monolog\Formatter\FormatterInterface;
1615
use Monolog\Formatter\LineFormatter;
16+
use Monolog\Logger;
17+
use Monolog\ResettableInterface;
1718

1819
/**
1920
* Base Handler class providing the Handler structure
2021
*
2122
* @author Jordi Boggiano <j.boggiano@seld.be>
2223
*/
23-
abstract class AbstractHandler implements HandlerInterface
24+
abstract class AbstractHandler implements HandlerInterface, ResettableInterface
2425
{
2526
protected $level = Logger::DEBUG;
2627
protected $bubble = true;
@@ -174,6 +175,17 @@ public function __destruct()
174175
}
175176
}
176177

178+
public function reset()
179+
{
180+
$this->close();
181+
182+
foreach ($this->processors as $processor) {
183+
if ($processor instanceof ResettableInterface) {
184+
$processor->reset();
185+
}
186+
}
187+
}
188+
177189
/**
178190
* Gets the default formatter.
179191
*

src/Monolog/Handler/AbstractProcessingHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Monolog\Handler;
1313

14+
use Monolog\ResettableInterface;
15+
1416
/**
1517
* Base Handler class providing the Handler structure
1618
*

src/Monolog/Handler/BrowserConsoleHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ public static function send()
6969
} elseif ($format === 'js') {
7070
static::writeOutput(static::generateScript());
7171
}
72-
static::reset();
72+
static::resetStatic();
7373
}
7474
}
7575

76+
public function reset()
77+
{
78+
self::resetStatic();
79+
}
80+
7681
/**
7782
* Forget all logged records
7883
*/
79-
public static function reset()
84+
public static function resetStatic()
8085
{
8186
static::$records = array();
8287
}

src/Monolog/Handler/BufferHandler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Monolog\Handler;
1313

1414
use Monolog\Logger;
15+
use Monolog\ResettableInterface;
1516

1617
/**
1718
* Buffers all records until closing the handler and then pass them as batch.
@@ -114,4 +115,13 @@ public function clear()
114115
$this->bufferSize = 0;
115116
$this->buffer = array();
116117
}
118+
119+
public function reset()
120+
{
121+
parent::reset();
122+
123+
if ($this->handler instanceof ResettableInterface) {
124+
$this->handler->reset();
125+
}
126+
}
117127
}

src/Monolog/Handler/FingersCrossedHandler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1515
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
1616
use Monolog\Logger;
17+
use Monolog\ResettableInterface;
1718

1819
/**
1920
* Buffers all records until a certain level is reached
@@ -147,7 +148,14 @@ public function close()
147148
*/
148149
public function reset()
149150
{
151+
parent::reset();
152+
153+
$this->buffer = array();
150154
$this->buffering = true;
155+
156+
if ($this->handler instanceof ResettableInterface) {
157+
$this->handler->reset();
158+
}
151159
}
152160

153161
/**

src/Monolog/Handler/GroupHandler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Monolog\Handler;
1313

1414
use Monolog\Formatter\FormatterInterface;
15+
use Monolog\ResettableInterface;
1516

1617
/**
1718
* Forwards records to multiple handlers
@@ -90,6 +91,17 @@ public function handleBatch(array $records)
9091
}
9192
}
9293

94+
public function reset()
95+
{
96+
parent::reset();
97+
98+
foreach ($this->handlers as $handler) {
99+
if ($handler instanceof ResettableInterface) {
100+
$handler->reset();
101+
}
102+
}
103+
}
104+
93105
/**
94106
* {@inheritdoc}
95107
*/

src/Monolog/Handler/HandlerWrapper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Monolog\Handler;
1313

14+
use Monolog\ResettableInterface;
1415
use Monolog\Formatter\FormatterInterface;
1516

1617
/**
@@ -30,7 +31,7 @@
3031
*
3132
* @author Alexey Karapetov <alexey@karapetov.com>
3233
*/
33-
class HandlerWrapper implements HandlerInterface
34+
class HandlerWrapper implements HandlerInterface, ResettableInterface
3435
{
3536
/**
3637
* @var HandlerInterface
@@ -105,4 +106,11 @@ public function getFormatter()
105106
{
106107
return $this->handler->getFormatter();
107108
}
109+
110+
public function reset()
111+
{
112+
if ($this->handler instanceof ResettableInterface) {
113+
return $this->handler->reset();
114+
}
115+
}
108116
}

src/Monolog/Logger.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Jordi Boggiano <j.boggiano@seld.be>
2727
*/
28-
class Logger implements LoggerInterface
28+
class Logger implements LoggerInterface, ResettableInterface
2929
{
3030
/**
3131
* Detailed debug information
@@ -354,6 +354,21 @@ public function addRecord($level, $message, array $context = array())
354354
return true;
355355
}
356356

357+
public function reset()
358+
{
359+
foreach ($this->handlers as $handler) {
360+
if ($handler instanceof ResettableInterface) {
361+
$handler->reset();
362+
}
363+
}
364+
365+
foreach ($this->processors as $processor) {
366+
if ($processor instanceof ResettableInterface) {
367+
$processor->reset();
368+
}
369+
}
370+
}
371+
357372
/**
358373
* Adds a log record at the DEBUG level.
359374
*

src/Monolog/Processor/UidProcessor.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Monolog\Processor;
1313

14+
use Monolog\ResettableInterface;
15+
1416
/**
1517
* Adds a unique identifier into records
1618
*
1719
* @author Simon Mönch <sm@webfactory.de>
1820
*/
19-
class UidProcessor implements ProcessorInterface
21+
class UidProcessor implements ProcessorInterface, ResettableInterface
2022
{
2123
private $uid;
2224

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

29-
$this->uid = substr(hash('md5', uniqid('', true)), 0, $length);
31+
32+
$this->uid = $this->generateUid($length);
3033
}
3134

3235
public function __invoke(array $record)
@@ -43,4 +46,14 @@ public function getUid()
4346
{
4447
return $this->uid;
4548
}
49+
50+
public function reset()
51+
{
52+
$this->uid = $this->generateUid(strlen($this->uid));
53+
}
54+
55+
private function generateUid($length)
56+
{
57+
return substr(hash('md5', uniqid('', true)), 0, $length);
58+
}
4659
}

0 commit comments

Comments
 (0)