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
- Host: GitHub
- URL: https://github.com/thecraftman/php-logs
- Owner: thecraftman
- Created: 2024-01-18T12:41:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-18T12:50:50.000Z (over 1 year ago)
- Last Synced: 2025-03-21T14:11:29.374Z (7 months ago)
- Language: PHP
- Size: 72.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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`
```