1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 14:16:42 +02:00

Merge branch '2.x' into main

This commit is contained in:
Jordi Boggiano
2022-05-10 11:45:36 +02:00
8 changed files with 205 additions and 53 deletions

View File

@@ -141,6 +141,52 @@ class LineFormatterTest extends TestCase
$this->assertMatchesRegularExpression('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 5).'\)\n\[stacktrace]\n#0}', $message);
}
public function testDefFormatWithExceptionAndStacktraceParserFull()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->includeStacktraces(true, function ($line) {
return $line;
});
$message = $formatter->format($this->getRecord(Level::Critical, context: ['exception' => new \RuntimeException('Foo')]));
$trace = explode('[stacktrace]', $message, 2)[1];
$this->assertStringContainsString('TestCase.php', $trace);
$this->assertStringContainsString('TestResult.php', $trace);
}
public function testDefFormatWithExceptionAndStacktraceParserCustom()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->includeStacktraces(true, function ($line) {
if (strpos($line, 'TestCase.php') === false) {
return $line;
}
});
$message = $formatter->format($this->getRecord(Level::Critical, context: ['exception' => new \RuntimeException('Foo')]));
$trace = explode('[stacktrace]', $message, 2)[1];
$this->assertStringNotContainsString('TestCase.php', $trace);
$this->assertStringContainsString('TestResult.php', $trace);
}
public function testDefFormatWithExceptionAndStacktraceParserEmpty()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->includeStacktraces(true, function ($line) {
return null;
});
$message = $formatter->format($this->getRecord(Level::Critical, context: ['exception' => new \RuntimeException('Foo')]));
$trace = explode('[stacktrace]', $message, 2)[1];
$this->assertStringNotContainsString('#', $trace);
}
public function testDefFormatWithPreviousException()
{
$formatter = new LineFormatter(null, 'Y-m-d');

View File

@@ -58,10 +58,8 @@ class UdpSocketTest extends TestCase
$socket->close();
}
public function testWriteAfterCloseErrors()
public function testWriteAfterCloseReopened()
{
$this->expectException(\RuntimeException::class);
$socket = new UdpSocket('127.0.0.1', 514);
$socket->close();
$socket->write('foo', "HEADER");

View File

@@ -11,6 +11,7 @@
namespace Monolog;
use Monolog\Handler\HandlerInterface;
use Monolog\Processor\WebProcessor;
use Monolog\Handler\TestHandler;
use Monolog\Test\TestCase;
@@ -70,6 +71,28 @@ class LoggerTest extends TestCase
/**
* @covers Logger::addRecord
*/
public function testLogPreventsCircularLogging()
{
$logger = new Logger(__METHOD__);
$loggingHandler = new LoggingHandler($logger);
$testHandler = new TestHandler();
$logger->pushHandler($loggingHandler);
$logger->pushHandler($testHandler);
$logger->addRecord(Level::Alert, 'test');
$records = $testHandler->getRecords();
$this->assertCount(3, $records);
$this->assertSame('ALERT', $records[0]->level->getName());
$this->assertSame('DEBUG', $records[1]->level->getName());
$this->assertSame('WARNING', $records[2]->level->getName());
}
/**
* @covers Monolog\Logger::addRecord
*/
public function testLog()
{
$logger = new Logger(__METHOD__);
@@ -718,3 +741,36 @@ class LoggerTest extends TestCase
$this->assertNotSame($uid2, $processorUid2->getUid());
}
}
class LoggingHandler implements HandlerInterface
{
/**
* @var Logger
*/
private $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function isHandling(LogRecord $record): bool
{
return true;
}
public function handle(LogRecord $record): bool
{
$this->logger->debug('Log triggered while logging');
return false;
}
public function handleBatch(array $records): void
{
}
public function close(): void
{
}
}