FileOutputter.php$0 ➔ __call()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
crap 1
1
<?php
2
3
namespace WebStream\Log\Outputter;
4
5
use WebStream\Cache\Driver\ICache;
0 ignored issues
show
Bug introduced by
The type WebStream\Cache\Driver\ICache was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Cache\Driver\CacheDriverFactory;
0 ignored issues
show
Bug introduced by
The type WebStream\Cache\Driver\CacheDriverFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use WebStream\IO\Writer\SimpleFileWriter;
0 ignored issues
show
Bug introduced by
The type WebStream\IO\Writer\SimpleFileWriter was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use WebStream\Log\LoggerCache;
10
11
/**
12
 * FileOutputter
13
 * @author Ryuichi Tanaka
14
 * @since 2016/01/26
15
 * @version 0.7
16
 */
17
class FileOutputter implements IOutputter, ILazyWriter
18
{
19
    /**
20
     * @var ICache キャッシュドライバ
21
     */
22
    private ICache $driver;
23
24
    /**
25
     * @var int バッファリングサイズ
26
     */
27
    private int $bufferSize;
28
29
    /**
30
     * @var bool 遅延書き出しフラグ
31
     */
32
    private bool $isLazyWrite;
33
34
    /**
35
     * @var SimpleFileWriter Writerオブジェクト
36
     */
37
    private SimpleFileWriter $writer;
38
39
    /**
40
     * @var LoggerCache ログキャッシュ
41
     */
42
    private LoggerCache $cache;
43
44
    /**
45
     * constructor
46
     * @param string $logPath ログファイルパス
47
     * @param int $bufferSize バッファリングサイズ
48 141
     */
49
    public function __construct(string $logPath, int $bufferSize = 1000)
50 141
    {
51 141
        $config = new Container(false);
52 141
        $config->classPrefix = "logger_cache";
53 141
        $factory = new CacheDriverFactory();
54
        $driver = $factory->create("WebStream\Cache\Driver\Apcu", $config);
55
56
        // LoggerCacheの中ではログは取らない
57 141
        $driver->inject('logger', new class ()
58 141
        {
59 141
            public function __call($name, $args)
60 141
            {
61 141
            }
62
        });
63
        $this->driver = $driver;
64
        $this->bufferSize = $bufferSize;
65
        $this->enableLazyWrite();
66
        $this->writer = new SimpleFileWriter($logPath);
67
    }
68
69
    /**
70
     * destructor
71
     */
72
    public function __destruct()
73
    {
74
        try {
75
            if ($this->isLazyWrite) {
76
                $this->writeLog(implode("", $this->cache->get()));
77
            }
78
        } catch (\Exception $ignore) {
79
            // デストラクタで例外が発生すると致命的なエラーとなる
80 141
        }
81
    }
82 141
83 141
    /**
84 141
     * {@inheritdoc}
85
     */
86 141
    public function enableLazyWrite()
87
    {
88
        if ($this->driver !== null) {
89
            $this->isLazyWrite = true;
90
            $this->cache = new LoggerCache($this->driver);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function enableDirectWrite()
98
    {
99
        if ($this->isLazyWrite && $this->cache->length() > 0) {
100
            $this->writeLog(implode("", $this->cache->get()));
101
        }
102
103
        $this->isLazyWrite = false;
104 95
        unset($this->cache);
105
    }
106 95
107 95
    /**
108
     * {@inheritdoc}
109
     */
110
    public function write(string $message)
111 95
    {
112
        if ($this->isLazyWrite) {
113
            if ($this->cache->length() >= $this->bufferSize) {
114
                $this->flush();
115 95
                $this->cache->clear();
116
            }
117
            $this->cache->add($message);
118
        } else {
119
            $this->writeLog($message);
120
        }
121
    }
122
123
    /**
124
     * バッファをログ出力する
125
     */
126
    private function flush()
127
    {
128
        if ($this->isLazyWrite && $this->cache->length() > 0) {
129
            $this->writeLog(implode("", $this->cache->get()));
130
        }
131
    }
132
133
    /**
134
     * ログファイルに書き出す
135
     * @param string $message ログメッセージ
136
     */
137
    private function writeLog(string $message)
138
    {
139
        $this->writer->write($message);
140
    }
141
}
142