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

Appearance settings

Commit 15628e6

Browse filesBrowse files
committed
[Console] Added standalone PSR-3 compliant logger
1 parent e40733d commit 15628e6
Copy full SHA for 15628e6

File tree

Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed

‎components/console/index.rst

Copy file name to clipboardExpand all lines: components/console/index.rst
+1Lines changed: 1 addition & 0 deletions
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

‎components/console/logger.rst

Copy file name to clipboard
+108Lines changed: 108 additions & 0 deletions
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/

‎cookbook/logging/monolog_console.rst

Copy file name to clipboardExpand all lines: cookbook/logging/monolog_console.rst
+5Lines changed: 5 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.