1 | <?php |
||
2 | |||
3 | namespace WebStream\Log; |
||
4 | |||
5 | use WebStream\Container\Container; |
||
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 メッセージ |
||
36 | * @param string ログレベル |
||
37 | * @return フォーマット済みメッセージ |
||
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 メッセージ |
||
66 | * @param string アプリケーション名 |
||
67 | * @return 埋め込み済みメッセージ |
||
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 メッセージ |
||
83 | * @return 埋め込み済みメッセージ |
||
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 メッセージ |
||
111 | * @param string ログレベル |
||
112 | * @return 埋め込み済みメッセージ |
||
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 |