1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Seasx\SeasLogger\Targets; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
use Seasx\SeasLogger\ArrayHelper; |
9
|
|
|
use Seasx\SeasLogger\HtmlColor; |
10
|
|
|
use Swoole\WebSocket\Server; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class WebsocketTarget |
14
|
|
|
* @package Seasx\SeasLogger\Targets |
15
|
|
|
*/ |
16
|
|
|
class WebsocketTarget extends AbstractTarget |
17
|
|
|
{ |
18
|
|
|
const COLOR_RANDOM = 'random'; |
19
|
|
|
const COLOR_LEVEL = 'level'; |
20
|
|
|
const COLOR_DEFAULT = 'default'; |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
private $colorTemplate = [ |
24
|
|
|
'Magenta', |
25
|
|
|
self::COLOR_LEVEL, |
26
|
|
|
self::COLOR_LEVEL, |
27
|
|
|
'DarkGray', |
28
|
|
|
'DarkGray', |
29
|
|
|
self::COLOR_RANDOM, |
30
|
|
|
self::COLOR_LEVEL, |
31
|
|
|
'DarkGray', |
32
|
|
|
self::COLOR_LEVEL, |
33
|
|
|
self::COLOR_LEVEL |
34
|
|
|
]; |
35
|
|
|
/** @var string */ |
36
|
|
|
private $default = 'LightGray'; |
37
|
|
|
/** @var callable */ |
38
|
|
|
private $getServer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param callable $function |
42
|
|
|
*/ |
43
|
|
|
public function setGetServer(callable $function): void |
44
|
|
|
{ |
45
|
|
|
$this->getServer = $function; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $messages |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function export(array $messages): void |
53
|
|
|
{ |
54
|
|
|
if (!is_callable($this->getServer)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
/** @var Server $server */ |
58
|
|
|
$swooleServer = call_user_func($this->getServer); |
59
|
|
|
if (!$swooleServer || !$swooleServer instanceof Server) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
foreach ($swooleServer->connections as $fd) { |
63
|
|
|
if ($swooleServer->isEstablished($fd)) { |
|
|
|
|
64
|
|
|
foreach ($messages as $message) { |
65
|
|
|
foreach ($message as $msg) { |
66
|
|
|
if (is_string($msg)) { |
67
|
|
|
switch (ini_get('seaslog.appender')) { |
68
|
|
|
case '2': |
69
|
|
|
case '3': |
70
|
|
|
$msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6))); |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
$msg = explode($this->split, trim($msg)); |
74
|
|
|
$ranColor = $this->default; |
75
|
|
|
} else { |
76
|
|
|
$ranColor = ArrayHelper::remove($msg, '%c'); |
77
|
|
|
} |
78
|
|
|
if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), |
79
|
|
|
$this->levelList)) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
if (empty($ranColor)) { |
83
|
|
|
$ranColor = $this->default; |
84
|
|
|
} elseif (is_array($ranColor) && isset($ranColor['websocket'])) { |
85
|
|
|
$ranColor = $ranColor['websocket']; |
86
|
|
|
} else { |
87
|
|
|
$ranColor = $this->default; |
88
|
|
|
} |
89
|
|
|
$colors = []; |
90
|
|
|
foreach ($msg as $index => $msgValue) { |
91
|
|
|
$msg[$index] = is_string($msgValue) ? trim($msgValue) : (string)$msgValue; |
92
|
|
|
$level = $this->getLevelColor(trim($msg[$this->levelIndex])); |
93
|
|
|
if (isset($this->colorTemplate[$index])) { |
94
|
|
|
$color = $this->colorTemplate[$index]; |
95
|
|
|
switch ($color) { |
96
|
|
|
case self::COLOR_LEVEL: |
97
|
|
|
$colors[] = HtmlColor::getColor($level); |
98
|
|
|
break; |
99
|
|
|
case self::COLOR_RANDOM: |
100
|
|
|
$colors[] = HtmlColor::getColor($ranColor); |
101
|
|
|
break; |
102
|
|
|
case self::COLOR_DEFAULT: |
103
|
|
|
$colors[] = $this->default; |
104
|
|
|
break; |
105
|
|
|
default: |
106
|
|
|
$colors[] = HtmlColor::getColor($color); |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
|
|
$colors[] = $level; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
$msg = json_encode([$msg, $colors], JSON_UNESCAPED_UNICODE); |
113
|
|
|
rgo(function () use ($swooleServer, $fd, $msg) { |
114
|
|
|
$swooleServer->push($fd, $msg); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $level |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
private function getLevelColor(string $level): string |
127
|
|
|
{ |
128
|
|
|
switch (strtolower($level)) { |
129
|
|
|
case LogLevel::INFO: |
130
|
|
|
return "Green"; |
131
|
|
|
case LogLevel::DEBUG: |
132
|
|
|
return 'DarkGray'; |
133
|
|
|
case LogLevel::ERROR: |
134
|
|
|
return "Red"; |
135
|
|
|
case LogLevel::WARNING: |
136
|
|
|
return 'Yellow'; |
137
|
|
|
default: |
138
|
|
|
return 'DarkRed'; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.