1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log\Outputter; |
3
|
|
|
|
4
|
|
|
use WebStream\Cache\Driver\ICache; |
|
|
|
|
5
|
|
|
use WebStream\Cache\Driver\CacheDriverFactory; |
|
|
|
|
6
|
|
|
use WebStream\Container\Container; |
|
|
|
|
7
|
|
|
use WebStream\IO\Writer\SimpleFileWriter; |
|
|
|
|
8
|
|
|
use WebStream\Log\LoggerCache; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* FileOutputter |
12
|
|
|
* @author Ryuichi Tanaka |
13
|
|
|
* @since 2016/01/26 |
14
|
|
|
* @version 0.7 |
15
|
|
|
*/ |
16
|
|
|
class FileOutputter implements IOutputter, ILazyWriter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ICache キャッシュドライバ |
20
|
|
|
*/ |
21
|
|
|
private $driver; |
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
|
141 |
|
public function __construct(string $logPath, int $bufferSize = 1000) |
49
|
|
|
{ |
50
|
141 |
|
$config = new Container(false); |
51
|
141 |
|
$config->classPrefix = "logger_cache"; |
52
|
141 |
|
$factory = new CacheDriverFactory(); |
53
|
141 |
|
$driver = $factory->create("WebStream\Cache\Driver\Apcu", $config); |
54
|
|
|
|
55
|
|
|
// LoggerCacheの中ではログは取らない |
56
|
|
|
$driver->inject('logger', new class() { function __call($name, $args) {} }); |
57
|
141 |
|
$this->driver = $driver; |
58
|
141 |
|
$this->bufferSize = $bufferSize; |
59
|
141 |
|
$this->enableLazyWrite(); |
60
|
141 |
|
$this->writer = new SimpleFileWriter($logPath); |
61
|
141 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* destructor |
65
|
|
|
*/ |
66
|
|
|
public function __destruct() |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
if ($this->isLazyWrite) { |
70
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
71
|
|
|
} |
72
|
|
|
} catch (\Exception $ignore) { |
73
|
|
|
// デストラクタで例外が発生すると致命的なエラーとなる |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
141 |
|
public function enableLazyWrite() |
81
|
|
|
{ |
82
|
141 |
|
if ($this->driver !== null) { |
83
|
141 |
|
$this->isLazyWrite = true; |
84
|
141 |
|
$this->cache = new LoggerCache($this->driver); |
85
|
|
|
} |
86
|
141 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function enableDirectWrite() |
92
|
|
|
{ |
93
|
|
View Code Duplication |
if ($this->isLazyWrite && $this->cache->length() > 0) { |
|
|
|
|
94
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->isLazyWrite = false; |
98
|
|
|
$this->cache = null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
95 |
View Code Duplication |
public function write($message) |
|
|
|
|
105
|
|
|
{ |
106
|
95 |
|
if ($this->isLazyWrite) { |
107
|
95 |
|
if ($this->cache->length() >= $this->bufferSize) { |
108
|
|
|
$this->flush(); |
109
|
|
|
$this->clear(); |
|
|
|
|
110
|
|
|
} |
111
|
95 |
|
$this->cache->add($message); |
112
|
|
|
} else { |
113
|
|
|
$this->writeLog($message); |
114
|
|
|
} |
115
|
95 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* バッファをログ出力する |
119
|
|
|
*/ |
120
|
|
|
private function flush() |
121
|
|
|
{ |
122
|
|
View Code Duplication |
if ($this->isLazyWrite && $this->cache->length() > 0) { |
|
|
|
|
123
|
|
|
$this->writeLog(implode("", $this->cache->get())); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* ログファイルに書き出す |
129
|
|
|
* @param string $message ログメッセージ |
130
|
|
|
*/ |
131
|
|
|
private function writeLog($message) |
132
|
|
|
{ |
133
|
|
|
$this->writer->write($message); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths