diff --git a/src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php b/src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php index efce0d7e56ee..2f4d9286c5c0 100644 --- a/src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php +++ b/src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php @@ -39,6 +39,9 @@ */ class ElasticsearchLogstashHandler extends AbstractHandler { + use ProcessableHandlerTrait; + use FormattableHandlerTrait; + private $endpoint; private $index; private $client; @@ -79,7 +82,13 @@ public function handleBatch(array $records): void protected function getDefaultFormatter(): FormatterInterface { - return new LogstashFormatter('application', null, null, 'ctxt_', LogstashFormatter::V1); + // Monolog 1.X + if (\defined(LogstashFormatter::class.'::V1')) { + return new LogstashFormatter('application', null, null, 'ctxt_', LogstashFormatter::V1); + } + + // Monolog 2.X + return new LogstashFormatter('application'); } private function sendToElasticsearch(array $records) diff --git a/src/Symfony/Bridge/Monolog/Handler/FormattableHandlerTrait.php b/src/Symfony/Bridge/Monolog/Handler/FormattableHandlerTrait.php new file mode 100644 index 000000000000..c03bc7556a68 --- /dev/null +++ b/src/Symfony/Bridge/Monolog/Handler/FormattableHandlerTrait.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/* + * This file is part of the Monolog package. + * + * (c) Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Monolog\Handler; + +use Monolog\Formatter\FormatterInterface; +use Monolog\Formatter\LineFormatter; + +/** + * Helper trait for implementing FormattableInterface. + * + * @author Jordi Boggiano + * + * @internal + */ +trait FormattableHandlerTrait +{ + /** + * @var FormatterInterface + */ + protected $formatter; + + /** + * {@inheritdoc} + * + * @suppress PhanTypeMismatchReturn + */ + public function setFormatter(FormatterInterface $formatter): HandlerInterface + { + $this->formatter = $formatter; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getFormatter(): FormatterInterface + { + if (!$this->formatter) { + $this->formatter = $this->getDefaultFormatter(); + } + + return $this->formatter; + } + + /** + * Gets the default formatter. + * + * Overwrite this if the LineFormatter is not a good default for your handler. + */ + protected function getDefaultFormatter(): FormatterInterface + { + return new LineFormatter(); + } +} diff --git a/src/Symfony/Bridge/Monolog/Handler/ProcessableHandlerTrait.php b/src/Symfony/Bridge/Monolog/Handler/ProcessableHandlerTrait.php new file mode 100644 index 000000000000..9c80d3318c32 --- /dev/null +++ b/src/Symfony/Bridge/Monolog/Handler/ProcessableHandlerTrait.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Monolog\Handler; + +use Monolog\ResettableInterface; + +/** + * Helper trait for implementing ProcessableInterface. + * + * @author Jordi Boggiano + * + * @internal + */ +trait ProcessableHandlerTrait +{ + /** + * @var callable[] + */ + protected $processors = []; + + /** + * {@inheritdoc} + * + * @suppress PhanTypeMismatchReturn + */ + public function pushProcessor($callback): HandlerInterface + { + array_unshift($this->processors, $callback); + + return $this; + } + + /** + * {@inheritdoc} + */ + public function popProcessor(): callable + { + if (!$this->processors) { + throw new \LogicException('You tried to pop from an empty processor stack.'); + } + + return array_shift($this->processors); + } + + /** + * Processes a record. + */ + protected function processRecord(array $record): array + { + foreach ($this->processors as $processor) { + $record = $processor($record); + } + + return $record; + } + + protected function resetProcessors() + { + foreach ($this->processors as $processor) { + if ($processor instanceof ResettableInterface) { + $processor->reset(); + } + } + } +} diff --git a/src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php b/src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php index a8875f27f21e..2940f0440ff8 100644 --- a/src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php @@ -27,11 +27,17 @@ public function testHandle() $responseFactory = function ($method, $url, $options) use (&$callCount) { $body = <<assertSame('POST', $method); $this->assertSame('http://es:9200/_bulk', $url); $this->assertSame($body, $options['body']); @@ -64,14 +70,20 @@ public function testBandleBatch() $responseFactory = function ($method, $url, $options) use (&$callCount) { $body = <<assertSame('POST', $method); $this->assertSame('http://es:9200/_bulk', $url); $this->assertSame($body, $options['body']); @@ -114,6 +126,12 @@ class ElasticsearchLogstashHandlerWithHardCodedHostname extends ElasticsearchLog { protected function getDefaultFormatter(): FormatterInterface { - return new LogstashFormatter('application', 'my hostname', null, 'ctxt_', LogstashFormatter::V1); + // Monolog 1.X + if (\defined(LogstashFormatter::class.'::V1')) { + return new LogstashFormatter('application', 'my hostname', null, 'ctxt_', LogstashFormatter::V1); + } + + // Monolog 2.X + return new LogstashFormatter('application', 'my hostname'); } }