8000 Only allow one output and a minimal level. Add tests for the compiler… · symfony/symfony@2d32b3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d32b3b

Browse files
committed
Only allow one output and a minimal level. Add tests for the compiler pass.
1 parent 2840059 commit 2d32b3b

File tree

4 files changed

+119
-54
lines changed

4 files changed

+119
-54
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/LoggerPass.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public function process(ContainerBuilder $container)
3939
$loggerDefinition = $container->register('logger', Logger::class);
4040
$loggerDefinition->setPublic(false);
4141
if ($container->getParameter('kernel.debug')) {
42-
$loggerDefinition->addArgument(array(
43-
LogLevel::NOTICE => 'php://stderr',
44-
LogLevel::INFO => 'php://stderr',
45-
LogLevel::DEBUG => 'php://stderr',
46-
));
42+
$loggerDefinition->addArgument(LogLevel::DEBUG);
4743
}
4844
}
4945
}

src/Symfony/Bundle/FrameworkBundle/Logger/Logger.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,64 @@
1616
use Psr\Log\LogLevel;
1717

1818
/**
19-
* Minimalist PSR-3 logger designed to write in stdout, stderr or any other stream.
19+
* Minimalist PSR-3 logger designed to write in stderr or any other stream.
2020
*
2121
* @internal
2222
*
2323
* @author Kévin Dunglas <dunglas@gmail.com>
2424
*/
2525
final class Logger extends AbstractLogger
2626
{
27-
private static $defaultOutputByLevel = array(
28-
LogLevel::EMERGENCY => 'php://stderr',
29-
LogLevel::ALERT => 'php://stderr',
30-
LogLevel::CRITICAL => 'php://stderr',
31-
LogLevel::ERROR => 'php://stderr',
32-
LogLevel::WARNING => 'php://stderr',
33-
LogLevel::NOTICE => false,
34-
LogLevel::INFO => false,
35-
LogLevel::DEBUG => false,
27+
private static $levels = array(
28+
LogLevel::DEBUG,
29+
LogLevel::INFO,
30+
LogLevel::NOTICE,
31+
LogLevel::WARNING,
32+
LogLevel::ERROR,
33+
LogLevel::CRITICAL,
34+
LogLevel::ALERT,
35+
LogLevel::EMERGENCY,
3636
);
37-
private $outputByLevel;
38-
private $handles = array();
37+
38+
private $minLevelIndex;
3939
private $formatter;
40+
private $handle;
4041

41-
public function __construct(array $outputByLevel = array(), callable $formatter = null)
42+
public function __construct($minLevel = LogLevel::WARNING, $output = 'php://stderr', callable $formatter = null)
4243
{
43-
$this->outputByLevel = $outputByLevel + self::$defaultOutputByLevel;
44+
if (!in_array($minLevel, self::$levels)) {
45+
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel));
46+
}
47+
48+
$this->minLevelIndex = array_search($minLevel, self::$levels, true);
4449
$this->formatter = $formatter;
50+
if (false === $this->handle = @fopen($output, 'a')) {
51+
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
52+
}
4553
}
4654

4755
public function __destruct()
4856
{
49-
foreach ($this->handles as $handle) {
50-
fclose($handle);
51-
}
57+
fclose($this->handle);
5258
}
5359

5460
/**
5561
* {@inheritdoc}
5662
*/
5763
public function log($level, $message, array $context = array())
5864
{
59-
if (!isset($this->outputByLevel[$level])) {
65+
if (!in_array($level, self::$levels)) {
6066
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
6167
}
6268

63-
if ($stream = $this->outputByLevel[$level]) {
64-
if (!isset($this->handles[$stream])) {
65-
$this->handles[$stream] = fopen($stream, 'a');
66-
}
69+
if ($this->minLevelIndex && array_search($level, self::$levels, true) < $this->minLevelIndex) {
70+
return;
71+
}
6772

68-
$formatter = $this->formatter;
69-
$message = $formatter ? $formatter($level, $message, $context) : sprintf('[%s] %s', $level, $this->interpolate($message, $context)).\PHP_EOL;
73+
$formatter = $this->formatter;
74+
$message = $formatter ? $formatter($level, $message, $context) : sprintf('[%s] %s', $level, $this->interpolate($message, $context)).\PHP_EOL;
7075

71-
fwrite($this->handles[$stream], $message);
72-
}
76+
fwrite($this->handle, $message);
7377
}
7478

7579
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
use Psr\Log\LogLevel;
17+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerPass;
18+
use Symfony\Bundle\FrameworkBundle\Logger\Logger;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
21+
/**
22+
* @author Kévin Dunglas <dunglas@gmail.com>
23+
*/
24+
class LoggerPassTest extends TestCase
25+
{
26+
public function testAlwaysSetAutowiringAlias()
27+
{
28+
$container = new ContainerBuilder();
29+
$container->register('logger','Foo');
30+
31+
(new LoggerPass())->process($container);
32+
33+
$this->assertFalse($container->getAlias(LoggerInterface::class)->isPublic());
34+
}
35+
36+
public function testDoNotOverrideExistingLogger()
37+
{
38+
$container = new ContainerBuilder();
39+
$container->register('logger','Foo');
40+
41+
(new LoggerPass())->process($container);
42+
43+
$this->assertSame('Foo', $container->getDefinition('logger')->getClass());
44+
}
45+
46+
public function testRegisterLogger()
47+
{
48+
$container = new ContainerBuilder();
49+
$container->setParameter('kernel.debug', false);
50+
51+
(new LoggerPass())->process($container);
52+
53+
$definition = $container->getDefinition('logger');
54+
$this->assertSame(Logger::class, $definition->getClass());
55+
$this->assertFalse($definition->isPublic());
56+
}
57+
58+
public function testSetMinLevelWhenDebugging()
59+
{
60+
$container = new ContainerBuilder();
61+
$container->setParameter('kernel.debug', true);
62+
63+
(new LoggerPass())->process($container);
64+
65+
$definition = $container->getDefinition('logger');
66+
$this->assertSame(LogLevel::DEBUG, $definition->getArgument(0));
67+
}
68+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Logger/LoggerTest.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@ class LoggerTest extends TestCase
3535
public function setUp()
3636
{
3737
$this->tmpFile = sys_get_temp_dir().'/log';
38-
$this->logger = new Logger(array(
39-
LogLevel::EMERGENCY => $this->tmpFile,
40-
LogLevel::ALERT => $this->tmpFile,
41-
LogLevel::CRITICAL => $this->tmpFile,
42-
LogLevel::ERROR => $this->tmpFile,
43-
LogLevel::WARNING => $this->tmpFile,
44-
LogLevel::NOTICE => $this->tmpFile,
45-
LogLevel::INFO => $this->tmpFile,
46-
LogLevel::DEBUG => $this->tmpFile,
47-
));
38+
$this->logger = new Logger(LogLevel::DEBUG, $this->tmpFile);
4839
file_put_contents($this->tmpFile, '');
4940
}
5041

@@ -94,7 +85,7 @@ public function provideLevelsAndMessages()
9485

9586
public function testLogLevelDisabled()
9687
{
97-
$this->logger = new Logger(array(LogLevel::DEBUG => false));
88+
$this->logger = new Logger(LogLevel::INFO, $this->tmpFile);
9889

9990
$this->logger->debug('test', array('user' => 'Bob'));
10091
$this->logger->log(LogLevel::DEBUG, 'test', array('user' => 'Bob'));
@@ -108,8 +99,23 @@ public function testLogLevelDisabled()
10899
*/
109100
public function testThrowsOnInvalidLevel()
110101
{
111-
$logger = $this->logger;
112-
$logger->log('invalid level', 'Foo');
102+
$this->logger->log('invalid level', 'Foo');
103+
}
104+
105+
/**
106+
* @expectedException \Psr\Log\InvalidArgumentException
107+
*/
108+
public function testThrowsOnInvalidMinLevel()
109+
{
110+
new Logger('invalid');
111+
}
112+
113+
/**
114+
* @expectedException \Psr\Log\InvalidArgumentException
115+
*/
116+
public function testInvalidOutput()
117+
{
118+
new Logger(LogLevel::DEBUG, '/');
113119
}
114120

115121
public function testContextReplacement()
@@ -172,16 +178,7 @@ public function testContextExceptionKeyCanBeExceptionOrOtherValues()
172178

173179
public function testFormatter()
174180
{
175-
$this->logger = new Logger(array(
176-
LogLevel::EMERGENCY => $this->tmpFile,
177-
LogLevel::ALERT => $this->tmpFile,
178-
LogLevel::CRITICAL => $this->tmpFile,
179-
LogLevel::ERROR => $this->tmpFile,
180-
LogLevel::WARNING => $this->tmpFile,
181-
LogLevel::NOTICE => $this->tmpFile,
182-
LogLevel::INFO => $this->tmpFile,
183-
LogLevel::DEBUG => $this->tmpFile,
184-
), function ($level, $message, $context) {
181+
$this->logger = new Logger(LogLevel::DEBUG, $this->tmpFile, function ($level, $message, $context) {
185182
return json_encode(array('level' => $level, 'message' => $message, 'context' => $context)).\PHP_EOL;
186183
});
187184

0 commit comments

Comments
 (0)
0