1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-03 11:47:38 +02:00
This commit is contained in:
Jordi Boggiano
2024-06-28 10:48:40 +02:00
parent 3ba77d1d39
commit 07cd08e0e4
34 changed files with 106 additions and 91 deletions

View File

@@ -244,6 +244,7 @@ class LineFormatter extends NormalizerFormatter
$str = preg_replace('/(?<!\\\\)\\\\[rn]/', "\n", $str); $str = preg_replace('/(?<!\\\\)\\\\[rn]/', "\n", $str);
if (null === $str) { if (null === $str) {
$pcreErrorCode = preg_last_error(); $pcreErrorCode = preg_last_error();
throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode)); throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode));
} }
} }

View File

@@ -44,7 +44,6 @@ class SyslogFormatter extends LineFormatter
} }
/** /**
* @param LogRecord $record
* @return array<string, mixed> * @return array<string, mixed>
*/ */
private function formatExtra(LogRecord $record): array private function formatExtra(LogRecord $record): array

View File

@@ -65,6 +65,7 @@ class AmqpHandler extends AbstractProcessingHandler
public function setExtraAttributes(array $extraAttributes): self public function setExtraAttributes(array $extraAttributes): self
{ {
$this->extraAttributes = $extraAttributes; $this->extraAttributes = $extraAttributes;
return $this; return $this;
} }
@@ -155,6 +156,7 @@ class AmqpHandler extends AbstractProcessingHandler
if (\count($this->extraAttributes) > 0) { if (\count($this->extraAttributes) > 0) {
$attributes = array_merge($attributes, $this->extraAttributes); $attributes = array_merge($attributes, $this->extraAttributes);
} }
return new AMQPMessage($data, $attributes); return new AMQPMessage($data, $attributes);
} }

View File

@@ -17,7 +17,6 @@ use Monolog\Utils;
use Monolog\LogRecord; use Monolog\LogRecord;
use Monolog\Level; use Monolog\Level;
use function count;
use function headers_list; use function headers_list;
use function stripos; use function stripos;

View File

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
use Monolog\Level; use Monolog\Level;
use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LogglyFormatter; use Monolog\Formatter\LogglyFormatter;
use function array_key_exists;
use CurlHandle; use CurlHandle;
use Monolog\LogRecord; use Monolog\LogRecord;

View File

@@ -182,8 +182,8 @@ class RotatingFileHandler extends StreamHandler
[$fileInfo['filename'], str_replace( [$fileInfo['filename'], str_replace(
['Y', 'y', 'm', 'd'], ['Y', 'y', 'm', 'd'],
['[0-9][0-9][0-9][0-9]', '[0-9][0-9]', '[0-9][0-9]', '[0-9][0-9]'], ['[0-9][0-9][0-9][0-9]', '[0-9][0-9]', '[0-9][0-9]', '[0-9][0-9]'],
$this->dateFormat) $this->dateFormat
], )],
($fileInfo['dirname'] ?? '') . '/' . $this->filenameFormat ($fileInfo['dirname'] ?? '') . '/' . $this->filenameFormat
); );
if (isset($fileInfo['extension'])) { if (isset($fileInfo['extension'])) {

View File

@@ -125,6 +125,7 @@ class StreamHandler extends AbstractProcessingHandler
set_error_handler(function (...$args) { set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args); return $this->customErrorHandler(...$args);
}); });
try { try {
$stream = fopen($url, 'a'); $stream = fopen($url, 'a');
if ($this->filePermission !== null) { if ($this->filePermission !== null) {

View File

@@ -13,7 +13,6 @@ namespace Monolog\Handler;
use Closure; use Closure;
use Monolog\Level; use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord; use Monolog\LogRecord;
use Monolog\Utils; use Monolog\Utils;
use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\FormatterInterface;
@@ -82,6 +81,7 @@ class SymfonyMailerHandler extends MailHandler
if (!$message instanceof Email) { if (!$message instanceof Email) {
$record = reset($records); $record = reset($records);
throw new \InvalidArgumentException('Could not resolve message as instance of Email or a callable returning it' . ($record instanceof LogRecord ? Utils::getRecordMessageForException($record) : '')); throw new \InvalidArgumentException('Could not resolve message as instance of Email or a callable returning it' . ($record instanceof LogRecord ? Utils::getRecordMessageForException($record) : ''));
} }

View File

@@ -12,7 +12,6 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Level; use Monolog\Level;
use Monolog\Utils;
use Monolog\LogRecord; use Monolog\LogRecord;
/** /**

View File

@@ -346,6 +346,7 @@ class Logger implements LoggerInterface, ResettableInterface
if ($logDepth === 3) { if ($logDepth === 3) {
$this->warning('A possible infinite logging loop was detected and aborted. It appears some of your handler code is triggering logging, see the previous log record for a hint as to what may be the cause.'); $this->warning('A possible infinite logging loop was detected and aborted. It appears some of your handler code is triggering logging, see the previous log record for a hint as to what may be the cause.');
return false; return false;
} elseif ($logDepth >= 5) { // log depth 4 is let through, so we can log the warning above } elseif ($logDepth >= 5) { // log depth 4 is let through, so we can log the warning above
return false; return false;

View File

@@ -1,4 +1,13 @@
<?php <?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Attribute; namespace Monolog\Attribute;
@@ -11,5 +20,4 @@ class WithMonologChannelTest extends TestCase
$attribute = new WithMonologChannel('fixture'); $attribute = new WithMonologChannel('fixture');
$this->assertSame('fixture', $attribute->channel); $this->assertSame('fixture', $attribute->channel);
} }
} }

View File

@@ -121,7 +121,7 @@ class JsonFormatterTest extends TestCase
public function testBasePathWithException(): void public function testBasePathWithException(): void
{ {
$formatter = new JsonFormatter(); $formatter = new JsonFormatter();
$formatter->setBasePath(dirname(dirname(dirname(__DIR__)))); $formatter->setBasePath(\dirname(\dirname(\dirname(__DIR__))));
$exception = new \RuntimeException('Foo'); $exception = new \RuntimeException('Foo');
$message = $this->formatRecordWithExceptionInContext($formatter, $exception); $message = $this->formatRecordWithExceptionInContext($formatter, $exception);

View File

@@ -84,7 +84,7 @@ class NormalizerFormatterTest extends TestCase
public function testFormatExceptionWithBasePath(): void public function testFormatExceptionWithBasePath(): void
{ {
$formatter = new NormalizerFormatter('Y-m-d'); $formatter = new NormalizerFormatter('Y-m-d');
$formatter->setBasePath(dirname(dirname(dirname(__DIR__)))); $formatter->setBasePath(\dirname(\dirname(\dirname(__DIR__))));
$e = new \LogicException('bar'); $e = new \LogicException('bar');
$formatted = $formatter->normalizeValue([ $formatted = $formatter->normalizeValue([
'exception' => $e, 'exception' => $e,

View File

@@ -103,16 +103,25 @@ class RedisHandlerTest extends TestCase
{ {
$redis = new class extends \Predis\Client { $redis = new class extends \Predis\Client {
public array $testResults = []; public array $testResults = [];
public function rpush(...$args) {
public function rpush(...$args)
{
$this->testResults[] = ['rpush', ...$args]; $this->testResults[] = ['rpush', ...$args];
return $this; return $this;
} }
public function ltrim(...$args) {
public function ltrim(...$args)
{
$this->testResults[] = ['ltrim', ...$args]; $this->testResults[] = ['ltrim', ...$args];
return $this; return $this;
} }
public function transaction(...$args) {
public function transaction(...$args)
{
$this->testResults[] = ['transaction start']; $this->testResults[] = ['transaction start'];
return ($args[0])($this); return ($args[0])($this);
} }
}; };

View File

@@ -59,7 +59,8 @@ class RotatingFileHandlerTest extends TestCase
unset($this->lastError); unset($this->lastError);
} }
private function rrmdir($directory) { private function rrmdir($directory)
{
if (! is_dir($directory)) { if (! is_dir($directory)) {
throw new InvalidArgumentException("$directory must be a directory"); throw new InvalidArgumentException("$directory must be a directory");
} }

View File

@@ -69,6 +69,7 @@ class SymfonyMailerHandlerTest extends TestCase
// Callback dynamically changes subject based on number of logged records // Callback dynamically changes subject based on number of logged records
$callback = function ($content, array $records) use ($expectedMessage) { $callback = function ($content, array $records) use ($expectedMessage) {
$subject = \count($records) > 0 ? 'Emergency' : 'Normal'; $subject = \count($records) > 0 ? 'Emergency' : 'Normal';
return $expectedMessage->subject($subject); return $expectedMessage->subject($subject);
}; };
$handler = new SymfonyMailerHandler($this->mailer, $callback); $handler = new SymfonyMailerHandler($this->mailer, $callback);

View File

@@ -285,11 +285,11 @@ class LoggerTest extends TestCase
$handler->expects($this->any()) $handler->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler->expects($this->any()) $handler->expects($this->any())
->method('handle') ->method('handle')
->willReturn(true); ->willReturn(true);
;
$logger->pushHandler($handler); $logger->pushHandler($handler);
$processor = $this->getMockBuilder('Monolog\Processor\WebProcessor') $processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
@@ -316,7 +316,7 @@ class LoggerTest extends TestCase
$handler->expects($this->once()) $handler->expects($this->once())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler); $logger->pushHandler($handler);
$that = $this; $that = $this;
$logger->pushProcessor(function ($record) use ($that) { $logger->pushProcessor(function ($record) use ($that) {
@@ -337,29 +337,29 @@ class LoggerTest extends TestCase
$handler1->expects($this->never()) $handler1->expects($this->never())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$handler1->expects($this->once()) $handler1->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler1); $logger->pushHandler($handler1);
$handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler2); $logger->pushHandler($handler2);
$handler3 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler3 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler3->expects($this->once()) $handler3->expects($this->once())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$handler3->expects($this->never()) $handler3->expects($this->never())
->method('handle') ->method('handle')
; ;
@@ -377,27 +377,25 @@ class LoggerTest extends TestCase
$handler1->expects($this->never()) $handler1->expects($this->never())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$handler1->expects($this->once()) $handler1->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$handler3 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler3 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler3->expects($this->once()) $handler3->expects($this->once())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$handler3->expects($this->never()) $handler3->expects($this->never())
->method('handle') ->method('handle')
; ;
@@ -419,22 +417,22 @@ class LoggerTest extends TestCase
$handler1->expects($this->any()) $handler1->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler1->expects($this->once()) $handler1->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler1); $logger->pushHandler($handler1);
$handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2 = $this->createMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->any()) $handler2->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('handle') ->method('handle')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler2); $logger->pushHandler($handler2);
$logger->debug('test'); $logger->debug('test');
@@ -451,7 +449,7 @@ class LoggerTest extends TestCase
$handler1->expects($this->any()) $handler1->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler1->expects($this->never()) $handler1->expects($this->never())
->method('handle') ->method('handle')
; ;
@@ -461,11 +459,11 @@ class LoggerTest extends TestCase
$handler2->expects($this->any()) $handler2->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler2->expects($this->once()) $handler2->expects($this->once())
->method('handle') ->method('handle')
->willReturn(true); ->willReturn(true);
;
$logger->pushHandler($handler2); $logger->pushHandler($handler2);
$logger->debug('test'); $logger->debug('test');
@@ -482,7 +480,6 @@ class LoggerTest extends TestCase
$handler1->expects($this->any()) $handler1->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(false); ->willReturn(false);
;
$logger->pushHandler($handler1); $logger->pushHandler($handler1);
$this->assertFalse($logger->isHandling(Level::Debug)); $this->assertFalse($logger->isHandling(Level::Debug));
@@ -491,7 +488,6 @@ class LoggerTest extends TestCase
$handler2->expects($this->any()) $handler2->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$logger->pushHandler($handler2); $logger->pushHandler($handler2);
$this->assertTrue($logger->isHandling(Level::Debug)); $this->assertTrue($logger->isHandling(Level::Debug));
@@ -677,7 +673,7 @@ class LoggerTest extends TestCase
$handler->expects($this->any()) $handler->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler->expects($this->any()) $handler->expects($this->any())
->method('handle') ->method('handle')
->will($this->throwException(new \Exception('Some handler exception'))) ->will($this->throwException(new \Exception('Some handler exception')))
@@ -706,7 +702,7 @@ class LoggerTest extends TestCase
$handler->expects($this->any()) $handler->expects($this->any())
->method('isHandling') ->method('isHandling')
->willReturn(true); ->willReturn(true);
;
$handler->expects($this->any()) $handler->expects($this->any())
->method('handle') ->method('handle')
->will($this->throwException(new \Exception('Some handler exception'))) ->will($this->throwException(new \Exception('Some handler exception')))
@@ -900,7 +896,6 @@ class LoggingHandler implements HandlerInterface
} }
} }
class FiberSuspendHandler implements HandlerInterface class FiberSuspendHandler implements HandlerInterface
{ {
public function isHandling(LogRecord $record): bool public function isHandling(LogRecord $record): bool