Completed
Push — feature/0.7.0 ( 79c386...7f3169 )
by Ryuichi
03:02
created

Logger::finalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace WebStream\Log;
3
4
use WebStream\Module\Utility\LoggerUtils;
5
use WebStream\Module\Container;
6
use WebStream\DI\ServiceLocator;
7
use WebStream\Exception\Extend\LoggerException;
8
9
/**
10
 * Loggerクラス
11
 * @author Ryuichi Tanaka
12
 * @since 2012/01/16
13
 * @version 0.7
14
 */
15
class Logger
16
{
17
    use LoggerUtils;
18
19
    /**
20
     * @var Logger ロガー
21
     */
22
    private static $logger;
23
24
    /**
25
     * @var LoggerFormatter ロガーフォーマッタ
26
     */
27
    private static $formatter;
28
29
    /**
30
     * @var Container ログ設定コンテナ
31
     */
32
    private static $config;
33
34
    /**
35
     * @var Container ログ設定コンテナ
36
     */
37
    private $logConfig;
38
39
    /**
40
     * @var array<IOoutputter> Outputterリスト
41
     */
42
    private $outputters;
43
44
    /**
45
     * コンストラクタ
46
     * @param Container ログ設定コンテナ
47
     */
48
    private function __construct(Container $logConfig)
49
    {
50
        $this->logConfig = $logConfig;
51
        $this->outputters = [];
52
    }
53
54
    /**
55
     * ログ設定を返却する
56
     * @return Container ログ設定
57
     */
58
    public function getConfig()
59
    {
60
        return $this->logConfig;
61
    }
62
63
    /**
64
     * デストラクタ
65
     */
66
    public function __destruct()
67
    {
68
        $this->directWrite();
69
    }
70
71
    /**
72
     * 遅延書き出しを有効にする
73
     */
74
    public static function enableLazyWrite()
75
    {
76
        self::$logger->lazyWrite();
77
    }
78
79
    /**
80
     * 即時書き出しを有効にする
81
     */
82
    public static function enableDirectWrite()
83
    {
84
        self::$logger->directWrite();
85
    }
86
87
    /**
88
     * インスタンスを返却する
89
     * @return WebStream\Module\Logger ロガーインスタンス
90
     */
91
    public static function getInstance()
92
    {
93
        return self::$logger;
94
    }
95
96
    /**
97
     * Loggerを初期化する
98
     * @param Container ログ設定コンテナ
99
     */
100
    public static function init(Container $config)
101
    {
102
        self::$config = $config;
103
        self::$logger = new Logger($config);
104
        self::$formatter = new LoggerFormatter($config);
105
    }
106
107
    /**
108
     * Loggerを終了する
109
     */
110
    public static function finalize()
111
    {
112
        self::$config = null;
113
        self::$logger = null;
114
        self::$formatter = null;
115
    }
116
117
    /**
118
     * Loggerが初期化済みかどうかチェックする
119
     * @param bool 初期化済みならtrue
120
     */
121
    public static function isInitialized()
122
    {
123
        return self::$logger !== null;
124
    }
125
126
    /**
127
     * Loggerメソッドの呼び出しを受ける
128
     * @param string メソッド名(ログレベル文字列)
129
     * @param array 引数
130
     */
131
    public static function __callStatic($level, $arguments)
132
    {
133
        if (self::$logger === null || self::$formatter === null) {
134
            if (self::$config !== null) {
135
                self::init(self::$config);
136
            } else {
137
                throw new LoggerException("Logger is not initialized.");
138
            }
139
        }
140
141
        call_user_func_array([self::$logger, "write"], array_merge([$level], $arguments));
142
    }
143
144
    /**
145
     * Outputterを設定する
146
     * @param array<IOutputter> $outputters Outputterリスト
147
     */
148
    public function setOutputter(array $outputters)
149
    {
150
        foreach ($outputters as $outputter) {
151
            if (!$outputter instanceof \WebStream\Log\Outputter\IOutputter) {
152
                throw new LoggerException("Log outputter must implement WebStream\Log\Outputter\IOutputter.");
153
            }
154
        }
155
        $this->outputters = $outputters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $outputters of type array<integer,object<WebStream\Log\IOutputter>> is incompatible with the declared type array<integer,object<WebStream\Log\IOoutputter>> of property $outputters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
156
    }
157
158
    /**
159
     * タイムスタンプを取得する
160
     * @return string タイムスタンプ
161
     */
162
    private function getTimeStamp()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
163
    {
164
        date_default_timezone_set('Asia/Tokyo');
165
        $msec = sprintf("%2d", floatval(microtime()) * 100);
166
167
        return strftime("%Y-%m-%d %H:%M:%S") . "," . $msec;
168
    }
169
170
    /**
171
     * ログメッセージにスタックトレースの内容を追加する
172
     * @param string ログメッセージ
173
     * @param string スタックトレース文字列
174
     * @return string 加工済みログメッセージ
175
     */
176
    private function message($msg, $stacktrace = null)
177
    {
178
        // スタックトレースから原因となるエラー箇所のみ抽出
179
        $stacktraceList = explode("#", $stacktrace);
180
        foreach ($stacktraceList as $stacktraceLine) {
181
            if ($stacktraceLine === "") {
182
                continue;
183
            }
184
            $msg .= "\n";
185
            $msg .= "\t#" . trim($stacktraceLine);
186
        }
187
188
        return $msg;
189
    }
190
191
    /**
192
     * ログを書き出す
193
     * @param string ログレベル文字列
194
     * @param string 出力文字列
195
     * @param array<mixed> 埋め込み値リスト
196
     */
197
    public function write($level, $msg, $context = null)
198
    {
199
        if ($this->logConfig->logLevel > $this->toLogLevelValue($level)) {
0 ignored issues
show
Documentation introduced by
The property logLevel does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
200
            return;
201
        }
202
203
        if (is_array($context)) {
204
            // sprintfと同様の展開
205
            // [a-zA-Z0-9_-\.] 以外もキーには指定可能だが仕様としてこれ以外は不可とする
206
            preg_match_all('/\{\s*([a-zA-Z0-9._-]+)\s*?\}/', $msg, $matches);
207
            foreach ($matches[1] as $index => $value) {
208
                if (array_key_exists($value, $context)) {
209
                    $matches[1][$index] = $context[$value];
210
                } else {
211
                    unset($matches[0][$index]);
212
                }
213
            }
214
            $msg = str_replace($matches[0], $matches[1], $msg);
215
        } else {
216
            $msg = $this->message($msg, $context);
217
        }
218
219
        $this->rotate();
220
        try {
221
            if (count($this->outputters) > 0) {
222
                foreach ($this->outputters as $outputter) {
223
                    $outputter->write(self::$formatter->getFormattedMessage($msg, $level));
224
                }
225
            } else {
226
                error_log(self::$formatter->getFormattedMessage($msg, $level), 3, $this->logConfig->logPath);
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
227
            }
228
        } catch (\Exception $e) {
229
            throw new LoggerException($e);
230
        }
231
    }
232
233
    /**
234
     * ログステータスファイルに書きこむ
235
     */
236
    private function writeStatus()
237
    {
238
        file_put_contents($this->logConfig->statusPath, intval(preg_replace('/^.*\s/', '', microtime())));
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
239
    }
240
241
    /**
242
     * ログステータスファイルを読み込む
243
     */
244
    private function readStatus()
245
    {
246
        $handle = fopen($this->logConfig->statusPath, "r");
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
247
        $size = filesize($this->logConfig->statusPath);
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
248
        $content = fread($handle, $size);
249
        fclose($handle);
250
        if (!preg_match('/^\d{10}$/', $content)) {
251
            throw new LoggerException("Invalid log state file contents: " . $content);
252
        }
253
254
        return intval($content);
255
    }
256
257
    /**
258
     * ステータスファイルを作成する
259
     */
260
    private function createstatusPath()
261
    {
262
        // ステータスファイルがない場合は書きだす
263
        if (!is_file($this->logConfig->statusPath)) {
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
264
            $this->writeStatus();
265
        }
266
    }
267
268
    /**
269
     * ログファイルをアーカイブする
270
     * stream.log -> stream.(作成日時)-(現在日時).log
271
     * @param string ログファイルパス
272
     */
273
    private function rotate()
274
    {
275
        // ログファイルがない場合はローテートしない
276
        if (!realpath($this->logConfig->logPath)) {
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
277
            return;
278
        }
279
        // ログローテート実行
280
        if ($this->logConfig->rotateCycle !== null) {
0 ignored issues
show
Documentation introduced by
The property rotateCycle does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
281
            $this->rotateByCycle();
282
        } elseif ($this->logConfig->rotateSize !== null) {
0 ignored issues
show
Documentation introduced by
The property rotateSize does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
283
            $this->rotateBySize();
284
        }
285
    }
286
287
    /**
288
     * ローテートを実行する
289
     * @param integer 作成日時のUnixTime
290
     * @param integer 現在日時のUnixTime
291
     */
292
    private function runRotate($from, $to)
293
    {
294
        $from_date = date("YmdHis", $from);
295
        $to_date = date("YmdHis", $to);
296
        $archive_path = null;
0 ignored issues
show
Unused Code introduced by
$archive_path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
297
        if (preg_match('/(.*)\.(.+)/', $this->logConfig->logPath, $matches)) {
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
298
            $archive_path = "$matches[1].${from_date}-${to_date}.$matches[2]";
299
            // mvを実行
300
            rename($this->logConfig->logPath, $archive_path);
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
301
            // ステータスファイルを削除
302
            unlink($this->logConfig->statusPath);
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
303
        }
304
    }
305
306
    /**
307
     * 時間単位でローテートする
308
     * stream.log -> stream.(作成日時)-(現在日時).log
309
     */
310 View Code Duplication
    private function rotateByCycle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
311
    {
312
        $this->createstatusPath();
313
        $now = intval(preg_replace('/^.*\s/', '', microtime()));
314
        $createdAt = $this->readStatus($this->logConfig->statusPath);
0 ignored issues
show
Documentation introduced by
The property statusPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
The call to Logger::readStatus() has too many arguments starting with $this->logConfig->statusPath.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
315
316
        // ローテート周期を過ぎている場合はログファイルをアーカイブする
317
        $hour = intval(floor(($now - $createdAt) / 3600));
318
        if ($hour >= $this->logConfig->rotateCycle) {
0 ignored issues
show
Documentation introduced by
The property rotateCycle does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
319
            $this->runRotate($createdAt, $now);
320
        }
321
    }
322
323
    /**
324
     * サイズ単位でローテートする
325
     * stream.log -> stream.(作成日時)-(現在日時).log
326
     */
327 View Code Duplication
    private function rotateBySize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
328
    {
329
        $this->createstatusPath();
330
        $now = intval(preg_replace('/^.*\s/', '', microtime()));
331
        $createdAt = $this->readStatus();
332
333
        $size_kb = (int) floor(filesize($this->logConfig->logPath) / 1024);
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
334
        // 指定したサイズより大きければローテート
335
        if ($size_kb >= $this->logConfig->rotateSize) {
0 ignored issues
show
Documentation introduced by
The property rotateSize does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
336
            $this->runRotate($createdAt, $now);
337
        }
338
    }
339
340
    /**
341
     * ログ出力パスを返却する
342
     * @return string ログ出力パス
343
     */
344
    public function getLogPath()
345
    {
346
        return $this->logConfig->logPath;
0 ignored issues
show
Documentation introduced by
The property logPath does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
347
    }
348
349
    /**
350
     * ログローテートサイクルを返却する
351
     * @return string ログ出力パス
352
     */
353
    public function getLogRotateCycle()
354
    {
355
        return $this->logConfig->rotateCycle;
0 ignored issues
show
Documentation introduced by
The property rotateCycle does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
356
    }
357
358
    /**
359
     * ログローテートサイズを返却する
360
     * @return string ログ出力パス
361
     */
362
    public function getLogRotateSize()
363
    {
364
        return $this->logConfig->rotateSize;
0 ignored issues
show
Documentation introduced by
The property rotateSize does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
365
    }
366
367
    /**
368
     * 遅延書き出しを有効にする
369
     */
370
    public function lazyWrite()
371
    {
372
        foreach ($this->outputters as $outputter) {
373
            if ($outputter instanceof \WebStream\Log\Outputter\ILazyWriter) {
374
                $outputter->enableLazyWrite();
375
            }
376
        }
377
    }
378
379
    /**
380
     * 即時書き出しを有効にする
381
     */
382
    public function directWrite()
383
    {
384
        foreach ($this->outputters as $outputter) {
385
            if ($outputter instanceof \WebStream\Log\Outputter\ILazyWriter) {
386
                $outputter->enableDirectWrite();
387
            }
388
        }
389
    }
390
}
391