8000 Flush loggers on kernel.reset · symfony/monolog-bundle@366f692 · GitHub
[go: up one dir, main page]

Skip to content

Commit 366f692

Browse files
dnnalyrixx
authored andcommitted
Flush loggers on kernel.reset
1 parent 8204f3c commit 366f692

File tree

10 files changed

+84
-8
lines changed

10 files changed

+84
-8
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ matrix:
2828
- php: 7.0
2929
- php: 7.1
3030
- php: 7.2
31+
- php: 7.3
3132
# Test against dev versions
3233
- php: nightly
3334
env: DEPENDENCIES=dev

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 3.3.2 (2018-12-29)
2+
3+
* Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing
4+
* Fixed regression when `app` channel is defined explicitly
5+
* Fixed handlers marked as nested not being ignored properly from the stack
6+
7+
## 3.3.1 (2018-11-04)
8+
9+
* Fixed compatiblity with Symfony 4.2
10+
111
## 3.3.0 (2018-06-04)
212

313
* Fixed the autowiring of the channel logger in autoconfigured services

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function process(ContainerBuilder $container)
8181

8282
// create additional channels
8383
foreach ($container->getParameter('monolog.additional_channels') as $chan) {
84+
if ($chan === 'app') {
85+
continue;
86+
}
8487
$loggerId = sprintf('monolog.logger.%s', $chan);
8588
$this->createLogger($chan, $loggerId, $container);
8689
$container->getDefinition($loggerId)->setPublic(true);

DependencyInjection/Configuration.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@
293293
* - [level]: level name or int value, defaults to DEBUG
294294
* - [bubble]: bool, defaults to true
295295
*
296+
* All handlers can also be marked with `nested: true` to make sure they are never added explicitly to the stack
297+
*
296298
* @author Jordi Boggiano <j.boggiano@seld.be>
297299
* @author Christophe Coevoet <stof@notk.org>
298300
*/
@@ -305,8 +307,8 @@ class Configuration implements ConfigurationInterface
305307
*/
306308
public function getConfigTreeBuilder()
307309
{
308-
$treeBuilder = new TreeBuilder();
309-
$rootNode = $treeBuilder->root('monolog');
310+
$treeBuilder = new TreeBuilder('monolog');
311+
$rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('monolog');
310312

311313
$rootNode
312314
->fixXmlConfig('channel')
@@ -342,7 +344,7 @@ public function getConfigTreeBuilder()
342344
->booleanNode('bubble')->defaultTrue()->end()
343345
->scalarNode('app_name')->defaultNull()->end()
344346
->booleanNode('include_stacktraces')->defaultFalse()->end()
345-
->booleanNode('process_psr_3_messages')->defaultTrue()->end()
347+
->booleanNode('process_psr_3_messages')->defaultNull()->end()
346348
->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end() // stream and rotating
347349
->scalarNode('file_permission') // stream and rotating
348350
->defaultNull()
@@ -708,6 +710,10 @@ public function getConfigTreeBuilder()
708710
->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_http_codes']) && !empty($v['excluded_404s']); })
709711
->thenInvalid('You can not use excluded_http_codes together with excluded_404s in a FingersCrossedHandler')
710712
->end()
713+
->validate()
714+
->ifTrue(function ($v) { return 'fingers_crossed' !== $v['type'] && (!empty($v['excluded_http_codes']) || !empty($v['excluded_404s'])); })
715+
->thenInvalid('You can only use excluded_http_codes/excluded_404s with a FingersCrossedHandler definition')
716+
->end()
711717
->validate()
712718
->ifTrue(function ($v) { return 'filter' === $v['type'] && "DEBUG" !== $v['min_level'] && !empty($v['accepted_levels']); })
713719
->thenInvalid('You can not use min_level together with accepted_levels in a FilterHandler')

DependencyInjection/MonologExtension.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Config\FileLocator;
1818
use Symfony\Component\DependencyInjection\Definition;
1919
use Symfony\Component\DependencyInjection\Reference;
20+
use Monolog\ResettableInterface;
2021

2122
/**
2223
* MonologExtension is an extension for the Monolog library.
@@ -131,17 +132,26 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
131132
if ('service' === $handler['type']) {
132133
$container->setAlias($handlerId, $handler['id']);
133134

135+
if (!empty($handler['nested']) && true === $handler['nested']) {
136+
$this->markNestedHandler($handlerId);
137+
}
138+
134139
return $handlerId;
135140
}
136141

137-
$definition = new Definition($this->getHandlerClassByType($handler['type']));
142+
$handlerClass = $this->getHandlerClassByType($handler['type']);
143+
$definition = new Definition($handlerClass);
138144

139145
$handler['level'] = $this->levelToMonologConst($handler['level']);
140146

141147
if ($handler['include_stacktraces']) {
142148
$definition->setConfigurator(array('Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces'));
143149
}
144150

151+
if (null === $handler['process_psr_3_messages']) {
152+
$handler['process_psr_3_messages'] = !isset($handler['handler']) && !$handler['members'];
153+
}
154+
145155
if ($handler['process_psr_3_messages']) {
146156
$processorId = 'monolog.processor.psr_log_message';
147157
if (!$container->hasDefinition($processorId)) {
@@ -718,7 +728,12 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
718728
break;
719729

720730
default:
721-
throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"', $handler['type'], $name));
731+
$nullWarning = '';
732+
if ($handler['type'] == '') {
733+
$nullWarning = ', if you meant to define a null handler in a yaml config, make sure you quote "null" so it does not get converted to a php null';
734+
}
735+
736+
throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"' . $nullWarning, $handler['type'], $name));
722737
}
723738

724739
if (!empty($handler['nested']) && true === $handler['nested']) {
@@ -728,6 +743,11 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
728743
if (!empty($handler['formatter'])) {
729744
$definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
730745
}
746+
747+
if (!in_array($handlerId, $this->nestedHandlers) && is_subclass_of($handlerClass, ResettableInterface::class)) {
748+
$definition->addTag('kernel.reset', array('method' => 'reset'));
749+
}
750+
731751
$container->setDefinition($handlerId, $definition);
732752

733753
return $handlerId;

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2004-2012 Fabien Potencier
1+
Copyright (c) 2004-2019 Fabien Potencier
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ public function testTagNotBreakingIfNoLogger()
134134
$this->assertEquals(array(), $dummyService->getArguments());
135135
}
136136

137+
public function testChannelsConfigurationOptionSupportsAppChannel()
138+
{
139+
$container = $this->getFunctionalContainer();
140+
141+
$container->setParameter('monolog.additional_channels', array('app'));
142+
$container->compile();
143+
}
144+
137145
private function getContainer()
138146
{
139147
$container = new ContainerBuilder();
@@ -202,6 +210,9 @@ private function getContainerWithSetter()
202210
return $container;
203211
}
204212

213+
/**
214+
* @return ContainerBuilder
215+
*/
205216
private function getFunctionalContainer()
206217
{
207218
$container = new ContainerBuilder();

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function testPsr3MessageProcessingEnabled()
202202

203203
$methodCalls = $logger->getMethodCalls();
204204

205-
$this->assertContains(array('pushProcessor', array(new Reference('monolog.processor.psr_log_message'))), $methodCalls, 'The PSR-3 processor should not be enabled', false, false);
205+
$this->assertContains(array('pushProcessor', array(new Reference('monolog.processor.psr_log_message'))), $methodCalls, 'The PSR-3 processor should be enabled', false, false);
206206
}
207207

208208
public function testPsr3MessageProcessingDisabled()

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@ public function testLoadWithServiceHandler()
8484
$this->assertTrue($container->hasDefinition('monolog.logger'));
8585
$this->assertTrue($container->hasAlias('monolog.handler.custom'));
8686

87+
$logger = $container->getDefinition('monolog.logger');
88+
// Custom service handler must be pushed to logger
89+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
90+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
91+
92+
$handler = $container->findDefinition('monolog.handler.custom');
93+
$this->assertDICDefinitionClass($handler, 'stdClass');
94+
$this->assertDICConstructorArguments($handler, array('foo', false));
95+
}
96+
97+
public function testLoadWithNestedServiceHandler()
98+
{
99+
$container = $this->getContainer(
100+
array(array('handlers' => array('custom' => array('type' => 'service', 'id' => 'some.service.id', 'nested' => true)))),
101+
array('some.service.id' => new Definition('stdClass', array('foo', false)))
102+
);
103+
104+
$this->assertTrue($container->hasDefinition('monolog.logger'));
105+
$this->assertTrue($container->hasAlias('monolog.handler.custom'));
106+
107+
$logger = $container->getDefinition('monolog.logger');
108+
// Nested service handler must not be pushed to logger
109+
$this->assertCount(1, $logger->getMethodCalls());
110+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
111+
87112
$handler = $container->findDefinition('monolog.handler.custom');
88113
$this->assertDICDefinitionClass($handler, 'stdClass');
89114
$this->assertDICConstructorArguments($handler, array('foo', false));

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require-dev": {
2727
"symfony/yaml": "~2.7|~3.3|~4.0",
2828
"symfony/console": "~2.7|~3.3|~4.0",
29-
"symfony/phpunit-bridge": "^3.3|^4.0"
29+
"symfony/phpunit-bridge": "^3.4.19|^4.0"
3030
},
3131
"autoload": {
3232
"psr-4": { "Symfony\\Bundle\\MonologBundle\\": "" },

0 commit comments

Comments
 (0)
0