Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/vendor/
|
38
README.md
Normal file
38
README.md
Normal file
@ -0,0 +1,38 @@
|
||||
# Monolog handler for LogDB
|
||||
|
||||
This is a handler to allow Monolog to be used with LogDB.
|
||||
|
||||
## Using
|
||||
|
||||
### Symfony 5+
|
||||
|
||||
```yaml
|
||||
# config/services.yaml
|
||||
services:
|
||||
LogDb\Monolog\LogDbHandler:
|
||||
arguments:
|
||||
$serverUrl: "http://127.0.0.1:8000"
|
||||
$scope: "org.myplatform.myapp.web"
|
||||
$source: "My App"
|
||||
```
|
||||
|
||||
```yaml
|
||||
# config/packages/prod/monolog.yaml
|
||||
monolog:
|
||||
handlers:
|
||||
logdb:
|
||||
type: service
|
||||
id: LogDb\Monolog\LogDbHandler
|
||||
```
|
||||
|
||||
```yaml
|
||||
# config/packages/prod/monolog.yaml
|
||||
monolog:
|
||||
handlers:
|
||||
main:
|
||||
type: fingers_crossed
|
||||
handler: logdb
|
||||
logdb:
|
||||
type: service
|
||||
id: LogDb\Monolog\LogDbHandler
|
||||
```
|
21
composer.json
Normal file
21
composer.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "logdb/monolog-handler",
|
||||
"description": "Monolog handler for LogDB",
|
||||
"type": "library",
|
||||
"license": "GPL-2.0-or-later",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"LogDb\\Monolog\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Christopher Vagnetoft",
|
||||
"email": "labs@noccy.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"monolog/monolog": "^3.9",
|
||||
"symfony/http-client": "^7.3"
|
||||
}
|
||||
}
|
75
src/LogDbHandler.php
Normal file
75
src/LogDbHandler.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace LogDb\Monolog;
|
||||
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\LogRecord;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class LogDbHandler extends AbstractProcessingHandler
|
||||
{
|
||||
private HttpClientInterface $client;
|
||||
|
||||
private bool $batching = false;
|
||||
|
||||
private array $batched = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly string $serverUrl,
|
||||
private readonly ?string $scope = null,
|
||||
private readonly ?string $source = null,
|
||||
)
|
||||
{
|
||||
$this->client = HttpClient::createForBaseUri($this->serverUrl);
|
||||
}
|
||||
|
||||
public function handleBatch(array $records): void
|
||||
{
|
||||
$this->batching = true;
|
||||
parent::handleBatch($records);
|
||||
$this->batching = false;
|
||||
$this->commit();
|
||||
}
|
||||
|
||||
protected function write(LogRecord $record): void
|
||||
{
|
||||
$event = [
|
||||
'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) {
|
||||
$this->batched[] = $record;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
||||
'json' => $event
|
||||
]);
|
||||
$response->getContent(true);
|
||||
} catch (\Exception $e) {
|
||||
// Silent fault.
|
||||
}
|
||||
}
|
||||
|
||||
private function commit(): void
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('POST', '/api/logdb/v1/create-event', [
|
||||
'json' => $this->batched
|
||||
]);
|
||||
$response->getContent(true);
|
||||
} finally {
|
||||
$this->batched = [];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user