Last active
May 16, 2023 06:54
-
-
Save mjkatgithub/292b8765ac183fab7074d350dde8fa4c to your computer and use it in GitHub Desktop.
monologInfluxDBHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use InfluxDB2\Client; | |
use InfluxDB2\Model\WritePrecision; | |
use Monolog\Handler\AbstractProcessingHandler; | |
use Monolog\Logger; | |
class InfluxDBHandler extends AbstractProcessingHandler | |
{ | |
private $client; | |
private $bucket; | |
private $org; | |
private \InfluxDB2\WriteApi $writeApi; | |
public function __construct($dsn, $bucket, $org, $token, $level = Logger::DEBUG, $bubble = true) | |
{ | |
parent::__construct($level, $bubble); | |
$this->client = new Client([ | |
'url' => $dsn, | |
"token" => $token, | |
]); | |
$this->writeApi = $this->client->createWriteApi(); | |
$this->bucket = $bucket; | |
$this->org = $org; | |
} | |
protected function write($record): void | |
{ | |
$record = (array) $record; | |
$level = (array) $record['level']; | |
$dataArray = [ | |
'name' => 'monolog', | |
'tags' => [ | |
'level' => strtolower($level['name']), | |
'channel' => $record['channel'] | |
], | |
'fields' => [ | |
'message' => $record['message'], | |
'context' => json_encode($record['context']), | |
'extra' => json_encode($record['extra']) | |
], | |
'time' => (float)$record['datetime']->format('Uu') / 1000000 | |
]; | |
$this->writeApi->write($dataArray, WritePrecision::S, $this->bucket, $this->org); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment