Skip to content

Commit 1908a15

Browse files
committed
feature #3696 [Console] Added standalone PSR-3 compliant logger (dunglas)
This PR was merged into the master branch. Discussion ---------- [Console] Added standalone PSR-3 compliant logger | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes symfony/symfony#10194 | Applies to | 2.5 Commits ------- 15628e6 [Console] Added standalone PSR-3 compliant logger
2 parents 955526c + 15628e6 commit 1908a15

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Diff for: components/console/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Console
99
changing_default_command
1010
single_command_tool
1111
events
12+
logger
1213
helpers/index

Diff for: components/console/logger.rst

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. index::
2+
single: Console; Logger
3+
4+
Using the Logger
5+
================
6+
7+
.. versionadded:: 2.5
8+
The :class:`Symfony\\Component\\Console\\Logger\\ConsoleLogger` was
9+
introduced in Symfony 2.5.
10+
11+
The Console component comes with a standalone logger complying with the
12+
`PSR-3_` standard.
13+
Depending of the verbosity setting, log messages will be sent to the
14+
:class:`Symfony\\Component\\Console\\Output\\OutputInterface` instance
15+
passed as a parameter to the constructor.
16+
17+
The logger does not have any external dependency except ``php-fig/log``.
18+
This is useful for console applications and commands needing a lightweight
19+
PSR-3 compliant logger::
20+
21+
namespace Acme;
22+
23+
use Psr\Log\LoggerInterface;
24+
25+
class MyDependency
26+
{
27+
private $logger;
28+
29+
public function __construct(LoggerInterface $logger)
30+
{
31+
$this->logger = $logger;
32+
}
33+
34+
public function doStuff()
35+
{
36+
$this->logger->info('I love Tony Vairelles\' hairdresser.');
37+
}
38+
}
39+
40+
You can rely on the logger to use this dependency inside a command::
41+
42+
namespace Acme\Console\Command;
43+
44+
use Acme\MyDependency;
45+
use Symfony\Component\Console\Command\Command;
46+
use Symfony\Component\Console\Input\InputInterface;
47+
use Symfony\Component\Console\Output\OutputInterface;
48+
use Symfony\Component\Console\Logger\ConsoleLogger;
49+
50+
class MyCommand extends Command
51+
{
52+
protected function configure()
53+
{
54+
$this
55+
->setName('my:command')
56+
->setDescription(
57+
'Use an external dependency requiring a PSR-3 logger'
58+
)
59+
;
60+
}
61+
62+
protected function execute(InputInterface $input, OutputInterface $output)
63+
{
64+
$logger = new ConsoleLogger($output);
65+
66+
$myDependency = MyDependency($logger);
67+
$myDependency->doStuff();
68+
}
69+
}
70+
71+
The dependency will use the instance of
72+
``Symfony\\Component\\Console\\Logger\\ConsoleLogger`` as logger.
73+
Log messages emitted will be displayed on the console output.
74+
75+
Verbosity
76+
---------
77+
78+
Depending on the verbosity level that the command is run, messages may or
79+
may not be sent to the ``Symfony\\Component\\Console\\Output\\OutputInterface`` instance.
80+
81+
By default, the console logger behaves like the
82+
:doc:`Monolog's Console Handler </cookbook/logging/monolog_console>`.
83+
The association between the log level and the verbosity can be configured
84+
through the second parameter of the :class:`Symfony\\Component\\Console\\ConsoleLogger`
85+
constructor::
86+
87+
// ...
88+
$verbosityLevelMap = array(
89+
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
90+
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
91+
);
92+
$logger = new ConsoleLogger($output, $verbosityLevelMap);
93+
94+
Color
95+
-----
96+
97+
The logger outputs the log messages formatted with a color reflecting their
98+
level. This behavior is configurable through the third parameter of the
99+
constructor::
100+
101+
// ...
102+
private $formatLevelMap = array(
103+
LogLevel::CRITICAL => self::INFO,
104+
LogLevel::DEBUG => self::ERROR,
105+
);
106+
$logger = new ConsoleLogger($output, array(), $formatLevelMap);
107+
108+
.. _PSR-3: http://www.php-fig.org/psr/psr-3/

Diff for: cookbook/logging/monolog_console.rst

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ It is possible to use the console to print messages for certain
1212
:class:`Symfony\\Component\\Console\\Output\\OutputInterface` instance that
1313
is passed when a command gets executed.
1414

15+
.. seealso::
16+
Alternatively, you can use the
17+
:doc:`standalone PSR-3 logger </components/console/logger>` provided with
18+
the console component.
19+
1520
When a lot of logging has to happen, it's cumbersome to print information
1621
depending on the verbosity settings (``-v``, ``-vv``, ``-vvv``) because the
1722
calls need to be wrapped in conditions. The code quickly gets verbose or dirty.

0 commit comments

Comments
 (0)