Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

Json   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 36
c 0
b 0
f 0
ccs 11
cts 18
cp 0.6111
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 15 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Logger\Formatter;
15
16
use Exception;
17
use Phalcon\Helper\Json as JsonHelper;
18
use Phalcon\Logger\Item;
19
20
/**
21
 * Phalcon\Logger\Formatter\Json
22
 *
23
 * Formats messages using JSON encoding
24
 */
25
class Json extends AbstractFormatter
26
{
27
    /**
28
     * Json constructor.
29
     *
30
     * @param string $dateFormat
31
     */
32 7
    public function __construct(string $dateFormat = "c")
33
    {
34 7
        $this->dateFormat = $dateFormat;
35 7
    }
36
37
    /**
38
     * Applies a format to a message before sent it to the internal log
39
     *
40
     * @param Item $item
41
     *
42
     * @return string
43
     * @throws Exception
44
     */
45 4
    public function format(Item $item): string
46
    {
47 4
        $message = $this->interpolate(
48 4
            $item->getMessage(),
49 4
            $item->getContext()
50
        );
51
52 4
        return JsonHelper::encode(
53
            [
54 4
                "type"      => $item->getName(),
55 4
                "message"   => $message,
56 4
                "timestamp" => $this->getFormattedDate(),
57
            ]
58
        );
59
    }
60
}
61