10000 Better stream handling, fix some comments. · symfony/symfony@b16dc6c · GitHub
[go: up one dir, main page]

Skip to content

Commit b16dc6c

Browse files
committed
Better stream handling, fix some comments.
1 parent 978d849 commit b16dc6c

File tree

5 files changed

+91
-39
lines changed

5 files changed

+91
-39
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Po 10000 tencier <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\DependencyInjection\Compiler;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\LogLevel;
16+
use Symfony\Bundle\FrameworkBundle\Logger\Logger;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
/**
21+
* Registers the default logger if necessary.
22+
*
23+
* @author Kévin Dunglas <dunglas@gmail.com>
24+
*/
25+
class LoggerPass implements CompilerPassInterface
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function process(ContainerBuilder $container)
31+
{
32+
$loggerDefinition = $container->register('logger', Logger::class);
33+
$loggerDefinition->setPublic(false);
34+
if ($container->getParameter('kernel.debug')) {
35+
$loggerDefinition->addArgument(array(
36+
LogLevel::NOTICE => 'php://stderr',
37+
LogLevel::INFO => 'php://stderr',
38+
LogLevel::DEBUG => 'php://stderr',
39+
));
40+
}
41+
42+
$alias = $container->setAlias(LoggerInterface::class, 'logger');
43+
$alias->setPublic(false);
44+
}
45+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,6 @@ public function load(array $configs, ContainerBuilder $container)
304304
$loader->load('web_link.xml');
305305
}
306306

307-
$loggerDefinition = $container->register(LoggerInterface::class, Logger::class);
308-
$loggerDefinition->setPublic(false);
309-
$container->setAlias('logger', LoggerInterface::class);
310-
311307
$this->addAnnotatedClassesToCompile(array(
312308
'**\\Controller\\',
313309
'**\\Entity\\',

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPrunerPass;
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
21+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerPass;
2122
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
2223
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2324
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
@@ -119,6 +120,7 @@ public function build(ContainerBuilder $container)
119120
$this->addCompilerPassIfExists($container, FormPass::class);
120121
$container->addCompilerPass(new WorkflowGuardListenerPass());
121122
$container->addCompilerPass(new ResettableServicePass());
123+
$container->addCompilerPass(new LoggerPass());
122124

123125
if ($container->getParameter('kernel.debug')) {
124126
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,58 @@
1919
* Minimalist PSR-3 logger designed to write in stdout, stderr or any other stream.
2020
*
2121
* @author Kévin Dunglas <dunglas@gmail.com>
22-
*
23-
* @see http://www.php-fig.org/psr/psr-3/
2422
*/
2523
final class Logger extends AbstractLogger
2624
{
27-
/**
28-
* @var array
29-
*/
30-
private static $defaultLevelToStream = array(
25+
private static $defaultOutputByLevel = array(
3126
LogLevel::EMERGENCY => 'php://stderr',
3227
LogLevel::ALERT => 'php://stderr',
3328
LogLevel::CRITICAL => 'php://stderr',
3429
LogLevel::ERROR => 'php://stderr',
35-
LogLevel::WARNING => 'php://stdout',
36-
LogLevel::NOTICE => 'php://stdout',
37-
LogLevel::INFO => 'php://stdout',
30+
LogLevel::WARNING => 'php://stderr',
31+
LogLevel::NOTICE => false,
32+
LogLevel::INFO => false,
3833
LogLevel::DEBUG => false,
3934
);
40-
41-
private $levelToStream;
42-
private $formatPattern;
43-
44-
/**
45-
* @var bool
46-
*/
35+
private $outputByLevel;
36+
private $format;
37+
private $dateFormat;
4738
private $errored = false;
39+
private $handles = array();
4840

49-
/**
50-
* @param array $levelToStream
51-
* @param string $formatPattern
52-
*/
53-
public function __construct(array $levelToStream = array(), $formatPattern = '[%s] %s')
41+
public function __construct(array $outputByLevel = array(), $format = '[%s] %s', $dateFormat = \DateTime::RFC3339)
5442
{
55-
$this->levelToStream = $levelToStream + self::$defaultLevelToStream;
56-
$this->formatPattern = $formatPattern;
43+
$this->outputByLevel = $outputByLevel + self::$defaultOutputByLevel;
44+
$this->format = $format;
45+
$this->dateFormat = $dateFormat;
46+
}
47+
48+
public function __destruct()
49+
{
50+
foreach ($this->handles as $handle) {
51+
fclose($handle);
52+
}
5753
}
5854

5955
/**
6056
* {@inheritdoc}
6157
*/
6258
public function log($level, $message, array $context = array())
6359
{
64-
if (!isset($this->levelToStream[$level])) {
60+
if (!isset($this->outputByLevel[$level])) {
6561
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
6662
}
6763

6864
if (!$this->errored) {
6965
$this->errored = in_array($level, array(LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR));
7066
}
7167

72-
if ($this->levelToStream[$level]) {
73-
file_put_contents($this->levelToStream[$level], sprintf($this->formatPattern, $level, $this->interpolate($message, $context)).PHP_EOL, FILE_APPEND);
68+
if ($stream = $this->outputByLevel[$level]) {
69+
if (!isset($this->handles[$stream])) {
70+
$this->handles[$stream] = fopen($stream, 'a');
71+
}
72+
73+
fwrite($this->handles[$stream], sprintf($this->format, $level, $this->interpolate($message, $context)).PHP_EOL);
7474
}
7575
}
7676

@@ -87,24 +87,33 @@ public function hasErrored()
8787
/**
8888
* Interpolates context values into the message placeholders.
8989
*
90-
* @author PHP Framework Interoperability Group
90+
* @author Jordi Boggiano <j.boggiano@seld.be>
91+
*
92+
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/PsrLogMessageProcessor.php
9193
*
9294
* @param string $message
93-
* @param array $context
9495
*
9596
* @return string
9697
*/
< 6377 /td>
9798
private function interpolate($message, array $context)
9899
{
99-
// build a replacement array with braces around the context keys
100-
$replace = array();
100+
if (false === strpos($message, '{')) {
101+
return $message;
102+
}
103+
104+
$replacements = array();
101105
foreach ($context as $key => $val) {
102-
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
103-
$replace[sprintf('{%s}', $key)] = $val;
106+
if (null === $val || \is_scalar($val) || (\is_object($val) && \method_exists($val, '__toString'))) {
107+
$replacements["{{$key}}"] = $val;
108+
} elseif ($val instanceof \DateTimeInterface) {
109+
$replacements["{{$key}}"] = $val->format($this->dateFormat);
110+
} elseif (is_object($val)) {
111+
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
112+
} else {
113+
$replacements["{{$key}}"] = '['.\gettype($val).']';
104114
}
105115
}
106116

107-
// interpolate replacement values into the message and return
108-
return strtr($message, $replace);
117+
return strtr($message, $replacements);
109118
}
110119
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function testObjectCastToString()
140140
} else {
141141
$dummy = $this->getMock(DummyTest::class, array('__toString'));
142142
}
143-
$dummy->expects($this->once())
143+
$dummy->expects($this->atLeastOnce())
144144
->method('__toString')
145145
->will($this->returnValue('DUMMY'));
146146

0 commit comments

Comments
 (0)
0