forked from NetherGamesMC/libLokiLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLokiLoggerAttachment.php
34 lines (28 loc) · 1002 Bytes
/
LokiLoggerAttachment.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
<?php
namespace libLokiLogger;
use pocketmine\thread\Thread;
use pocketmine\thread\Worker;
use pocketmine\utils\TextFormat;
use ReflectionClass;
use ThreadedLoggerAttachment;
class LokiLoggerAttachment extends ThreadedLoggerAttachment
{
/** @var LokiLoggerThread */
private LokiLoggerThread $lokiInstance;
public function __construct()
{
$this->lokiInstance = LokiLoggerThread::getInstance();
}
public function log($level, $message)
{
$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->lokiInstance->write(preg_filter('/^\[(.*?)] /', '', TextFormat::clean($message)), ['level' => $level, 'thread' => $threadName]);
}
}