ConsoleFormatter example

if ($filter) {
            if (!class_exists(ExpressionLanguage::class)) {
                throw new LogicException('Package "symfony/expression-language" is required to use the "filter" option. Try running "composer require symfony/expression-language".');
            }
            $this->el = new ExpressionLanguage();
        }

        $this->handler = new ConsoleHandler($output, true, [
            OutputInterface::VERBOSITY_NORMAL => Logger::DEBUG,
        ]);

        $this->handler->setFormatter(new ConsoleFormatter([
            'format' => str_replace('\n', "\n", $input->getOption('format')),
            'date_format' => $input->getOption('date-format'),
            'colors' => $output->isDecorated(),
            'multiline' => OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity(),
        ]));

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

        if (!$socket = stream_socket_server($host$errno$errstr)) {
            
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Bridge\Monolog\Tests\RecordFactory;

class ConsoleFormatterTest extends TestCase
{
    /** * @dataProvider providerFormatTests */
    public function testFormat(array|LogRecord $record$expectedMessage)
    {
        $formatter = new ConsoleFormatter();
        self::assertSame($expectedMessage$formatter->format($record));
    }

    public static function providerFormatTests(): array
    {
        $currentDateTime = new \DateTimeImmutable();

        $tests = [
            'record with DateTime object in datetime field' => [
                'record' => RecordFactory::create(datetime: $currentDateTime),
                'expectedMessage' => sprintf(
                    

        // at this point we've determined for sure that we want to output the record, so use the output's own verbosity         $this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
    }

    protected function getDefaultFormatter(): FormatterInterface
    {
        if (!class_exists(CliDumper::class)) {
            return new LineFormatter();
        }
        if (!$this->output) {
            return new ConsoleFormatter($this->consoleFormatterOptions);
        }

        return new ConsoleFormatter(array_replace([
            'colors' => $this->output->isDecorated(),
            'multiline' => OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity(),
        ]$this->consoleFormatterOptions));
    }

    /** * Updates the logging level based on the verbosity setting of the console output. * * @return bool Whether the handler is enabled and verbosity is not set to quiet */
Home | Imprint | This part of the site doesn't use cookies.