|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Seasx\SeasLogger\Targets; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Log\LogLevel; |
|
7
|
|
|
use Seasx\SeasLogger\ArrayHelper; |
|
8
|
|
|
use Wujunze\Colors; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class StyleTarget |
|
12
|
|
|
* @package Seasx\SeasLogger\Targets |
|
13
|
|
|
*/ |
|
14
|
|
|
class StyleTarget extends AbstractTarget |
|
15
|
|
|
{ |
|
16
|
|
|
const COLOR_RANDOM = 'random'; |
|
17
|
|
|
const COLOR_DEFAULT = 'default'; |
|
18
|
|
|
const COLOR_LEVEL = 'level'; |
|
19
|
|
|
/** @var Colors */ |
|
20
|
|
|
private $color; |
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $colorTemplate = [ |
|
23
|
|
|
'magenta', |
|
24
|
|
|
self::COLOR_LEVEL, |
|
25
|
|
|
self::COLOR_LEVEL, |
|
26
|
|
|
'dark_gray', |
|
27
|
|
|
'dark_gray', |
|
28
|
|
|
self::COLOR_RANDOM, |
|
29
|
|
|
self::COLOR_LEVEL, |
|
30
|
|
|
'dark_gray', |
|
31
|
|
|
self::COLOR_LEVEL |
|
32
|
|
|
]; |
|
33
|
|
|
private $default = null; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $splitColor = 'cyan'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* StyleTarget constructor. |
|
39
|
|
|
* @param array $levelList |
|
40
|
|
|
* @param Colors|null $color |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $levelList = [], ?Colors $color = null) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($color === null) { |
|
45
|
|
|
$color = new Colors(); |
|
46
|
|
|
} |
|
47
|
|
|
$this->color = $color; |
|
48
|
|
|
$this->levelList = $levelList; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $messages |
|
53
|
|
|
*/ |
|
54
|
|
|
public function export(array $messages): void |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($messages as $message) { |
|
57
|
|
|
foreach ($message as $msg) { |
|
58
|
|
|
if (is_string($msg)) { |
|
59
|
|
|
switch (ini_get('seaslog.appender')) { |
|
60
|
|
|
case '2': |
|
61
|
|
|
case '3': |
|
62
|
|
|
$msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6))); |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
$msg = explode($this->split, trim($msg)); |
|
66
|
|
|
$ranColor = $this->default; |
|
67
|
|
|
} else { |
|
68
|
|
|
$ranColor = ArrayHelper::remove($msg, '%c'); |
|
69
|
|
|
} |
|
70
|
|
|
if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), $this->levelList)) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
if (empty($ranColor)) { |
|
74
|
|
|
$ranColor = $this->default; |
|
75
|
|
|
} elseif (is_array($ranColor) && isset($ranColor['console'])) { |
|
76
|
|
|
$ranColor = $ranColor['console']; |
|
77
|
|
|
} else { |
|
78
|
|
|
$ranColor = $this->default; |
|
79
|
|
|
} |
|
80
|
|
|
$context = []; |
|
81
|
|
|
foreach ($msg as $index => $msgValue) { |
|
82
|
|
|
$level = $this->getLevelColor(trim($msg[$this->levelIndex])); |
|
83
|
|
|
if (isset($this->colorTemplate[$index])) { |
|
84
|
|
|
$color = $this->colorTemplate[$index]; |
|
85
|
|
|
$msgValue = is_string($msgValue) ? trim($msgValue) : (string)$msgValue; |
|
86
|
|
|
switch ($color) { |
|
87
|
|
|
case self::COLOR_LEVEL: |
|
88
|
|
|
$context[] = $this->color->getColoredString($msgValue, $level); |
|
89
|
|
|
break; |
|
90
|
|
|
case self::COLOR_DEFAULT: |
|
91
|
|
|
$context[] = $this->color->getColoredString($msgValue, $this->default); |
|
92
|
|
|
break; |
|
93
|
|
|
case self::COLOR_RANDOM: |
|
94
|
|
|
$context[] = $this->color->getColoredString($msgValue, $ranColor); |
|
95
|
|
|
break; |
|
96
|
|
|
default: |
|
97
|
|
|
$context[] = $this->color->getColoredString($msgValue, $color); |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
$context[] = $this->color->getColoredString($msgValue, $level); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if (isset($context)) { |
|
104
|
|
|
echo implode(' ' . $this->color->getColoredString('|', $this->splitColor) . ' ', |
|
105
|
|
|
$context) . PHP_EOL; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $level |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getLevelColor(string $level): string |
|
116
|
|
|
{ |
|
117
|
|
|
switch (strtolower($level)) { |
|
118
|
|
|
case LogLevel::INFO: |
|
119
|
|
|
return "green"; |
|
120
|
|
|
case LogLevel::DEBUG: |
|
121
|
|
|
return 'dark_gray'; |
|
122
|
|
|
case LogLevel::ERROR: |
|
123
|
|
|
return "red"; |
|
124
|
|
|
case LogLevel::WARNING: |
|
125
|
|
|
return 'yellow'; |
|
126
|
|
|
default: |
|
127
|
|
|
return 'light_red'; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |