-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Console] Added standalone PSR-3 compliant logger #3696
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ Console | |
changing_default_command | ||
single_command_tool | ||
events | ||
logger | ||
helpers/index |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
.. index:: | ||
single: Console; Logger | ||
|
||
Using the Logger | ||
================ | ||
|
||
.. versionadded:: 2.5 | ||
The :class:`Symfony\\Component\\Console\\Logger\\ConsoleLogger` was | ||
introduced in Symfony 2.5. | ||
|
||
The Console component comes with a standalone logger complying with the | ||
`PSR-3_` standard. | ||
Depending of the verbosity setting, log messages will be sent to the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. depending on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending on [...] |
||
:class:`Symfony\\Component\\Console\\Output\\OutputInterface` instance | ||
passed as a parameter to the constructor. | ||
|
||
The logger 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:: | ||
|
||
namespace Acme; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
class MyDependency | ||
{ | ||
private $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function doStuff() | ||
{ | ||
$this->logger->info('I love Tony Vairelles\' hairdresser.'); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe state a sentence here that this is the usage inside the command? |
||
You can rely on the logger to use this dependency inside a command:: | ||
|
||
namespace Acme\Console\Command; | ||
|
||
use Acme\MyDependency; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Logger\ConsoleLogger; | ||
|
||
class MyCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('my:command') | ||
->setDescription( | ||
'Use an external dependency requiring a PSR-3 logger' | ||
) | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rewrite these lines: $this->setName('my:command')
->setDescription(
'Use an external dependency requiring a PSR-3 logger'
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just do: $this
->setName('my:command')
->setDescription(
'Use an external dependency requiring a PSR-3 logger'
)
; |
||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$logger = new ConsoleLogger($output); | ||
|
||
$myDependency = MyDependency($logger); | ||
$myDependency->doStuff(); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a little explaination of what will happen when you execute the command |
||
The dependency will use the instance of | ||
``Symfony\\Component\\Console\\Logger\\ConsoleLogger`` as logger. | ||
Log messages emitted will be displayed on the console output. | ||
|
||
Verbosity | ||
--------- | ||
|
||
Depending on the verbosity level that the command is run, messages may or | ||
may not be sent to the ``Symfony\\Component\\Console\\Output\\OutputInterface`` instance. | ||
|
||
By default, the console logger behaves like the | ||
:doc:`Monolog's Console Handler </cookbook/logging/monolog_console>`. | ||
The association between the log level and the verbosity can be configured | ||
through the second parameter of the :class:`Symfony\\Component\\Console\\ConsoleLogger` | ||
constructor:: | ||
|
||
// ... | ||
$verbosityLevelMap = array( | ||
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, | ||
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL, | ||
); | ||
$logger = new ConsoleLogger($output, $verbosityLevelMap); | ||
|
||
Color | ||
----- | ||
|
||
The logger outputs the log messages formatted with a color reflecting their | ||
level. This behavior is configurable through the third parameter of the | ||
constructor:: | ||
|
||
// ... | ||
private $formatLevelMap = array( | ||
LogLevel::CRITICAL => self::INFO, | ||
LogLevel::DEBUG => self::ERROR, | ||
); | ||
$logger = new ConsoleLogger($output, array(), $formatLevelMap); | ||
|
||
.. _PSR-3: http://www.php-fig.org/psr/psr-3/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we need change
PSR-3_
to_PSR-3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the underscore should be after the closing backtick instead of before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #3721