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

Json::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 10
cc 1
nc 1
nop 1
crap 1.0156
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