Use logdb/client, add support for UDP
This commit is contained in:
@@ -16,6 +16,12 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"monolog/monolog": "^3.9",
|
"monolog/monolog": "^3.9",
|
||||||
"symfony/http-client": "^7.3"
|
"logdb/client": "^0.1.0"
|
||||||
|
},
|
||||||
|
"repositories": {
|
||||||
|
"logdb": {
|
||||||
|
"type": "composer",
|
||||||
|
"url": "https://dev.noccylabs.info/api/packages/logdb/composer"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace LogDb\Monolog;
|
namespace LogDb\Monolog;
|
||||||
|
|
||||||
|
use LogDb\Client\LogDbClient;
|
||||||
|
use LogDb\Client\LogEvent;
|
||||||
use Monolog\Handler\AbstractProcessingHandler;
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
use Monolog\Handler\HandlerInterface;
|
use Monolog\Handler\HandlerInterface;
|
||||||
use Monolog\Level;
|
use Monolog\Level;
|
||||||
@@ -11,12 +13,14 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|||||||
|
|
||||||
class LogDbHandler extends AbstractProcessingHandler
|
class LogDbHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
private HttpClientInterface $client;
|
//private HttpClientInterface $client;
|
||||||
|
|
||||||
private bool $batching = false;
|
private bool $batching = false;
|
||||||
|
|
||||||
private array $batched = [];
|
private array $batched = [];
|
||||||
|
|
||||||
|
private LogDbClient $client;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly string $serverUrl,
|
private readonly string $serverUrl,
|
||||||
private readonly ?string $scope = null,
|
private readonly ?string $scope = null,
|
||||||
@@ -26,7 +30,24 @@ class LogDbHandler extends AbstractProcessingHandler
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct($level, $bubble);
|
parent::__construct($level, $bubble);
|
||||||
$this->client = HttpClient::createForBaseUri($this->serverUrl);
|
//$this->client = HttpClient::createForBaseUri($this->serverUrl);
|
||||||
|
|
||||||
|
if (!str_contains($serverUrl, "://")) {
|
||||||
|
$proto = 'http';
|
||||||
|
$server = $serverUrl;
|
||||||
|
} else {
|
||||||
|
[$proto,$server] = explode("://", $serverUrl, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($proto) {
|
||||||
|
case 'http':
|
||||||
|
case 'https':
|
||||||
|
$this->client = new LogDbClient("{$proto}://{$server}", "http");
|
||||||
|
break;
|
||||||
|
case 'udp':
|
||||||
|
$this->client = new LogDbClient($server, "udp");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleBatch(array $records): void
|
public function handleBatch(array $records): void
|
||||||
@@ -39,7 +60,7 @@ class LogDbHandler extends AbstractProcessingHandler
|
|||||||
|
|
||||||
protected function write(LogRecord $record): void
|
protected function write(LogRecord $record): void
|
||||||
{
|
{
|
||||||
$event = [
|
/*$event = [
|
||||||
'source' => $this->source,
|
'source' => $this->source,
|
||||||
'scope' => $this->scope,
|
'scope' => $this->scope,
|
||||||
'date' => $record->datetime->format('Y-m-d H:i:s P'),
|
'date' => $record->datetime->format('Y-m-d H:i:s P'),
|
||||||
@@ -47,7 +68,16 @@ class LogDbHandler extends AbstractProcessingHandler
|
|||||||
'brief' => $record->message,
|
'brief' => $record->message,
|
||||||
'detail' => $record->formatted,
|
'detail' => $record->formatted,
|
||||||
'context' => $record->context,
|
'context' => $record->context,
|
||||||
];
|
];*/
|
||||||
|
$event = new LogEvent(
|
||||||
|
source: $this->source,
|
||||||
|
scope: $this->scope,
|
||||||
|
date: $record->datetime, // ->format('Y-m-d H:i:s P'),
|
||||||
|
level: $record->level->toPsrLogLevel(),
|
||||||
|
brief: $record->message,
|
||||||
|
detail: $record->formatted,
|
||||||
|
context: $record->context,
|
||||||
|
);
|
||||||
|
|
||||||
if ($this->batching) {
|
if ($this->batching) {
|
||||||
$this->batched[] = $event;
|
$this->batched[] = $event;
|
||||||
@@ -55,10 +85,11 @@ class LogDbHandler extends AbstractProcessingHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
$this->client->logEvent($event);
|
||||||
'json' => $event
|
//$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
||||||
]);
|
// 'json' => $event
|
||||||
$response->getContent(true);
|
//]);
|
||||||
|
//$response->getContent(true);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Silent fault.
|
// Silent fault.
|
||||||
}
|
}
|
||||||
@@ -67,10 +98,11 @@ class LogDbHandler extends AbstractProcessingHandler
|
|||||||
private function commit(): void
|
private function commit(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
$this->client->logEventBatch($this->batched);
|
||||||
'json' => $this->batched
|
//$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
||||||
]);
|
// 'json' => $this->batched
|
||||||
$response->getContent(true);
|
//]);
|
||||||
|
//$response->getContent(true);
|
||||||
} finally {
|
} finally {
|
||||||
$this->batched = [];
|
$this->batched = [];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user