1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log\Outputter; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ConsoleOutputter |
6
|
|
|
* @author Ryuichi Tanaka |
7
|
|
|
* @since 2016/01/26 |
8
|
|
|
* @version 0.7 |
9
|
|
|
*/ |
10
|
|
|
class ConsoleOutputter implements IOutputter, ILazyWriter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* https://github.com/php/php-src/tree/master/sapi |
14
|
|
|
* PHP7以前のものは対応しない |
15
|
|
|
* @var SAPIリスト |
16
|
|
|
*/ |
17
|
|
|
private $sapis = [ |
18
|
|
|
'apache2handler' => 'http', |
19
|
|
|
'cgi' => 'http', |
20
|
|
|
'cli' => 'console', |
21
|
|
|
'fpm' => 'http', |
22
|
|
|
'embed' => 'unsupported', |
23
|
|
|
'litespeed' => 'unsupported', |
24
|
|
|
'phpdbg' => 'unsupported', |
25
|
|
|
'tests' => 'unsupported' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array<string> ログメッセージリスト |
30
|
|
|
*/ |
31
|
|
|
private $logMessages; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int バッファリングサイズ |
35
|
|
|
*/ |
36
|
|
|
private $bufferSize; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool 遅延書き出しフラグ |
40
|
|
|
*/ |
41
|
|
|
private $isLazyWrite; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* constructor |
45
|
|
|
*/ |
46
|
126 |
|
public function __construct($bufferSize = 1000) |
47
|
|
|
{ |
48
|
126 |
|
$this->logMessages = []; |
49
|
126 |
|
$this->bufferSize = $bufferSize; |
50
|
126 |
|
$this->isLazyWrite = false; |
51
|
126 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* destructor |
55
|
|
|
*/ |
56
|
|
|
public function __destruct() |
57
|
|
|
{ |
58
|
|
|
$this->writeLog(implode("", $this->logMessages)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function enableLazyWrite() |
65
|
|
|
{ |
66
|
1 |
|
$this->isLazyWrite = true; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function enableDirectWrite() |
73
|
|
|
{ |
74
|
2 |
|
$this->flush(); |
75
|
2 |
|
$this->isLazyWrite = false; |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
82 |
|
public function write($message) |
82
|
|
|
{ |
83
|
82 |
|
if ($this->isLazyWrite) { |
84
|
1 |
|
if (count($this->logMessages) >= $this->bufferSize) { |
85
|
|
|
$this->flush(); |
86
|
|
|
$this->clear(); |
87
|
|
|
} |
88
|
1 |
|
$this->logMessages[] = $message; |
89
|
|
|
} else { |
90
|
81 |
|
$this->writeLog($message); |
91
|
|
|
} |
92
|
82 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* バッファをクリアする |
96
|
|
|
*/ |
97
|
1 |
|
private function clear() |
98
|
|
|
{ |
99
|
1 |
|
$this->logMessages = []; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* バッファをログ出力する |
104
|
|
|
*/ |
105
|
2 |
|
private function flush() |
106
|
|
|
{ |
107
|
2 |
|
if ($this->isLazyWrite && count($this->logMessages) > 0) { |
108
|
1 |
|
$this->writeLog(implode("", $this->logMessages)); |
109
|
1 |
|
$this->clear(); |
110
|
|
|
} |
111
|
2 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* ログファイルに書き出す |
115
|
|
|
* @param string $message ログメッセージ |
116
|
|
|
*/ |
117
|
82 |
|
private function writeLog($message) |
118
|
|
|
{ |
119
|
82 |
|
$sapi = php_sapi_name(); |
120
|
82 |
|
if (array_key_exists($sapi, $this->sapis) && $this->sapis[$sapi] === 'console') { |
121
|
82 |
|
echo $message; |
122
|
|
|
} |
123
|
82 |
|
} |
124
|
|
|
} |
125
|
|
|
|