1 | <?php |
||
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) {} }); |
||
62 | |||
63 | /** |
||
64 | * destructor |
||
65 | */ |
||
66 | 1 | public function __destruct() |
|
67 | { |
||
68 | try { |
||
69 | 1 | if ($this->isLazyWrite) { |
|
70 | 1 | $this->writeLog(implode("", $this->cache->get())); |
|
71 | } |
||
72 | } catch (\Exception $ignore) { |
||
73 | // デストラクタで例外が発生すると致命的なエラーとなる |
||
74 | } |
||
75 | 1 | } |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 141 | public function enableLazyWrite() |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 1 | public function enableDirectWrite() |
|
92 | { |
||
93 | 1 | if ($this->isLazyWrite && $this->cache->length() > 0) { |
|
94 | 1 | $this->writeLog(implode("", $this->cache->get())); |
|
95 | } |
||
96 | |||
97 | 1 | $this->isLazyWrite = false; |
|
98 | 1 | $this->cache = null; |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 95 | public function write($message) |
|
116 | |||
117 | /** |
||
118 | * バッファをログ出力する |
||
119 | */ |
||
120 | private function flush() |
||
126 | |||
127 | /** |
||
128 | * ログファイルに書き出す |
||
129 | * @param string $message ログメッセージ |
||
130 | */ |
||
131 | 1 | private function writeLog($message) |
|
135 | } |
||
136 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.