Passed
Push — master ( 2afdeb...b846a2 )
by Nikolaos
06:23 queued 03:51
created

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 61
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

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