LogDNAHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A setHttpClient() 0 4 1
A write() 0 19 1
A getDefaultFormatter() 0 4 1
1
<?php declare(strict_types = 1);
2
namespace Lyal\MonologLogDNA;
3
4
use Carbon\Carbon;
5
use GuzzleHttp\Client;
6
use Monolog\Formatter\FormatterInterface;
7
use Monolog\Handler\AbstractProcessingHandler;
8
use Monolog\Logger;
9
10
class LogDNAHandler extends AbstractProcessingHandler
11
{
12
    private $carbon;
13
14
    protected $key;
15
    protected $hostname;
16
    protected $mac;
17
    protected $ip;
18
19
    protected $httpClient;
20
21
    protected $api = 'https://logs.logdna.com/logs/ingest';
22
23
24
    /**
25
     * LogDNAHandler constructor.
26
     * @param null $key
27
     * @param null $host
28
     * @param null $mac
29
     * @param null $ip
30
     * @param null $httpClient
31
     * @param int $level
32
     * @param bool $bubble
33
     */
34
35
36
    public function __construct(
37
        $key = null,
38
        $host = null,
39
        $mac = null,
40
        $ip = null,
41
        $httpClient = null,
42
        $level = Logger::DEBUG,
43
        $bubble = true
44
    )
45
    {
46
47
48
        parent::__construct($level, $bubble);
49
        $this->key = $key ?? getenv('LOGDNA_INGESTION_KEY');
50
        $this->carbon = new Carbon;
51
        $this->hostname = $host ?? getenv('LOGDNA_HOSTNAME') ?: gethostname();
52
        $this->mac = $mac;
53
        $this->ip = $ip ?? getenv('LOGDNA_HOST_IP') ?: gethostbyname(gethostname());
54
        $this->setHttpClient($httpClient ?? new Client());
55
        $this->api = getenv('LOGDNA_API_URL') ?: $this->api;
56
    }
57
58
    /**
59
     * @param Client $client
60
     */
61
62
    protected function setHttpClient(Client $client)
63
    {
64
        $this->httpClient = $client;
65
    }
66
67
    /**
68
     * Attempts to write a log item once to the LogDNA api service
69
     *
70
     * It's possible that we should try this 5 times as other libraries do
71
     *
72
     * @param array $record
73
     */
74
75
    protected function write(array $record)
76
    {
77
        $this->httpClient->post($this->api, [
78
            'headers' => [
79
                'Content-Type' => 'application/json',
80
81
            ],
82
            'query' => [
83
                'hostname' => $this->hostname,
84
                'mac' => $this->mac,
85
                'ip' => $this->ip,
86
                'now' => $this->carbon->getTimestamp()
87
            ],
88
            'auth' => [$this->key, ''],
89
            'body' => $record['formatted']
90
        ]);
91
92
93
    }
94
95
    /**
96
     * Set the default formatter to our own
97
     *
98
     * @return FormatterInterface
99
     */
100
101
    protected function getDefaultFormatter(): FormatterInterface
102
    {
103
        return new LogDNAFormatter();
104
    }
105
}
106