An open API service indexing awesome lists of open source software.

https://github.com/thecraftman/php-logs

send PHP logs to Axiom
https://github.com/thecraftman/php-logs

Last synced: 6 months ago
JSON representation

send PHP logs to Axiom

Awesome Lists containing this project

README

          

## Directory path

- `app/http/testcontroller.php`

```php
info('Testing Axiom logger!');
return 'Log sent to Axiom';
}
}
```

- `app/logging/AxiomHandler`

```
apiToken = $apiToken;
$this->dataset = $dataset;
}

private function initializeCurl(): \CurlHandle
{
$endpoint = "https://api.axiom.co/v1/datasets/{$this->dataset}/ingest";
$ch = curl_init($endpoint);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->apiToken,
'Content-Type: application/json',
]);

return $ch;
}

protected function write(LogRecord $record): void
{
$ch = $this->initializeCurl();

$data = [
'message' => $record->message,
'context' => $record->context,
'level' => $record->level->getName(),
'channel' => $record->channel,
'extra' => $record->extra,
];

$payload = json_encode([$data]);

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_exec($ch);
if (curl_errno($ch)) {
// Optionally log the curl error to PHP error log
error_log('Curl error: ' . curl_error($ch));
}

curl_close($ch);
}

protected function getDefaultFormatter(): FormatterInterface
{
return new \Monolog\Formatter\JsonFormatter();
}
}
```

- `config/logging.php`

```php
'axiom' => [
'driver' => 'monolog',
'handler' => App\Logging\AxiomHandler::class,
'level' => env('LOG_LEVEL', 'debug'),
'with' => [
'apiToken' => env('AXIOM_API_TOKEN'),
'dataset' => env('AXIOM_DATASET'),
],
],
```

- `routes/web.php`

```