LoggerFormatter::compileLogLevel()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.6111
cc 5
nc 6
nop 2
crap 5
1
<?php
2
3
namespace WebStream\Log;
4
5
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...
6
7
/**
8
 * LoggerFormatterクラス
9
 * ログフォーマッタ処理を行う
10
 * @author Ryuichi Tanaka
11
 * @since 2015/12/06
12
 * @version 0.7
13
 */
14
class LoggerFormatter
15
{
16
    use LoggerUtils;
17
18
    /**
19
     * @var Container ログ設定コンテナ
20
     */
21
    private Container $logConfig;
22
23
    /**
24
     * コンストラクタ
25
     * @param Container $logConfig
26 141
     */
27
    public function __construct(Container $logConfig)
28 141
    {
29 141
        $this->logConfig = $logConfig;
30 141
        $this->compile();
31
    }
32
33
    /**
34
     * フォーマット済みメッセージを返却する
35
     * @param  string メッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment メッセージ at position 0 could not be parsed: Unknown type name 'メッセージ' at position 0 in メッセージ.
Loading history...
36
     * @param  string ログレベル
37
     * @return フォーマット済みメッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment フォーマット済みメッセージ at position 0 could not be parsed: Unknown type name 'フォーマット済みメッセージ' at position 0 in フォーマット済みメッセージ.
Loading history...
38 97
     */
39
    public function getFormattedMessage($message, $logLevel)
40 97
    {
41
        $formattedMessage = $this->logConfig->format;
42
43 97
        // 日付
44
        $formattedMessage = $this->compileDateTime($formattedMessage);
45
46 97
        // ログレベル
47
        $formattedMessage = $this->compileLogLevel($formattedMessage, $logLevel);
48
49 97
        // メッセージ
50
        $formattedMessage = preg_replace('/%m/', $message, $formattedMessage);
51 97
52
        return $formattedMessage . PHP_EOL;
53
    }
54
55
    /**
56
     * 固定の項目を埋め込む
57 141
     */
58
    private function compile()
59 141
    {
60 141
        $this->logConfig->format = $this->compileApplicationName($this->logConfig->format, $this->logConfig->applicationName);
61
    }
62
63
    /**
64
     * アプリケーション名項目を埋め込む
65
     * @param  string メッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment メッセージ at position 0 could not be parsed: Unknown type name 'メッセージ' at position 0 in メッセージ.
Loading history...
66
     * @param  string アプリケーション名
67
     * @return 埋め込み済みメッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment 埋め込み済みメッセージ at position 0 could not be parsed: Unknown type name '埋め込み済みメッセージ' at position 0 in 埋め込み済みメッセージ.
Loading history...
68 141
     */
69
    private function compileApplicationName($message, $applicationName)
70
    {
71 141
        // アプリケーション名
72 15
        if ($applicationName !== null && preg_match('/%([0-9]{0,})c/', $this->logConfig->format, $matches)) {
73 15
            $applicationName = $matches[1] !== null ? str_pad($applicationName, intval($matches[1]), ' ') : $applicationName;
74
            $message = preg_replace('/%(?:[0-9]*)c/', $applicationName, $message);
75
        }
76 141
77
        return $message;
78
    }
79
80
    /**
81
     * 日付項目を埋め込む
82
     * @param  string メッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment メッセージ at position 0 could not be parsed: Unknown type name 'メッセージ' at position 0 in メッセージ.
Loading history...
83
     * @return 埋め込み済みメッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment 埋め込み済みメッセージ at position 0 could not be parsed: Unknown type name '埋め込み済みメッセージ' at position 0 in 埋め込み済みメッセージ.
Loading history...
84 97
     */
85
    private function compileDateTime($message)
86 97
    {
87 16
        if (preg_match('/%([0-9]{0,})d(?:\{(.+?)\}){1}/', $message, $formatMatches)) {
88 16
            $message = preg_replace('/%[0-9]*d/', '%d', $message);
89 16
            $now = microtime(true);
90 16
            $decimal = "000";
91 16
            if (preg_match('/^[0-9]*\\.([0-9]+)$/', $now, $matches) === 1) {
92
                $decimal = str_pad(substr($matches[1], 0, 3), 3, "0");
93 16
            }
94 16
            $dateTimeFormat = preg_replace('/(%f)/', $decimal, $formatMatches[2]);
95 16
            $dateTime = strftime($dateTimeFormat, $now);
96 16
            $dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
97 81
            $message = preg_replace('/%d\{.+?\}/', $dateTime, $message);
98 2
        } elseif (preg_match('/%([0-9]*)d/', $message, $formatMatches)) {
99 2
            $message = preg_replace('/%[0-9]*d/', '%d', $message);
100 2
            $dateTime = strftime($this->defaultDateTimeFormatter());
101 2
            $dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
102
            $message = preg_replace('/%d/', $dateTime, $message);
103
        }
104 97
105
        return $message;
106
    }
107
108
    /**
109
     * ログレベル項目を埋め込む
110
     * @param  string メッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment メッセージ at position 0 could not be parsed: Unknown type name 'メッセージ' at position 0 in メッセージ.
Loading history...
111
     * @param  string ログレベル
112
     * @return 埋め込み済みメッセージ
0 ignored issues
show
Documentation Bug introduced by
The doc comment 埋め込み済みメッセージ at position 0 could not be parsed: Unknown type name '埋め込み済みメッセージ' at position 0 in 埋め込み済みメッセージ.
Loading history...
113 97
     */
114
    private function compileLogLevel($message, $logLevel)
115
    {
116 97
        // ログレベル
117 16
        if (preg_match('/%([0-9]*)L/', $message, $matches)) {
118 16
            $upperLevel = strtoupper(empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' '));
119
            $message = preg_replace('/%([0-9]*)L/', $upperLevel, $message);
120 97
        }
121 2
        if (preg_match('/%([0-9]*)l/', $message, $matches)) {
122 2
            $lowerLevel = empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' ');
123
            $message = preg_replace('/%([0-9]*)l/', $lowerLevel, $message);
124
        }
125 97
126
        return $message;
127
    }
128
}
129