2 namespace Consolidation\Log;
5 use Symfony\Component\Console\Output\OutputInterface;
6 use Symfony\Component\Console\Style\OutputStyle;
9 * Styles log output based on format mappings provided in the constructor.
11 * Override for greater control.
13 class LogOutputStyler extends UnstyledLogOutputStyler
15 const TASK_STYLE_INFO = 'fg=white;bg=cyan;options=bold';
16 const TASK_STYLE_SUCCESS = 'fg=white;bg=green;options=bold';
17 const TASK_STYLE_WARNING = 'fg=black;bg=yellow;options=bold;';
18 const TASK_STYLE_ERROR = 'fg=white;bg=red;options=bold';
20 protected $defaultStyles = [
21 '*' => LogLevel::INFO,
23 protected $labelStyles = [
24 LogLevel::EMERGENCY => self::TASK_STYLE_ERROR,
25 LogLevel::ALERT => self::TASK_STYLE_ERROR,
26 LogLevel::CRITICAL => self::TASK_STYLE_ERROR,
27 LogLevel::ERROR => self::TASK_STYLE_ERROR,
28 LogLevel::WARNING => self::TASK_STYLE_WARNING,
29 LogLevel::NOTICE => self::TASK_STYLE_INFO,
30 LogLevel::INFO => self::TASK_STYLE_INFO,
31 LogLevel::DEBUG => self::TASK_STYLE_INFO,
32 ConsoleLogLevel::SUCCESS => self::TASK_STYLE_SUCCESS,
34 protected $messageStyles = [
35 LogLevel::EMERGENCY => self::TASK_STYLE_ERROR,
36 LogLevel::ALERT => self::TASK_STYLE_ERROR,
37 LogLevel::CRITICAL => self::TASK_STYLE_ERROR,
38 LogLevel::ERROR => self::TASK_STYLE_ERROR,
39 LogLevel::WARNING => '',
40 LogLevel::NOTICE => '',
42 LogLevel::DEBUG => '',
43 ConsoleLogLevel::SUCCESS => '',
46 public function __construct($labelStyles = [], $messageStyles = [])
48 $this->labelStyles = $labelStyles + $this->labelStyles;
49 $this->messageStyles = $messageStyles + $this->messageStyles;
55 public function defaultStyles()
57 return $this->defaultStyles;
63 public function style($context)
65 $context += ['_style' => []];
66 $context['_style'] += $this->defaultStyles();
67 foreach ($context as $key => $value) {
69 if (!isset($context['_style'][$styleKey])) {
72 if (is_string($value) && isset($context['_style'][$styleKey])) {
73 $style = $context['_style'][$styleKey];
74 $context[$key] = $this->wrapFormatString($context[$key], $style);
81 * Wrap a string in a format element.
83 protected function wrapFormatString($string, $style)
86 return "<{$style}>$string</>";
92 * Look up the label and message styles for the specified log level,
93 * and use the log level as the label for the log message.
95 protected function formatMessageByLevel($level, $message, $context)
98 return $this->formatMessage($label, $message, $context, $this->labelStyles[$level], $this->messageStyles[$level]);
102 * Apply styling with the provided label and message styles.
104 protected function formatMessage($label, $message, $context, $labelStyle, $messageStyle = '')
106 if (!empty($messageStyle)) {
107 $message = $this->wrapFormatString(" $message ", $messageStyle);
109 if (!empty($label)) {
110 $message = ' ' . $this->wrapFormatString("[$label]", $labelStyle) . ' ' . $message;