1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Utility\LoggerUtils; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
use WebStream\Exception\Extend\LoggerException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Loggerクラス |
10
|
|
|
* @author Ryuichi Tanaka |
11
|
|
|
* @since 2012/01/16 |
12
|
|
|
* @version 0.7 |
13
|
|
|
*/ |
14
|
|
|
class Logger |
15
|
|
|
{ |
16
|
|
|
use LoggerUtils; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Logger ロガー |
20
|
|
|
*/ |
21
|
|
|
private static $logger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoggerFormatter ロガーフォーマッタ |
25
|
|
|
*/ |
26
|
|
|
private static $formatter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Container ログ設定コンテナ |
30
|
|
|
*/ |
31
|
|
|
private static $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Container ログ設定コンテナ |
35
|
|
|
*/ |
36
|
|
|
private $logConfig; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array<IOutputter> Outputterリスト |
40
|
|
|
*/ |
41
|
|
|
private $outputters; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* コンストラクタ |
45
|
|
|
* @param Container ログ設定コンテナ |
46
|
|
|
*/ |
47
|
|
|
private function __construct(Container $logConfig) |
48
|
|
|
{ |
49
|
|
|
$this->logConfig = $logConfig; |
50
|
|
|
$this->outputters = []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* ログ設定を返却する |
55
|
|
|
* @return Container ログ設定 |
56
|
|
|
*/ |
57
|
|
|
public function getConfig() |
58
|
|
|
{ |
59
|
|
|
return $this->logConfig; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* デストラクタ |
64
|
|
|
*/ |
65
|
|
|
public function __destruct() |
66
|
|
|
{ |
67
|
|
|
$this->directWrite(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* 遅延書き出しを有効にする |
72
|
|
|
*/ |
73
|
|
|
public static function enableLazyWrite() |
74
|
|
|
{ |
75
|
|
|
self::$logger->lazyWrite(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 即時書き出しを有効にする |
80
|
|
|
*/ |
81
|
|
|
public static function enableDirectWrite() |
82
|
|
|
{ |
83
|
|
|
self::$logger->directWrite(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* インスタンスを返却する |
88
|
|
|
* @return WebStream\Module\Logger ロガーインスタンス |
89
|
|
|
*/ |
90
|
|
|
public static function getInstance() |
91
|
|
|
{ |
92
|
|
|
return self::$logger; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Loggerを初期化する |
97
|
|
|
* @param Container ログ設定コンテナ |
98
|
|
|
*/ |
99
|
|
|
public static function init(Container $config) |
100
|
|
|
{ |
101
|
|
|
self::$config = $config; |
102
|
|
|
self::$logger = new Logger($config); |
103
|
|
|
self::$formatter = new LoggerFormatter($config); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Loggerを終了する |
108
|
|
|
*/ |
109
|
|
|
public static function finalize() |
110
|
|
|
{ |
111
|
|
|
self::$config = null; |
112
|
|
|
self::$logger = null; |
113
|
|
|
self::$formatter = null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Loggerが初期化済みかどうかチェックする |
118
|
|
|
* @param bool 初期化済みならtrue |
119
|
|
|
*/ |
120
|
|
|
public static function isInitialized() |
121
|
|
|
{ |
122
|
|
|
return self::$logger !== null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Loggerメソッドの呼び出しを受ける |
127
|
|
|
* @param string メソッド名(ログレベル文字列) |
128
|
|
|
* @param array 引数 |
129
|
|
|
*/ |
130
|
|
|
public static function __callStatic($level, $arguments) |
131
|
|
|
{ |
132
|
|
|
if (self::$logger === null || self::$formatter === null) { |
133
|
|
|
if (self::$config !== null) { |
134
|
|
|
self::init(self::$config); |
135
|
|
|
} else { |
136
|
|
|
throw new LoggerException("Logger is not initialized."); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
call_user_func_array([self::$logger, "write"], array_merge([$level], $arguments)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Outputterを設定する |
145
|
|
|
* @param array<IOutputter> $outputters Outputterリスト |
146
|
|
|
*/ |
147
|
|
|
public function setOutputter(array $outputters) |
148
|
|
|
{ |
149
|
|
|
foreach ($outputters as $outputter) { |
150
|
|
|
if (!$outputter instanceof \WebStream\Log\Outputter\IOutputter) { |
151
|
|
|
throw new LoggerException("Log outputter must implement WebStream\Log\Outputter\IOutputter."); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
$this->outputters = $outputters; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* タイムスタンプを取得する |
159
|
|
|
* @return string タイムスタンプ |
160
|
|
|
*/ |
161
|
|
|
private function getTimeStamp() |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
date_default_timezone_set('Asia/Tokyo'); |
164
|
|
|
$msec = sprintf("%2d", floatval(microtime()) * 100); |
165
|
|
|
|
166
|
|
|
return strftime("%Y-%m-%d %H:%M:%S") . "," . $msec; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* ログを書き出す |
171
|
|
|
* @param string ログレベル文字列 |
172
|
|
|
* @param string 出力文字列 |
173
|
|
|
* @param array<mixed> 埋め込み値リスト |
174
|
|
|
*/ |
175
|
|
|
public function write($level, $msg, $context = null) |
176
|
|
|
{ |
177
|
|
|
if ($this->logConfig->logLevel > $this->toLogLevelValue($level)) { |
|
|
|
|
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (is_array($context) && count($context) > 0) { |
182
|
|
|
// sprintfと同様の展開 |
183
|
|
|
// [a-zA-Z0-9_-\.] 以外もキーには指定可能だが仕様としてこれ以外は不可とする |
184
|
|
|
preg_match_all('/\{\s*([a-zA-Z0-9._-]+)\s*?\}/', $msg, $matches); |
185
|
|
|
foreach ($matches[1] as $index => $value) { |
186
|
|
|
if (array_key_exists($value, $context)) { |
187
|
|
|
$matches[1][$index] = $context[$value]; |
188
|
|
|
} else { |
189
|
|
|
unset($matches[0][$index]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
$msg = str_replace($matches[0], $matches[1], $msg); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->rotate(); |
196
|
|
|
try { |
197
|
|
|
if (count($this->outputters) > 0) { |
198
|
|
|
foreach ($this->outputters as $outputter) { |
199
|
|
|
$outputter->write(self::$formatter->getFormattedMessage($msg, $level)); |
200
|
|
|
} |
201
|
|
|
} else { |
202
|
|
|
error_log(self::$formatter->getFormattedMessage($msg, $level), 3, $this->logConfig->logPath); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
} catch (LoggerException $e) { |
205
|
|
|
throw $e; |
206
|
|
|
} catch (\Exception $e) { |
207
|
|
|
throw new LoggerException($e); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* ログステータスファイルに書きこむ |
213
|
|
|
*/ |
214
|
|
|
private function writeStatus() |
215
|
|
|
{ |
216
|
|
|
file_put_contents($this->logConfig->statusPath, intval(preg_replace('/^.*\s/', '', microtime()))); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* ログステータスファイルを読み込む |
221
|
|
|
* @return int UnixTime |
222
|
|
|
*/ |
223
|
|
|
private function readStatus() |
224
|
|
|
{ |
225
|
|
|
$handle = fopen($this->logConfig->statusPath, "r"); |
|
|
|
|
226
|
|
|
$size = filesize($this->logConfig->statusPath); |
|
|
|
|
227
|
|
|
$content = fread($handle, $size); |
228
|
|
|
fclose($handle); |
229
|
|
|
if (!preg_match('/^\d{10}$/', $content)) { |
230
|
|
|
throw new LoggerException("Invalid log state file contents: " . $content); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return intval($content); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* ステータスファイルを作成する |
238
|
|
|
*/ |
239
|
|
|
private function createstatusPath() |
240
|
|
|
{ |
241
|
|
|
// ステータスファイルがない場合は書きだす |
242
|
|
|
if (!is_file($this->logConfig->statusPath)) { |
|
|
|
|
243
|
|
|
$this->writeStatus(); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* ログファイルをアーカイブする |
249
|
|
|
* stream.log -> stream.(作成日時)-(現在日時).log |
250
|
|
|
* @param string ログファイルパス |
251
|
|
|
*/ |
252
|
|
|
private function rotate() |
253
|
|
|
{ |
254
|
|
|
// ログファイルがない場合はローテートしない |
255
|
|
|
if (!realpath($this->logConfig->logPath)) { |
|
|
|
|
256
|
|
|
return; |
257
|
|
|
} |
258
|
|
|
// ログローテート実行 |
259
|
|
|
if ($this->logConfig->rotateCycle !== null) { |
|
|
|
|
260
|
|
|
$this->rotateByCycle(); |
261
|
|
|
} elseif ($this->logConfig->rotateSize !== null) { |
|
|
|
|
262
|
|
|
$this->rotateBySize(); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* ローテートを実行する |
268
|
|
|
* @param integer 作成日時のUnixTime |
269
|
|
|
* @param integer 現在日時のUnixTime |
270
|
|
|
*/ |
271
|
|
|
private function runRotate($from, $to) |
272
|
|
|
{ |
273
|
|
|
$from_date = date("YmdHis", $from); |
274
|
|
|
$to_date = date("YmdHis", $to); |
275
|
|
|
$archive_path = null; |
|
|
|
|
276
|
|
|
if (preg_match('/(.*)\.(.+)/', $this->logConfig->logPath, $matches)) { |
|
|
|
|
277
|
|
|
$archive_path = "$matches[1].${from_date}-${to_date}.$matches[2]"; |
278
|
|
|
// mvを実行 |
279
|
|
|
rename($this->logConfig->logPath, $archive_path); |
|
|
|
|
280
|
|
|
// ステータスファイルを削除 |
281
|
|
|
unlink($this->logConfig->statusPath); |
|
|
|
|
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* 時間単位でローテートする |
287
|
|
|
* stream.log -> stream.(作成日時)-(現在日時).log |
288
|
|
|
*/ |
289
|
|
View Code Duplication |
private function rotateByCycle() |
|
|
|
|
290
|
|
|
{ |
291
|
|
|
$this->createstatusPath(); |
292
|
|
|
$now = intval(preg_replace('/^.*\s/', '', microtime())); |
293
|
|
|
$createdAt = $this->readStatus($this->logConfig->statusPath); |
|
|
|
|
294
|
|
|
|
295
|
|
|
// ローテート周期を過ぎている場合はログファイルをアーカイブする |
296
|
|
|
$hour = intval(floor(($now - $createdAt) / 3600)); |
297
|
|
|
if ($hour >= $this->logConfig->rotateCycle) { |
|
|
|
|
298
|
|
|
$this->runRotate($createdAt, $now); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* サイズ単位でローテートする |
304
|
|
|
* stream.log -> stream.(作成日時)-(現在日時).log |
305
|
|
|
*/ |
306
|
|
View Code Duplication |
private function rotateBySize() |
|
|
|
|
307
|
|
|
{ |
308
|
|
|
$this->createstatusPath(); |
309
|
|
|
$now = intval(preg_replace('/^.*\s/', '', microtime())); |
310
|
|
|
$createdAt = $this->readStatus(); |
311
|
|
|
|
312
|
|
|
$size_kb = (int) floor(filesize($this->logConfig->logPath) / 1024); |
|
|
|
|
313
|
|
|
// 指定したサイズより大きければローテート |
314
|
|
|
if ($size_kb >= $this->logConfig->rotateSize) { |
|
|
|
|
315
|
|
|
$this->runRotate($createdAt, $now); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* ログ出力パスを返却する |
321
|
|
|
* @return string ログ出力パス |
322
|
|
|
*/ |
323
|
|
|
public function getLogPath() |
324
|
|
|
{ |
325
|
|
|
return $this->logConfig->logPath; |
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* ログローテートサイクルを返却する |
330
|
|
|
* @return string ログ出力パス |
331
|
|
|
*/ |
332
|
|
|
public function getLogRotateCycle() |
333
|
|
|
{ |
334
|
|
|
return $this->logConfig->rotateCycle; |
|
|
|
|
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* ログローテートサイズを返却する |
339
|
|
|
* @return string ログ出力パス |
340
|
|
|
*/ |
341
|
|
|
public function getLogRotateSize() |
342
|
|
|
{ |
343
|
|
|
return $this->logConfig->rotateSize; |
|
|
|
|
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* 遅延書き出しを有効にする |
348
|
|
|
*/ |
349
|
|
|
public function lazyWrite() |
350
|
|
|
{ |
351
|
|
|
foreach ($this->outputters as $outputter) { |
352
|
|
|
if ($outputter instanceof \WebStream\Log\Outputter\ILazyWriter) { |
353
|
|
|
$outputter->enableLazyWrite(); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* 即時書き出しを有効にする |
360
|
|
|
*/ |
361
|
|
|
public function directWrite() |
362
|
|
|
{ |
363
|
|
|
foreach ($this->outputters as $outputter) { |
364
|
|
|
if ($outputter instanceof \WebStream\Log\Outputter\ILazyWriter) { |
365
|
|
|
$outputter->enableDirectWrite(); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|