1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\Log; |
3
|
|
|
|
4
|
|
|
use Psr\Log\LogLevel; |
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Styles log output based on format mappings provided in the constructor. |
10
|
|
|
* |
11
|
|
|
* Override for greater control. |
12
|
|
|
*/ |
13
|
|
|
class LogOutputStyler extends UnstyledLogOutputStyler |
14
|
|
|
{ |
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'; |
19
|
|
|
|
20
|
|
|
protected $defaultStyles = [ |
21
|
|
|
'*' => LogLevel::INFO, |
22
|
|
|
]; |
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, |
33
|
|
|
]; |
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 => '', |
41
|
|
|
LogLevel::INFO => '', |
42
|
|
|
LogLevel::DEBUG => '', |
43
|
|
|
ConsoleLogLevel::SUCCESS => '', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
public function __construct($labelStyles = [], $messageStyles = []) |
47
|
|
|
{ |
48
|
|
|
$this->labelStyles = $labelStyles + $this->labelStyles; |
49
|
|
|
$this->messageStyles = $messageStyles + $this->messageStyles; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function defaultStyles() |
56
|
|
|
{ |
57
|
|
|
return $this->defaultStyles; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function style($context) |
64
|
|
|
{ |
65
|
|
|
$context += ['_style' => []]; |
66
|
|
|
$context['_style'] += $this->defaultStyles(); |
67
|
|
|
foreach ($context as $key => $value) { |
68
|
|
|
$styleKey = $key; |
69
|
|
|
if (!isset($context['_style'][$styleKey])) { |
70
|
|
|
$styleKey = '*'; |
71
|
|
|
} |
72
|
|
|
if (is_string($value) && isset($context['_style'][$styleKey])) { |
73
|
|
|
$style = $context['_style'][$styleKey]; |
74
|
|
|
$context[$key] = $this->wrapFormatString($context[$key], $style); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $context; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Wrap a string in a format element. |
82
|
|
|
*/ |
83
|
|
|
protected function wrapFormatString($string, $style) |
84
|
|
|
{ |
85
|
|
|
if ($style) { |
86
|
|
|
return "<{$style}>$string</>"; |
87
|
|
|
} |
88
|
|
|
return $string; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
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. |
94
|
|
|
*/ |
95
|
|
|
protected function formatMessageByLevel($level, $message, $context) |
96
|
|
|
{ |
97
|
|
|
$label = $level; |
98
|
|
|
return $this->formatMessage($label, $message, $context, $this->labelStyles[$level], $this->messageStyles[$level]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Apply styling with the provided label and message styles. |
103
|
|
|
*/ |
104
|
|
|
protected function formatMessage($label, $message, $context, $labelStyle, $messageStyle = '') |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (!empty($messageStyle)) { |
107
|
|
|
$message = $this->wrapFormatString(" $message ", $messageStyle); |
108
|
|
|
} |
109
|
|
|
if (!empty($label)) { |
110
|
|
|
$message = ' ' . $this->wrapFormatString("[$label]", $labelStyle) . ' ' . $message; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $message; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.