|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Monolog\Tests\Handler; |
| 13 | + |
| 14 | +use Monolog\Formatter\FormatterInterface; |
| 15 | +use Monolog\Formatter\LogstashFormatter; |
| 16 | +use Monolog\Logger; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler; |
| 19 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 20 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 21 | + |
| 22 | +class ElasticsearchLogstashHandlerTest extends TestCase |
| 23 | +{ |
| 24 | + public function testHandle() |
| 25 | + { |
| 26 | + $callCount = 0; |
| 27 | + $responseFactory = function ($method, $url, $options) use (&$callCount) { |
| 28 | + $body = <<<EOBODY |
| 29 | +{"index":{"_index":"log","_type":"_doc"}} |
| 30 | +{"@timestamp":"2020-01-01T00:00:00.000000+01:00","@version":1,"host":"my hostname","message":"My info message","type":"application","channel":"app","level":"INFO"} |
| 31 | +
|
| 32 | +
|
| 33 | +EOBODY; |
| 34 | + |
| 35 | + $this->assertSame('POST', $method); |
| 36 | + $this->assertSame('http://es:9200/_bulk', $url); |
| 37 | + $this->assertSame($body, $options['body']); |
| 38 | + $this->assertSame('content-type: application/json', $options['request_headers'][0]); |
| 39 | + ++$callCount; |
| 40 | + |
| 41 | + return new MockResponse(); |
| 42 | + }; |
| 43 | + |
| 44 | + $handler = new ElasticsearchLogstashHandlerWithHardCodedHostname('http://es:9200', 'log', new MockHttpClient($responseFactory)); |
| 45 | + |
| 46 | + $record = [ |
| 47 | + 'message' => 'My info message', |
| 48 | + 'context' => [], |
| 49 | + 'level' => Logger::INFO, |
| 50 | + 'level_name' => Logger::getLevelName(Logger::INFO), |
| 51 | + 'channel' => 'app', |
| 52 | + 'datetime' => new \DateTime('2020-01-01T00:00:00+01:00'), |
| 53 | + 'extra' => [], |
| 54 | + ]; |
| 55 | + |
| 56 | + $handler->handle($record); |
| 57 | + |
| 58 | + $this->assertSame(1, $callCount); |
| 59 | + } |
| 60 | + |
| 61 | + public function testBandleBatch() |
| 62 | + { |
| 63 | + $callCount = 0; |
| 64 | + $responseFactory = function ($method, $url, $options) use (&$callCount) { |
| 65 | + $body = <<<EOBODY |
| 66 | +{"index":{"_index":"log","_type":"_doc"}} |
| 67 | +{"@timestamp":"2020-01-01T00:00:00.000000+01:00","@version":1,"host":"my hostname","message":"My info message","type":"application","channel":"app","level":"INFO"} |
| 68 | +
|
| 69 | +{"index":{"_index":"log","_type":"_doc"}} |
| 70 | +{"@timestamp":"2020-01-01T00:00:01.000000+01:00","@version":1,"host":"my hostname","message":"My second message","type":"application","channel":"php","level":"WARNING"} |
| 71 | +
|
| 72 | +
|
| 73 | +EOBODY; |
| 74 | + |
| 75 | + $this->assertSame('POST', $method); |
| 76 | + $this->assertSame('http://es:9200/_bulk', $url); |
| 77 | + $this->assertSame($body, $options['body']); |
| 78 | + $this->assertSame('content-type: application/json', $options['request_headers'][0]); |
| 79 | + ++$callCount; |
| 80 | + |
| 81 | + return new MockResponse(); |
| 82 | + }; |
| 83 | + |
| 84 | + $handler = new ElasticsearchLogstashHandlerWithHardCodedHostname('http://es:9200', 'log', new MockHttpClient($responseFactory)); |
| 85 | + |
| 86 | + $records = [ |
| 87 | + [ |
| 88 | + 'message' => 'My info message', |
| 89 | + 'context' => [], |
| 90 | + 'level' => Logger::INFO, |
| 91 | + 'level_name' => Logger::getLevelName(Logger::INFO), |
| 92 | + 'channel' => 'app', |
| 93 | + 'datetime' => new \DateTime('2020-01-01T00:00:00+01:00'), |
| 94 | + 'extra' => [], |
| 95 | + ], |
| 96 | + [ |
| 97 | + 'message' => 'My second message', |
| 98 | + 'context' => [], |
| 99 | + 'level' => Logger::WARNING, |
| 100 | + 'level_name' => Logger::getLevelName(Logger::WARNING), |
| 101 | + 'channel' => 'php', |
| 102 | + 'datetime' => new \DateTime('2020-01-01T00:00:01+01:00'), |
| 103 | + 'extra' => [], |
| 104 | + ], |
| 105 | + ]; |
| 106 | + |
| 107 | + $handler->handleBatch($records); |
| 108 | + |
| 109 | + $this->assertSame(1, $callCount); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +class ElasticsearchLogstashHandlerWithHardCodedHostname extends ElasticsearchLogstashHandler |
| 114 | +{ |
| 115 | + protected function getDefaultFormatter(): FormatterInterface |
| 116 | + { |
| 117 | + return new LogstashFormatter('application', 'my hostname', null, 'ctxt_', LogstashFormatter::V1); |
| 118 | + } |
| 119 | +} |
0 commit comments