Logger::ERROR, OutputInterface::VERBOSITY_NORMAL => Logger::WARNING, OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE, OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO, OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG, ); /** * Map log levels to colors. * * @var array */ static protected $colors = array( Logger::ERROR => 'red', Logger::WARNING => 'yellow', Logger::NOTICE => 'green', // No color markup below NOTICE Logger::INFO => false ); /** * @var OutputInterface */ protected $output; /** * @param OutputInterface $output */ public function __construct(OutputInterface $output) { parent::__construct(static::$levels[$output->getVerbosity()]); $this->output = $output; $this->pushProcessor(array($this, 'addConsoleColor')); } /** * Write a log entry to the terminal. * @param array $record */ protected function write(array $record) { if ($record['level'] >= Logger::ERROR && $this->output instanceof ConsoleOutputInterface) { $output = $this->output->getErrorOutput(); } else { $output = $this->output; } $output->write($record['formatted']); } /** * Enable the enhancements of the default formatter. * * @return FormatterInterface */ protected function getDefaultFormatter() { $formatter = parent::getDefaultFormatter(); $formatter->ignoreEmptyContextAndExtra(true); $formatter->allowInlineLineBreaks(true); $formatter->includeStacktraces(true); return $formatter; } /** * Add console coloring to the message. * * @param array $record * @return array * * @internal Used as a Processor. */ public function addConsoleColor($record) { foreach (static::$colors as $level => $color) { if ($record['level'] >= $level) { break; } } if ($color !== false) { $record['message'] = sprintf('%s', $color, rtrim($record['message']), $color); } return $record; } }