Passed
Push — master ( afa595...3d30f2 )
by Marcel
08:58
created

DatabaseHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A write() 0 19 2
1
<?php
2
3
namespace App\Monolog;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Exception;
7
use Doctrine\DBAL\Types\Types;
8
use Monolog\Handler\AbstractProcessingHandler;
9
use Monolog\Logger;
10
11
class DatabaseHandler extends AbstractProcessingHandler {
12
13
    public function __construct(private Connection $connection, int $level = Logger::INFO) {
14
        parent::__construct($level, false);
15
    }
16
17
    /**
18
     * @inheritDoc
19
     */
20
    protected function write(array $record): void {
21
        $entry = [
22
            'channel' => $record['channel'],
23
            'level' => $record['level'],
24
            'message' => $record['formatted'],
25
            'time' => $record['datetime'],
26
            'details' => json_encode($record['extra'], JSON_PRETTY_PRINT)
27
        ];
28
29
        try {
30
            $this->connection
31
                ->insert('log', $entry, [
32
                    Types::STRING,
33
                    Types::INTEGER,
34
                    Types::TEXT,
35
                    Types::DATETIME_MUTABLE,
36
                    Types::STRING
37
                ]);
38
        } catch (Exception) {
39
            // Logging failed :-/
40
        }
41
    }
42
}