|
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 Phalcon\Logger\Item; |
|
15
|
|
|
|
|
16
|
|
|
use function date; |
|
17
|
|
|
use function is_array; |
|
18
|
|
|
use function str_replace; |
|
19
|
|
|
use function strpos; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Phalcon\Logger\Formatter\Line |
|
23
|
|
|
* |
|
24
|
|
|
* Formats messages using an one-line string |
|
25
|
|
|
* |
|
26
|
|
|
* @property string $dateFormat |
|
27
|
|
|
* @property string $format |
|
28
|
|
|
*/ |
|
29
|
|
|
class Line extends AbstractFormatter |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Default date format |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $dateFormat; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Format applied to each message |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $format; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Line constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $format |
|
49
|
|
|
* @param string $dateFormat |
|
50
|
|
|
*/ |
|
51
|
36 |
|
public function __construct( |
|
52
|
|
|
string $format = "[%date%][%type%] %message%", |
|
53
|
|
|
string $dateFormat = "c" |
|
54
|
|
|
) { |
|
55
|
36 |
|
$this->format = $format; |
|
56
|
36 |
|
$this->dateFormat = $dateFormat; |
|
57
|
36 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getDateFormat(): string |
|
63
|
|
|
{ |
|
64
|
3 |
|
return $this->dateFormat; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function getFormat(): string |
|
71
|
|
|
{ |
|
72
|
2 |
|
return $this->format; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Applies a format to a message before sent it to the internal log |
|
77
|
|
|
* |
|
78
|
|
|
* @param Item $item |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
23 |
|
public function format(Item $item): string |
|
83
|
|
|
{ |
|
84
|
23 |
|
$format = $this->format; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check if the format has the %date% placeholder |
|
88
|
|
|
*/ |
|
89
|
23 |
|
if (false !== strpos($format, "%date%")) { |
|
90
|
23 |
|
$format = str_replace( |
|
91
|
23 |
|
"%date%", |
|
92
|
23 |
|
$this->getFormattedDate(), |
|
93
|
23 |
|
$format |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if the format has the %type% placeholder |
|
99
|
|
|
*/ |
|
100
|
23 |
|
if (false !== strpos($format, "%type%")) { |
|
101
|
23 |
|
$format = str_replace("%type%", $item->getName(), $format); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
23 |
|
$format = str_replace("%message%", $item->getMessage(), $format); |
|
105
|
|
|
|
|
106
|
23 |
|
if (is_array($item->getContext())) { |
|
|
|
|
|
|
107
|
23 |
|
return $this->interpolate( |
|
108
|
23 |
|
$format, |
|
109
|
23 |
|
$item->getContext() |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $format; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $dateFormat |
|
118
|
|
|
* |
|
119
|
|
|
* @return Line |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function setDateFormat(string $dateFormat): Line |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->dateFormat = $dateFormat; |
|
124
|
|
|
|
|
125
|
1 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $format |
|
130
|
|
|
* |
|
131
|
|
|
* @return Line |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function setFormat(string $format): Line |
|
134
|
|
|
{ |
|
135
|
1 |
|
$this->format = $format; |
|
136
|
|
|
|
|
137
|
1 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|