1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log\Outputter; |
3
|
|
|
|
4
|
|
|
use WebStream\IO\Writer\SimpleFileWriter; |
5
|
|
|
use WebStream\Log\LoggerCache; |
6
|
|
|
use WebStream\Module\Utility\LoggerUtils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* FileOutputter |
10
|
|
|
* @author Ryuichi Tanaka |
11
|
|
|
* @since 2016/01/26 |
12
|
|
|
* @version 0.7 |
13
|
|
|
*/ |
14
|
|
|
class FileOutputter implements IOutputter, ILazyWriter |
15
|
|
|
{ |
16
|
|
|
use LoggerUtils; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string ログファイルパス |
20
|
|
|
*/ |
21
|
|
|
private $logPath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int バッファリングサイズ |
25
|
|
|
*/ |
26
|
|
|
private $bufferSize; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool 遅延書き出しフラグ |
30
|
|
|
*/ |
31
|
|
|
private $isLazyWrite; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SimpleFileWriter Writerオブジェクト |
35
|
|
|
*/ |
36
|
|
|
private $writer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoggerCache ログキャッシュ |
40
|
|
|
*/ |
41
|
|
|
private $cache; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* constructor |
45
|
|
|
* @param string $logPath ログファイルパス |
46
|
|
|
* @param int $bufferSize バッファリングサイズ |
47
|
|
|
*/ |
48
|
|
|
public function __construct($logPath, $bufferSize = 1000) |
49
|
|
|
{ |
50
|
|
|
$this->logPath = $logPath; |
51
|
|
|
$this->bufferSize = $bufferSize; |
52
|
|
|
$this->enableLazyWrite(); |
53
|
|
|
$this->writer = new SimpleFileWriter($logPath); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* destructor |
58
|
|
|
*/ |
59
|
|
|
public function __destruct() |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
if ($this->isLazyWrite) { |
63
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
64
|
|
|
} |
65
|
|
|
} catch (\Exception $ignore) { |
66
|
|
|
// デストラクタで例外が発生すると致命的なエラーとなる |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function enableLazyWrite() |
74
|
|
|
{ |
75
|
|
|
if ($this->enableApcu()) { |
76
|
|
|
$this->isLazyWrite = true; |
77
|
|
|
$this->cache = new LoggerCache(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function enableDirectWrite() |
85
|
|
|
{ |
86
|
|
View Code Duplication |
if ($this->isLazyWrite && $this->cache->length() > 0) { |
|
|
|
|
87
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->isLazyWrite = false; |
91
|
|
|
$this->cache = null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function write($message) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if ($this->isLazyWrite) { |
100
|
|
|
if ($this->cache->length() >= $this->bufferSize) { |
101
|
|
|
$this->flush(); |
102
|
|
|
$this->clear(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
$this->cache->add($message); |
105
|
|
|
} else { |
106
|
|
|
$this->writeLog($message); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* バッファをログ出力する |
112
|
|
|
*/ |
113
|
|
|
private function flush() |
114
|
|
|
{ |
115
|
|
View Code Duplication |
if ($this->isLazyWrite && $this->cache->length() > 0) { |
|
|
|
|
116
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* ログファイルに書き出す |
122
|
|
|
* @param string $message ログメッセージ |
123
|
|
|
*/ |
124
|
|
|
private function writeLog($message) |
125
|
|
|
{ |
126
|
|
|
$this->writer->write($message); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.