getLogs example

use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Contracts\Service\ResetInterface;

/** * @deprecated since Symfony 6.4, use HttpKernel's DebugLoggerConfigurator instead */
class Logger extends BaseLogger implements DebugLoggerInterface, ResetInterface
{
    public function getLogs(Request $request = null): array
    {
        if ($logger = $this->getDebugLogger()) {
            return $logger->getLogs($request);
        }

        return [];
    }

    public function countErrors(Request $request = null): int
    {
        if ($logger = $this->getDebugLogger()) {
            return $logger->countErrors($request);
        }

        


namespace Symfony\Bridge\Monolog\Tests;

use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\HttpFoundation\Request;

class ClassThatInheritLogger extends Logger
{
    public function getLogs(Request $request = null): array
    {
        return parent::getLogs($request);
    }

    public function countErrors(Request $request = null): int
    {
        return parent::countErrors($request);
    }
}
echo $this->include('views/traces.html.php', [
                        'exception' => $e,
                        'index' => $i + 1,
                        'expand' => in_array($i$exceptionWithUserCode, true) || ([] === $exceptionWithUserCode && 0 === $i),
                    ]);
                }
                ?> </div> </div> <?php if ($logger) { ?> <div class="tab <?= !$logger->getLogs() ? 'disabled' : ''; ?>"> <h3 class="tab-title"> Logs <?php if ($logger->countErrors()) { ?><span class="badge status-error"><?= $logger->countErrors(); ?></span><?php } ?> </h3> <div class="tab-content"> <?php if ($logger->getLogs()) { ?> <?= $this->include('views/logs.html.php', ['logs' => $logger->getLogs()]); ?> <?php } else { ?> <div class="empty"> <p>No log messages</p> </div>

    public function testLogsAtAllLevels($level$message)
    {
        $this->logger->{$level}($message['user' => 'Bob']);
        $this->logger->log($level$message['user' => 'Bob']);

        $expected = [
            "[$level] message of level $level with context: Bob",
            "[$level] message of level $level with context: Bob",
        ];
        $this->assertLogsMatch($expected$this->getLogs());
    }

    public static function provideLevelsAndMessages()
    {
        return [
            LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'message of level emergency with context: {user}'],
            LogLevel::ALERT => [LogLevel::ALERT, 'message of level alert with context: {user}'],
            LogLevel::CRITICAL => [LogLevel::CRITICAL, 'message of level critical with context: {user}'],
            LogLevel::ERROR => [LogLevel::ERROR, 'message of level error with context: {user}'],
            LogLevel::WARNING => [LogLevel::WARNING, 'message of level warning with context: {user}'],
            LogLevel::NOTICE => [LogLevel::NOTICE, 'message of level notice with context: {user}'],
            
echo $this->include('views/traces.html.php', [
                        'exception' => $e,
                        'index' => $i + 1,
                        'expand' => in_array($i$exceptionWithUserCode, true) || ([] === $exceptionWithUserCode && 0 === $i),
                    ]);
                }
                ?> </div> </div> <?php if ($logger) { ?> <div class="tab <?= !$logger->getLogs() ? 'disabled' : ''; ?>"> <h3 class="tab-title"> Logs <?php if ($logger->countErrors()) { ?><span class="badge status-error"><?= $logger->countErrors(); ?></span><?php } ?> </h3> <div class="tab-content"> <?php if ($logger->getLogs()) { ?> <?= $this->include('views/logs.html.php', ['logs' => $logger->getLogs()]); ?> <?php } else { ?> <div class="empty"> <p>No log messages</p> </div>
$this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
    }

    public function lateCollect(): void
    {
        if ($this->logger) {
            $containerDeprecationLogs = $this->getContainerDeprecationLogs();
            $this->data = $this->computeErrorsCount($containerDeprecationLogs);
            // get compiler logs later (only when they are needed) to improve performance             $this->data['compiler_logs'] = [];
            $this->data['compiler_logs_filepath'] = $this->containerPathPrefix.'Compiler.log';
            $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest)$containerDeprecationLogs));
            $this->data = $this->cloneVar($this->data);
        }
        $this->currentRequest = null;
    }

    public function getLogs(): Data|array
    {
        return $this->data['logs'] ?? [];
    }

    public function getProcessedLogs(): array
    {
$count = $this->countRevisions($entityTypeId);
    $this->submitForm([], 'Revert');

    // A new revision was created.     $this->assertEquals($count + 1, $this->countRevisions($entityTypeId));

    // Destination.     $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->addressEquals($expectedDestination);

    // Logger log.     $logs = $this->getLogs($entity->getEntityType()->getProvider());
    $this->assertEquals([0 => $expectedLog]$logs);
    // Messenger message.     $this->assertSession()->pageTextContains($expectedMessage);
  }

  /** * Data provider for testSubmitForm. */
  public function providerSubmitForm(): array {
    $data = [];

    
// Destination.     if ($expectedDestination === 404) {
      $this->assertSession()->statusCodeEquals(404);
    }
    else {
      $this->assertSession()->statusCodeEquals(200);
      $this->assertSession()->addressEquals($expectedDestination);
    }

    // Logger log.     $logs = $this->getLogs($entity->getEntityType()->getProvider());
    $this->assertEquals([0 => $expectedLog]$logs);
    // Messenger message.     $this->assertSession()->pageTextContains($expectedMessage);
  }

  /** * Data provider for testSubmitForm. */
  public function providerSubmitForm(): array {
    $data = [];

    
'multiline' => OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity(),
        ]));

        if (!str_contains($host = $input->getOption('host'), '://')) {
            $host = 'tcp://'.$host;
        }

        if (!$socket = stream_socket_server($host$errno$errstr)) {
            throw new RuntimeException(sprintf('Server start failed on "%s": ', $host).$errstr.' '.$errno);
        }

        foreach ($this->getLogs($socket) as $clientId => $message) {
            $record = unserialize(base64_decode($message));

            // Impossible to decode the message, give up.             if (false === $record) {
                continue;
            }

            if ($filter && !$this->el->evaluate($filter$record)) {
                continue;
            }

            
LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL,
        ]);
    }

    /** * Return the log messages in order. * * @return string[] */
    public function getLogs(): array
    {
        return $this->output->getLogs();
    }

    /** * @dataProvider provideOutputMappingParams */
    public function testOutputMapping($logLevel$outputVerbosity$isOutput$addVerbosityLevelMap = [])
    {
        $out = new BufferedOutput($outputVerbosity);
        $logger = new ConsoleLogger($out$addVerbosityLevelMap);
        $logger->log($logLevel, 'foo bar');
        $logs = $out->fetch();
        


namespace Symfony\Bridge\Monolog\Tests\Processor;

use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Component\HttpFoundation\Request;

class ClassThatInheritDebugProcessor extends DebugProcessor
{
    public function getLogs(Request $request = null): array
    {
        return parent::getLogs($request);
    }

    public function countErrors(Request $request = null): int
    {
        return parent::countErrors($request);
    }
}
$this->assertEquals('logger', $c->getName());
        $this->assertEquals($nb$c->countErrors());

        $logs = array_map(function D$v) {
            if (isset($v['context']['exception'])) {
                $e = &$v['context']['exception'];
                $e = isset($e["\0*\0message"]) ? [$e["\0*\0message"]$e["\0*\0severity"]] : [$e["\0Symfony\Component\ErrorHandler\Exception\SilencedErrorContext\0severity"]];
            }

            return $v;
        }$c->getLogs()->getValue(true));
        $this->assertEquals($expectedLogs$logs);
        $this->assertEquals($expectedDeprecationCount$c->countDeprecations());
        $this->assertEquals($expectedScreamCount$c->countScreams());

        if (isset($expectedPriorities)) {
            $this->assertSame($expectedPriorities$c->getPriorities()->getValue(true));
        }
    }

    public static function getCollectTestData()
    {
        
try {
            $l->logKernelException($event2);
            $l->onKernelException($event2);
            $this->fail('RuntimeException expected');
        } catch (\RuntimeException $e) {
            $this->assertSame('bar', $e->getMessage());
            $this->assertSame('foo', $e->getPrevious()->getMessage());
        }

        $this->assertEquals(3, $logger->countErrors());
        $logs = $logger->getLogs('critical');
        $this->assertCount(3, $logs);
        $this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[0]);
        $this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[1]);
        $this->assertStringStartsWith('Exception thrown when handling an exception (RuntimeException: bar at ErrorListenerTest.php line', $logs[2]);
    }

    public function testHandleWithLoggerAndCustomConfiguration()
    {
        $request = new Request();
        $event = new ExceptionEvent(new TestKernel()$request, HttpKernelInterface::MAIN_REQUEST, new \RuntimeException('bar'));
        $logger = new TestLogger();
        
/** * @group legacy */
class LoggerTest extends TestCase
{
    public function testGetLogsWithoutDebugProcessor()
    {
        $handler = new TestHandler();
        $logger = new Logger(__METHOD__, [$handler]);

        $logger->error('error message');
        $this->assertSame([]$logger->getLogs());
    }

    public function testCountErrorsWithoutDebugProcessor()
    {
        $handler = new TestHandler();
        $logger = new Logger(__METHOD__, [$handler]);

        $logger->error('error message');
        $this->assertSame(0, $logger->countErrors());
    }

    
$this->data = [];
    }

    public function lateCollect(): void
    {
        if (isset($this->logger)) {
            $containerDeprecationLogs = $this->getContainerDeprecationLogs();
            $this->data = $this->computeErrorsCount($containerDeprecationLogs);
            // get compiler logs later (only when they are needed) to improve performance             $this->data['compiler_logs'] = [];
            $this->data['compiler_logs_filepath'] = $this->containerPathPrefix.'Compiler.log';
            $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest)$containerDeprecationLogs));
            $this->data = $this->cloneVar($this->data);
        }
        $this->currentRequest = null;
    }

    public function getLogs()
    {
        return $this->data['logs'] ?? [];
    }

    public function getProcessedLogs()
    {
Home | Imprint | This part of the site doesn't use cookies.