-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogThread.php
215 lines (178 loc) · 8.09 KB
/
LogThread.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
namespace libLogDNA;
use libLogDNA\task\LogRegisterTask;
use LogLevel;
use pocketmine\Server;
use pocketmine\thread\Thread;
use pocketmine\thread\Worker;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetException;
use ReflectionClass;
use Threaded;
/**
* Logging utility which utilizes to log all messages even when the message comes from another thread.
*/
class LogThread extends Thread
{
public const PUBLISHING_DELAY = 5; // every 5 seconds
public const PUBLISHING_MAX_LINES = 20; // 20 lines per query.
public const PUBLISHER_URL = "https://logs.logdna.com/logs/ingest?hostname={host}&tags={tags}";
/** @var Threaded */
private Threaded $mainToThreadBuffer;
/** @var string */
private string $tagsEncoded;
/** @var string */
private string $threadExclusion;
/** @var string */
private string $logExclusion;
/** @var string */
private string $regexInclusion;
/** @var string */
private string $forceIgnore;
/**
* @param string $accessToken The access token to the logDNA server, this property is required.
* @param string $hostname The hostname of this server.
* @param array $tags The tags of the software itself, can be more than one to indicate specific type of server.
* @param array $threadExclusion The thread that will be responsible to excludes all given log exclusion. Other threads will be able to send the logs freely even with exclusion.
* @param array $logExcludes The log level that will be excluded from being logged into logDNA.
* @param array $forceIgnore Force ignores any lines within this level.
* @param array $regexIncludes The regular expression for a log pattern that will be included even the log is being excluded.
* @param string $environment The environment of the server software.
*/
public function __construct(
private string $composerPath,
private string $accessToken,
private string $hostname,
array $tags,
array $threadExclusion = ["Server thread"],
array $logExcludes = [LogLevel::INFO, LogLevel::NOTICE, LogLevel::WARNING, LogLevel::ERROR],
array $forceIgnore = [LogLevel::NOTICE, LogLevel::WARNING, LogLevel::ERROR],
array $regexIncludes = ["#^\[NetworkSession: ((?!localhost 19132).)*] Player#i", "#^\[NetworkSession:((?!localhost 19132).)*] Session closed#i", "/Graceful shutdown complete/i"],
private string $environment = 'production'
)
{
$this->mainToThreadBuffer = new Threaded;
$this->tagsEncoded = implode(",", $tags);
$this->threadExclusion = implode(",", $threadExclusion);
$this->logExclusion = implode(",", $logExcludes);
$this->regexInclusion = implode(",", $regexIncludes);
$this->forceIgnore = implode(",", $forceIgnore);
LogInstance::set($this);
Server::getInstance()->getAsyncPool()->addWorkerStartHook(function (int $worker): void {
Server::getInstance()->getAsyncPool()->submitTaskToWorker(new LogRegisterTask(), $worker);
});
$this->start(PTHREADS_INHERIT_INI | PTHREADS_INHERIT_CONSTANTS);
}
protected function onRun(): void
{
if (!empty($this->composerPath)) {
require $this->composerPath;
}
LogInstance::set($this);
$this->ingestLog("Starting LogDNA logger utility.");
$logExcludes = explode(',', $this->logExclusion);
$regexIncludes = explode(',', $this->regexInclusion);
$threadExclusion = explode(',', $this->threadExclusion);
$forceIgnore = explode(",", $this->forceIgnore);
$pending = null;
while (!$this->isKilled) {
$start = microtime(true);
$this->tickProcessor($pending, $logExcludes, $regexIncludes, $threadExclusion, $forceIgnore);
$time = microtime(true) - $start;
if ($time < self::PUBLISHING_DELAY && $pending === null) {
$sleepUntil = (int)((self::PUBLISHING_DELAY - $time) * 1000000);
$this->synchronized(function () use ($sleepUntil): void {
$this->wait($sleepUntil);
});
}
}
$this->ingestLog("Shutting down LogDNA logger utility.");
$this->tickProcessor($pending, $logExcludes, $regexIncludes, $threadExclusion, $forceIgnore);
}
private function tickProcessor(?array &$pending, array $logExcludes, array $regexIncludes, array $threadExclusion, array $forceIgnore): void
{
if ($pending === null) {
$payload = $this->mainToThreadBuffer->synchronized(function (): array {
$results = [];
while (($buffer = $this->mainToThreadBuffer->shift()) !== null) {
$message = igbinary_unserialize($buffer);
$digest = [];
$digest['line'] = $message[0];
$digest['level'] = strtoupper($message[1]);
$digest['app'] = $message[2];
$digest['timestamp'] = $message[3];
$digest['env'] = $this->environment;
$results[] = $digest;
}
return $results;
});
} else {
$payload = $pending;
}
foreach ($payload as $id => ['level' => $level, 'line' => $line, 'app' => $threadName]) {
if (in_array(strtolower($level), $logExcludes, true) && in_array($threadName, $threadExclusion)) {
$regexMatches = false;
foreach ($regexIncludes as $regex) {
$result = preg_match($regex, $line);
if ($regexMatches = (!is_bool($result) && $result > 0)) {
break;
}
}
if (!$regexMatches) {
unset($payload[$id]);
}
}
if (in_array(strtolower($level), $forceIgnore, true)) {
unset($payload[$id]);
}
}
// Try to reindex the array values, the keys should be in the wrong
// order because of the previous filtering and such.
if (empty($payload = array_values($payload))) {
return;
}
$ingestUrl = str_replace(["{host}", "{tags}"], [$this->hostname, $this->tagsEncoded, time()], self::PUBLISHER_URL);
try {
$chunks = array_chunk($payload, self::PUBLISHING_MAX_LINES);
$lines = array_shift($chunks);
$pending = array_merge(...$chunks);
$jsonPayload = json_encode(['lines' => $lines]);
$v = Internet::simpleCurl($ingestUrl, 10, [
"User-Agent: NetherGamesMC/libLogDNA",
'Content-Type: application/json'
], [
CURLOPT_POST => 1,
CURLOPT_USERPWD => $this->accessToken . ':',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_POSTFIELDS => $jsonPayload
]);
if ($v->getCode() === 200 && empty($pending)) {
$pending = null;
}
} catch (InternetException) {
$pending = $payload;
}
}
/**
* LogDNA ingestion function, utility function to communicate across threads to pass log messages.
* Can be used in any threads as long the object holds this initialization correctly.
*
* @param string $message
* @param string $level
*/
public function ingestLog(string $message, string $level = LogLevel::INFO): void
{
$thread = Thread::getCurrentThread();
if ($thread === null) {
$threadName = "Server thread";
} elseif ($thread instanceof Thread or $thread instanceof Worker) {
$threadName = $thread->getThreadName() . " thread";
} else {
$threadName = (new ReflectionClass($thread))->getShortName() . " thread";
}
$this->mainToThreadBuffer->synchronized(function () use ($message, $level, $threadName) {
$this->mainToThreadBuffer[] = igbinary_serialize([$message, $level, $threadName, time()]);
});
}
}