TelegrafHandler::getDefaultFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Monolog\Handler;
6
7
use InfluxDB\Driver\UDP;
8
use Monolog\Formatter\FormatterInterface;
9
use Monolog\Handler\AbstractProcessingHandler;
10
use Monolog\Logger;
11
use Umbrellio\TableSync\Monolog\Formatter\InfluxDBFormatter;
12
13
class TelegrafHandler extends AbstractProcessingHandler
14
{
15
    private $measurement;
16
    private $socket;
17
18
    public function __construct($host, $port, ?string $measurement, $level = Logger::INFO, $bubble = true)
19
    {
20
        parent::__construct($level, $bubble);
21
22
        $this->measurement = $measurement ?? InfluxDBHandler::POINT_MEASUREMENT;
23
        $this->socket = new UDP($host, $port);
24
    }
25
26
    protected function write(array $record): void
27
    {
28
        foreach ($record['formatted'] as $record) {
29
            $this->socket->write((string) $record);
30
        }
31
    }
32
33
    protected function getDefaultFormatter(): FormatterInterface
34
    {
35
        return new InfluxDBFormatter($this->measurement);
36
    }
37
}
38