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\Adapter; |
15
|
|
|
|
16
|
|
|
use LogicException; |
17
|
|
|
use Phalcon\Helper\Arr; |
18
|
|
|
use Phalcon\Logger\Exception; |
19
|
|
|
use Phalcon\Logger\Item; |
20
|
|
|
|
21
|
|
|
use function fclose; |
22
|
|
|
use function fopen; |
23
|
|
|
use function fwrite; |
24
|
|
|
use function is_resource; |
25
|
|
|
use function sprintf; |
26
|
|
|
use function strpos; |
27
|
|
|
|
28
|
|
|
use const PHP_EOL; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Phalcon\Logger\Adapter\Stream |
32
|
|
|
* |
33
|
|
|
* Adapter to store logs in plain text files |
34
|
|
|
* |
35
|
|
|
*```php |
36
|
|
|
* $logger = new \Phalcon\Logger\Adapter\Stream("app/logs/test.log"); |
37
|
|
|
* |
38
|
|
|
* $logger->log("This is a message"); |
39
|
|
|
* $logger->log(\Phalcon\Logger::ERROR, "This is an error"); |
40
|
|
|
* $logger->error("This is another error"); |
41
|
|
|
* |
42
|
|
|
* $logger->close(); |
43
|
|
|
*``` |
44
|
|
|
* |
45
|
|
|
* @property resource|null $handler |
46
|
|
|
* @property string $mode |
47
|
|
|
* @property string $name |
48
|
|
|
* @property array $options |
49
|
|
|
*/ |
50
|
|
|
class Stream extends AbstractAdapter |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Stream handler resource |
54
|
|
|
* |
55
|
|
|
* @var resource|null |
56
|
|
|
*/ |
57
|
|
|
protected $handler = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The file open mode. Defaults to "ab" |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $mode = "ab"; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Stream name |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $name; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Path options |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $options; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Stream constructor. |
82
|
|
|
* |
83
|
|
|
* @param string $name |
84
|
|
|
* @param array $options |
85
|
|
|
* |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
72 |
|
public function __construct(string $name, array $options = []) |
89
|
|
|
{ |
90
|
72 |
|
$mode = Arr::get($options, "mode", "ab"); |
91
|
72 |
|
if (false !== strpos($mode, "r")) { |
92
|
2 |
|
throw new Exception("Adapter cannot be opened in read mode"); |
93
|
|
|
} |
94
|
|
|
|
95
|
70 |
|
$this->name = $name; |
96
|
70 |
|
$this->mode = $mode; |
97
|
70 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Closes the stream |
101
|
|
|
*/ |
102
|
54 |
|
public function close(): bool |
103
|
|
|
{ |
104
|
54 |
|
$result = true; |
105
|
|
|
|
106
|
54 |
|
if (is_resource($this->handler)) { |
107
|
34 |
|
$result = $this->fclose($this->handler); |
108
|
|
|
} |
109
|
|
|
|
110
|
54 |
|
$this->handler = null; |
111
|
|
|
|
112
|
54 |
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
public function getName(): string |
119
|
|
|
{ |
120
|
2 |
|
return $this->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Processes the message i.e. writes it to the file |
125
|
|
|
* |
126
|
|
|
* @param Item $item |
127
|
|
|
*/ |
128
|
36 |
|
public function process(Item $item): void |
129
|
|
|
{ |
130
|
36 |
|
if (!is_resource($this->handler)) { |
131
|
36 |
|
$handler = $this->fopen($this->name, $this->mode); |
132
|
|
|
|
133
|
36 |
|
if (!is_resource($handler)) { |
134
|
2 |
|
$this->handler = null; |
135
|
|
|
|
136
|
2 |
|
throw new LogicException( |
137
|
2 |
|
sprintf( |
138
|
2 |
|
"The file '%s' cannot be opened with mode '%s'", |
139
|
2 |
|
$this->name, |
140
|
2 |
|
$this->mode |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
34 |
|
$this->handler = $handler; |
146
|
|
|
} |
147
|
|
|
|
148
|
34 |
|
$formatter = $this->getFormatter(); |
149
|
34 |
|
$formattedMessage = (string) $formatter->format($item) . PHP_EOL; |
150
|
|
|
|
151
|
34 |
|
$this->fwrite($this->handler, $formattedMessage); |
152
|
34 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Closes an open file pointer |
156
|
|
|
* |
157
|
|
|
* @link https://php.net/manual/en/function.fclose.php |
158
|
|
|
* |
159
|
|
|
* @param resource $handle |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
34 |
|
public function fclose($handle) |
164
|
|
|
{ |
165
|
34 |
|
return fclose($handle); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Opens file or URL |
170
|
|
|
* |
171
|
|
|
* @link https://php.net/manual/en/function.fopen.php |
172
|
|
|
* |
173
|
|
|
* @param string $filename |
174
|
|
|
* @param string $mode |
175
|
|
|
* |
176
|
|
|
* @return false|resource |
177
|
|
|
*/ |
178
|
34 |
|
protected function fopen($filename, $mode) |
179
|
|
|
{ |
180
|
34 |
|
return fopen($filename, $mode); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Binary-safe file write |
185
|
|
|
* |
186
|
|
|
* @link https://php.net/manual/en/function.fwrite.php |
187
|
|
|
* |
188
|
|
|
* @param resource $handle |
189
|
|
|
* @param string $string |
190
|
|
|
* |
191
|
|
|
* @return int|false |
192
|
|
|
*/ |
193
|
34 |
|
protected function fwrite($handle, $string) |
194
|
|
|
{ |
195
|
34 |
|
return fwrite($handle, $string); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|