-
-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for configuring Gelf encoders in Monolog configuration #492
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures; | ||
|
||
class DummyClassForClassExistsCheck | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor; | ||
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessorWithPriority; | ||
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor; | ||
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\DummyClassForClassExistsCheck; | ||
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\ServiceWithChannel; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
@@ -186,6 +187,97 @@ public function testExceptionWhenUsingGelfWithoutPublisherHostname() | |
$loader->load([['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => []]]]], $container); | ||
} | ||
|
||
public function testExceptionWhenUsingLegacyGelfImplementationWithUnsupportedEncoder(): void | ||
{ | ||
if (!class_exists('Gelf\MessagePublisher')) { | ||
class_alias(DummyClassForClassExistsCheck::class, 'Gelf\MessagePublisher'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing such class aliases is a hack, and won't work properly. If the Tests should be written against the installed dependencies, without such hacks, and skipped in case they are not relevant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback; I completely agree. While this approach is hackish, it did feel wrong that the changes couldn’t be properly tested. I initially used these as part of my testing process and I left them in the PR... As I mentioned in my previous comment:
I'll remove them. |
||
} | ||
|
||
$container = new ContainerBuilder(); | ||
$loader = new MonologExtension(); | ||
|
||
$this->expectException(InvalidConfigurationException::class); | ||
|
||
$loader->load([['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => ['hostname' => 'localhost', 'encoder' => 'json']]]]], $container); | ||
} | ||
|
||
/** | ||
* @dataProvider encoderOptionsProvider | ||
*/ | ||
public function testLegacyGelfImplementationEncoderOption(array $config): void | ||
{ | ||
if (!class_exists('Gelf\MessagePublisher')) { | ||
class_alias(DummyClassForClassExistsCheck::class, 'Gelf\MessagePublisher'); | ||
} | ||
|
||
$container = $this->getContainer($config); | ||
$this->assertTrue($container->hasDefinition('monolog.handler.gelf')); | ||
|
||
$handler = $container->getDefinition('monolog.handler.gelf'); | ||
/** @var Definition $publisher */ | ||
$publisher = $handler->getArguments()[0]; | ||
|
||
$this->assertDICConstructorArguments($publisher, ['localhost', 12201, 1420]); | ||
} | ||
|
||
public function encoderOptionsProvider(): array | ||
{ | ||
return [ | ||
[ | ||
[['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => ['hostname' => 'localhost', 'encoder' => 'compressed_json']]]]], | ||
], | ||
[ | ||
[['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => ['hostname' => 'localhost']]]]], | ||
], | ||
]; | ||
} | ||
|
||
public function testExceptionWhenUsingGelfWithInvalidEncoder(): void | ||
{ | ||
if (!class_exists('Gelf\Transport\UdpTransport')) { | ||
class_alias(DummyClassForClassExistsCheck::class, 'Gelf\Transport\UdpTransport'); | ||
} | ||
|
||
$container = new ContainerBuilder(); | ||
$loader = new MonologExtension(); | ||
|
||
$this->expectException(InvalidConfigurationException::class); | ||
|
||
$loader->load([['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => ['hostname' => 'localhost', 'encoder' => 'invalid_encoder']]]]], $container); | ||
} | ||
|
||
/** | ||
* @dataProvider gelfEncoderProvider | ||
*/ | ||
public function testGelfWithEncoder($encoderValue, $expectedClass): void | ||
{ | ||
if (!class_exists('Gelf\Transport\UdpTransport')) { | ||
class_alias(DummyClassForClassExistsCheck::class, 'Gelf\Transport\UdpTransport'); | ||
} | ||
|
||
$container = $this->getContainer([['handlers' => ['gelf' => ['type' => 'gelf', 'publisher' => ['hostname' => 'localhost', 'encoder' => $encoderValue]]]]]); | ||
$this->assertTrue($container->hasDefinition('monolog.handler.gelf')); | ||
|
||
$handler = $container->getDefinition('monolog.handler.gelf'); | ||
/** @var Definition $publisher */ | ||
$publisher = $handler->getArguments()[0]; | ||
/** @var Definition $transport */ | ||
$transport = $publisher->getMethodCalls()[0][1][0]; | ||
$encoder = $transport->getMethodCalls()[0][1][0]; | ||
|
||
$this->assertDICConstructorArguments($transport, ['localhost', 12201, 1420]); | ||
$this->assertDICDefinitionClass($encoder, $expectedClass); | ||
$this->assertDICConstructorArguments($handler, [$publisher, 'DEBUG', true]); | ||
} | ||
|
||
public function gelfEncoderProvider(): array | ||
{ | ||
return [ | ||
['json', 'Gelf\Encoder\JsonEncoder'], | ||
['compressed_json', 'Gelf\Encoder\CompressedJsonEncoder'], | ||
]; | ||
} | ||
|
||
public function testExceptionWhenUsingServiceWithoutId() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before your change, it was clear that I should either provide a service ID or configure the connection by specifying the hostname, port etc. The host name is not "optional if id is given" as you wrote, it's ignored entirely if a service ID has been configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I like how it was before, since it is clear and sound, however I it lacks details about the options (type, enum options, defaults, etc) and I wanted to provide more verbose information especially for the new option as it is has predefined values. btw I tried to keep consistent with the other docs, this comment is reused from the mongo section. 😃
Would you add comment just like you said "ignored when service ID has been configured" to all of these options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about this approach?