DbFormatter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 1
nop 4
crap 2
1
<?php declare(strict_types=1);
2
/**
3
 * Starlit Db Logging.
4
 *
5
 * @copyright Copyright (c) 2019 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\Db\Logging;
10
11
use Monolog\Formatter\LineFormatter;
12
13
/**
14
 * @author Andreas Nilsson <http://github.com/jandreasn>
15
 */
16
class DbFormatter extends LineFormatter
17
{
18
    public const FORMAT_DEFAULT = "%message% %context% %extra%";
19
20
    public const MAX_LENGTH_DEFAULT = 65535;
21
22
    /**
23
     * @var int
24
     */
25
    protected $maxLength;
26
27 7
    public function __construct(
28
        string $format = null,
29
        bool $allowInlineLineBreaks = true,
30
        bool $ignoreEmptyContextAndExtra = true,
31
        int $maxLength = self::MAX_LENGTH_DEFAULT
32
    ) {
33 7
        parent::__construct(
34 7
            $format ?: static::FORMAT_DEFAULT,
35 7
            null,
36 7
            $allowInlineLineBreaks,
37 7
            $ignoreEmptyContextAndExtra
38
        );
39
40 7
        $this->maxLength = $maxLength;
41 7
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 7
    public function format(array $record)
47
    {
48 7
        $output = trim(parent::format($record));
49
50 7
        if ($this->maxLength !== null && strlen($output) > $this->maxLength) {
51 1
            $output = substr($output, 0, $this->maxLength - 3) . '...';
52
        }
53
54 7
        return $output;
55
    }
56
}
57