Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6586eaa

Browse files
committed
feature#10194 [Console] Added standalone PSR-3 compliant logger (dunglas)
This PR was merged into the 2.5-dev branch.Discussion----------[Console] Added standalone PSR-3 compliant loggerThis PR adds a standalone, PSR-3 compliant, logger to the Console component. It logs all messages on the console output. Messages of `DEBUG`, `INFO` and `NOTICE` levels are displayed using the `info` format (default to green). Higher levels are displayed using the `error` formatter (default to red).This logger is similar to the [Monolog's Console Handler](http://symfony.com/doc/current/cookbook/logging/monolog_console.html) but does not have any external dependency (except `php-fig/log`). This is useful for console applications and commands needing a lightweight PSR-3 compliant logger (e.g. required by a dependency or to display basic informations to the user).An usage example is available here:https://github.com/dunglas/php-schema.org-model/blob/master/src/SchemaOrgModel/Command/GenerateEntitiesCommand.php#L71| Q | A| ------------- | ---| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | n/a| License | MIT| Doc PR |symfony/symfony-docs#3696Commits-------e40b34d [Console] Added standalone PSR-3 compliant logger
2 parents53fec31 +e40b34d commit6586eaa

File tree

4 files changed

+214
-2
lines changed

4 files changed

+214
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
namespaceSymfony\Component\Console\Logger;
13+
14+
usePsr\Log\AbstractLogger;
15+
usePsr\Log\InvalidArgumentException;
16+
usePsr\Log\LogLevel;
17+
useSymfony\Component\Console\Output\OutputInterface;
18+
useSymfony\Component\Console\Output\ConsoleOutputInterface;
19+
20+
/**
21+
* PSR-3 compliant console logger
22+
*
23+
* @author Kévin Dunglas <dunglas@gmail.com>
24+
* @link http://www.php-fig.org/psr/psr-3/
25+
*/
26+
class ConsoleLoggerextends AbstractLogger
27+
{
28+
constINFO ='info';
29+
constERROR ='error';
30+
31+
/**
32+
* @var OutputInterface
33+
*/
34+
private$output;
35+
/**
36+
* @var array
37+
*/
38+
private$verbosityLevelMap =array(
39+
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
40+
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
41+
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
42+
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
43+
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
44+
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
45+
LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
46+
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG
47+
);
48+
/**
49+
* @var array
50+
*/
51+
private$formatLevelMap =array(
52+
LogLevel::EMERGENCY =>self::ERROR,
53+
LogLevel::ALERT =>self::ERROR,
54+
LogLevel::CRITICAL =>self::ERROR,
55+
LogLevel::ERROR =>self::ERROR,
56+
LogLevel::WARNING =>self::INFO,
57+
LogLevel::NOTICE =>self::INFO,
58+
LogLevel::INFO =>self::INFO,
59+
LogLevel::DEBUG =>self::INFO
60+
);
61+
62+
/**
63+
* @param OutputInterface $output
64+
* @param array $verbosityLevelMap
65+
* @param array $formatLevelMap
66+
*/
67+
publicfunction__construct(OutputInterface$output,array$verbosityLevelMap =array(),array$formatLevelMap =array())
68+
{
69+
$this->output =$output;
70+
$this->verbosityLevelMap =$verbosityLevelMap +$this->verbosityLevelMap;
71+
$this->formatLevelMap =$formatLevelMap +$this->formatLevelMap;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
publicfunctionlog($level,$message,array$context =array())
78+
{
79+
if (!isset($this->verbosityLevelMap[$level])) {
80+
thrownewInvalidArgumentException(sprintf('The log level "%s" does not exist.',$level));
81+
}
82+
83+
// Write to the error output if necessary and available
84+
if ($this->formatLevelMap[$level] ===self::ERROR &&$this->outputinstanceof ConsoleOutputInterface) {
85+
$output =$this->output->getErrorOutput();
86+
}else {
87+
$output =$this->output;
88+
}
89+
90+
if ($output->getVerbosity() >=$this->verbosityLevelMap[$level]) {
91+
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>',$this->formatLevelMap[$level],$level,$this->interpolate($message,$context)));
92+
}
93+
}
94+
95+
/**
96+
* Interpolates context values into the message placeholders
97+
*
98+
* @author PHP Framework Interoperability Group
99+
* @param string $message
100+
* @param array $context
101+
* @return string
102+
*/
103+
privatefunctioninterpolate($message,array$context)
104+
{
105+
// build a replacement array with braces around the context keys
106+
$replace =array();
107+
foreach ($contextas$key =>$val) {
108+
if (!is_array($val) && (!is_object($val) ||method_exists($val,'__toString'))) {
109+
$replace[sprintf('{%s}',$key)] =$val;
110+
}
111+
}
112+
113+
// interpolate replacement values into the message and return
114+
returnstrtr($message,$replace);
115+
}
116+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
namespaceSymfony\Component\Console\Tests\Fixtures;
13+
14+
useSymfony\Component\Console\Output\BufferedOutput;
15+
16+
/**
17+
* Dummy output
18+
*
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class DummyOutputextends BufferedOutput
22+
{
23+
/**
24+
* @return array
25+
*/
26+
publicfunctiongetLogs()
27+
{
28+
$logs =array();
29+
foreach (explode("\n",trim($this->fetch()))as$message) {
30+
preg_match('/^\[(.*)\] (.*)/',$message,$matches);
31+
$logs[] =sprintf('%s %s',$matches[1],$matches[2]);
32+
}
33+
34+
return$logs;
35+
}
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
namespaceSymfony\Component\Console\Tests\Logger;
13+
14+
usePsr\Log\Test\LoggerInterfaceTest;
15+
usePsr\Log\LogLevel;
16+
useSymfony\Component\Console\Logger\ConsoleLogger;
17+
useSymfony\Component\Console\Tests\Fixtures\DummyOutput;
18+
useSymfony\Component\Console\Output\OutputInterface;
19+
20+
/**
21+
* Console logger test
22+
*
23+
* @author Kévin Dunglas <dunglas@gmail.com>
24+
*/
25+
class ConsoleLoggerTestextends LoggerInterfaceTest
26+
{
27+
/**
28+
* @var DummyOutput
29+
*/
30+
protected$output;
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
publicfunctiongetLogger()
36+
{
37+
$this->output =newDummyOutput(OutputInterface::VERBOSITY_VERBOSE);
38+
39+
returnnewConsoleLogger($this->output,array(
40+
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
41+
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
42+
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
43+
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
44+
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
45+
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
46+
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
47+
LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL
48+
));
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
publicfunctiongetLogs()
55+
{
56+
return$this->output->getLogs();
57+
}
58+
}

‎src/Symfony/Component/Console/composer.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
"php":">=5.3.3"
2020
},
2121
"require-dev": {
22-
"symfony/event-dispatcher":"~2.1"
22+
"symfony/event-dispatcher":"~2.1",
23+
"psr/log":"~1.0"
2324
},
2425
"suggest": {
25-
"symfony/event-dispatcher":""
26+
"symfony/event-dispatcher":"",
27+
"psr/log":"For using the console logger"
2628
},
2729
"autoload": {
2830
"psr-0": {"Symfony\\Component\\Console\\":"" }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp