Skip to content

Commit e1acd0a

Browse files
committed
Merge pull request #22 from hason/sockethandler
Added configuration for socket handler
2 parents b731848 + 481134a commit e1acd0a

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public function getConfigTreeBuilder()
112112
->scalarNode('factory-method')->defaultNull()->end()
113113
->end()
114114
->end()
115+
->scalarNode('connection_string')->end() // socket_handler
116+
->scalarNode('timeout')->end() // socket_handler
117+
->scalarNode('connection_timeout')->end() // socket_handler
118+
->booleanNode('persistent')->end() // socket_handler
115119
->arrayNode('channels')
116120
->fixXmlConfig('channel', 'elements')
117121
->canBeUnset()
@@ -188,6 +192,10 @@ public function getConfigTreeBuilder()
188192
->ifTrue(function($v) { return 'gelf' === $v['type'] && !isset($v['publisher']); })
189193
->thenInvalid('The publisher has to be specified to use a GelfHandler')
190194
->end()
195+
->validate()
196+
->ifTrue(function($v) { return 'socket' === $v['type'] && !isset($v['connection_string']); })
197+
->thenInvalid('The connection_string has to be specified to use a SocketHandler')
198+
->end()
191199
->end()
192200
->validate()
193201
->ifTrue(function($v) { return isset($v['debug']); })

DependencyInjection/MonologExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,23 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
260260
));
261261
break;
262262

263+
case 'socket':
264+
$definition->setArguments(array(
265+
$handler['connection_string'],
266+
$handler['level'],
267+
$handler['bubble'],
268+
));
269+
if (isset($handler['timeout'])) {
270+
$definition->addMethodCall('setTimeout', array($handler['timeout']));
271+
}
272+
if (isset($handler['connection_timeout'])) {
273+
$definition->addMethodCall('setConnectionTimeout', array($handler['connection_timeout']));
274+
}
275+
if (isset($handler['persistent'])) {
276+
$definition->addMethodCall('setPersistent', array($handler['persistent']));
277+
}
278+
break;
279+
263280
// Handlers using the constructor of AbstractHandler without adding their own arguments
264281
case 'test':
265282
case 'null':

Resources/config/monolog.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<parameter key="monolog.handler.debug.class">Symfony\Bridge\Monolog\Handler\DebugHandler</parameter>
2121
<parameter key="monolog.handler.swift_mailer.class">Monolog\Handler\SwiftMailerHandler</parameter>
2222
<parameter key="monolog.handler.native_mailer.class">Monolog\Handler\NativeMailerHandler</parameter>
23+
<parameter key="monolog.handler.socket.class">Monolog\Handler\SocketHandler</parameter>
2324

2425
<parameter key="monolog.handler.fingers_crossed.class">Monolog\Handler\FingersCrossedHandler</parameter>
2526
<parameter key="monolog.handler.fingers_crossed.error_level_activation_strategy.class">Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy</parameter>

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1819

1920
class MonologExtensionTest extends DependencyInjectionTest
2021
{
@@ -140,6 +141,34 @@ public function testSyslogHandlerWithLogopts()
140141
$this->assertDICConstructorArguments($handler, array(false, 'user', \Monolog\Logger::DEBUG, true, LOG_CONS));
141142
}
142143

144+
public function testSocketHandler()
145+
{
146+
try {
147+
$this->getContainer(array(array('handlers' => array('socket' => array('type' => 'socket')))));
148+
$this->fail();
149+
} catch (InvalidConfigurationException $e) {
150+
$this->assertContains('connection_string', $e->getMessage());
151+
}
152+
153+
$container = $this->getContainer(array(array('handlers' => array('socket' => array(
154+
'type' => 'socket', 'timeout' => 1, 'persistent' => true,
155+
'connection_string' => 'localhost:50505', 'connection_timeout' => '0.6')
156+
))));
157+
$this->assertTrue($container->hasDefinition('monolog.logger'));
158+
$this->assertTrue($container->hasDefinition('monolog.handler.socket'));
159+
160+
$logger = $container->getDefinition('monolog.logger');
161+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.socket')));
162+
163+
$handler = $container->getDefinition('monolog.handler.socket');
164+
$this->assertDICDefinitionClass($handler, '%monolog.handler.socket.class%');
165+
$this->assertDICConstructorArguments($handler, array('localhost:50505', \Monolog\Logger::DEBUG, true));
166+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'setTimeout', array('1'));
167+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setConnectionTimeout', array('0.6'));
168+
$this->assertDICDefinitionMethodCallAt(2, $handler, 'setPersistent', array(true));
169+
170+
}
171+
143172
protected function getContainer(array $config = array())
144173
{
145174
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)