LogRecord example


    public function handle(LogRecord $record): bool
    {
        if (
            isset($record->context['exception'])
            && \is_object($record->context['exception'])
            && $record->context['exception'] instanceof ShopwareHttpException
            && \array_key_exists($record->context['exception']->getErrorCode()$this->errorCodesToLogLevel)
        ) {
            $level = Level::fromName($this->errorCodesToLogLevel[$record->context['exception']->getErrorCode()]);

            $record = new LogRecord(
                $record->datetime,
                $record->channel,
                $level,
                $record->message,
                $record->context,
                $record->extra,
                $record->formatted
            );
        }

        return $this->handler->handle($record);
    }
use Monolog\Logger;
use Monolog\LogRecord;

class RecordFactory
{
    public static function create(int|string $level = 'warning', string|\Stringable $message = 'test', string $channel = 'test', array $context = [], \DateTimeImmutable $datetime = new \DateTimeImmutable(), array $extra = []): LogRecord|array
    {
        $level = Logger::toMonologLevel($level);

        if (Logger::API >= 3) {
            return new LogRecord(
                message: (string) $message,
                context: $context,
                level: $level,
                channel: $channel,
                datetime: $datetime,
                extra: $extra,
            );
        }

        return [
            'message' => $message,
            
$handler->handle($record);
    }

    /** * @return iterable<string, array{0: LogRecord, 1: list<string>, 2: bool}> */
    public static function cases(): iterable
    {
        // record, exclude list, should be passed         yield 'empty record' => [
            new LogRecord(new \DateTimeImmutable(), 'foo', Level::Alert, 'some message'),
            [],
            true,
        ];

        yield 'exception without exclude list' => [
            new LogRecord(
                new \DateTimeImmutable(),
                'foo',
                Level::Alert,
                'some message',
                [
                    
$errorCodeLogLevelMapping
        );

        $handler->handle($record);
    }

    /** * @return iterable<string, array{0: LogRecord, 1: array<string, value-of<Level::NAMES>|LogLevel::*|'Debug'|'Info'|'Notice'|'Warning'|'Error'|'Critical'|'Alert'|'Emergency'>, 2: Level}> */
    public static function cases(): iterable
    {
        $logRecord = new LogRecord(new \DateTimeImmutable(), 'foo', Level::Alert, 'some message');
        yield 'log level stays same without exception' => [
            $logRecord,
            [],
            Level::Alert,
        ];

        $logRecord = new LogRecord(new \DateTimeImmutable(), 'foo', Level::Alert, 'some message', ['exception' => new \RuntimeException('')]);
        yield 'log level stays same without shopware exception' => [
            $logRecord,
            [],
            Level::Alert,
        ];
$handler->handle($record);
    }

    /** * @return iterable<string, array{0: LogRecord, 1: list<string>, 2: bool}> */
    public static function cases(): iterable
    {
        // record, exclude list, should be passed         yield 'event without exclude list' => [
            new LogRecord(new \DateTimeImmutable(), 'foo', Level::Alert, 'some message'),
            [],
            true,
        ];

        yield 'event with exclude list that matches but different channel' => [
            new LogRecord(new \DateTimeImmutable(), 'app', Level::Alert, UserRecoveryRequestEvent::EVENT_NAME),
            [
                UserRecoveryRequestEvent::EVENT_NAME,
            ],
            true,
        ];

        
public function testOnlyController(): void
    {
        $requestStack = new RequestStack();
        $inner = $this->createMock(AbstractHandler::class);
        $container = $this->createMock(ContainerInterface::class);
        $handler = new AnnotatePackageProcessor($requestStack$container);

        $request = new Request();
        $request->attributes->set('_controller', TestController::class D '::load');
        $requestStack->push($request);

        $record = new LogRecord(
            new \DateTimeImmutable(),
            'business events',
            Level::Error,
            'Some message'
        );

        $expected = new LogRecord(
            $record->datetime,
            $record->channel,
            $record->level,
            $record->message,
            []
protected function setUp(): void
    {
        $this->connection = $this->createMock(Connection::class);
    }

    public function testWrite(): void
    {
        $this->connection->expects(static::once())->method('insert');

        $handler = new DoctrineSQLHandler($this->connection);

        $record = new LogRecord(
            new \DateTimeImmutable(),
            'business events',
            Level::Error,
            'Some message'
        );

        $handler->handle($record);
    }

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