|
| 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/ |
0 commit comments