1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Utility\FileUtils; |
5
|
|
|
use WebStream\Module\Utility\LoggerUtils; |
6
|
|
|
use WebStream\Module\Container; |
7
|
|
|
use WebStream\Exception\Extend\LoggerException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* LoggerConfigurationManager |
11
|
|
|
* @author Ryuichi Tanaka |
12
|
|
|
* @since 2016/01/29 |
13
|
|
|
* @version 0.7 |
14
|
|
|
*/ |
15
|
|
|
class LoggerConfigurationManager |
16
|
|
|
{ |
17
|
|
|
use FileUtils, LoggerUtils; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Container ログ設定コンテナ |
21
|
|
|
*/ |
22
|
|
|
private $logContainer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<string> ログ設定情報 |
26
|
|
|
*/ |
27
|
|
|
private $configMap; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->logContainer = new Container(false); |
35
|
|
|
$this->configMap = []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 設定を読み込む |
40
|
|
|
* @param string $configPath 設定ファイル相対パス |
41
|
|
|
* @throws LoggerException |
42
|
|
|
*/ |
43
|
|
|
public function load($configPath) |
44
|
|
|
{ |
45
|
|
|
$this->loadConfigFile($configPath) |
46
|
|
|
->loadLogLevel() |
47
|
|
|
->loadLogFilePath() |
48
|
|
|
->loadRotateCycle() |
49
|
|
|
->loadRotateSize() |
50
|
|
|
->loadApplicationName() |
51
|
|
|
->loadFormat(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ログ設定を返却する |
56
|
|
|
* @return Container ログ設定 |
57
|
|
|
*/ |
58
|
|
|
public function getConfig() |
59
|
|
|
{ |
60
|
|
|
return $this->logContainer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 設定ファイルを読み込む |
65
|
|
|
* @param string $configPath 設定アイル相対パス |
66
|
|
|
* @throws LoggerException |
67
|
|
|
*/ |
68
|
|
|
private function loadConfigFile($configPath) |
69
|
|
|
{ |
70
|
|
|
$configMap = $this->parseConfig($configPath); |
71
|
|
|
if ($configMap === null) { |
72
|
|
|
throw new LoggerException("Log config file does not exist: " . $configPath); |
73
|
|
|
} |
74
|
|
|
$this->configMap = $configMap; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* ログレベルを読み込む |
81
|
|
|
* @throws LoggerException |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
private function loadLogLevel() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
if (!array_key_exists("level", $this->configMap)) { |
86
|
|
|
throw new LoggerException("Log level must be defined."); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$logLevel = $this->toLogLevelValue($this->configMap["level"]); |
90
|
|
|
if ($logLevel === 0) { |
91
|
|
|
throw new LoggerException("Invalid log level: " . $this->configMap["level"]); |
92
|
|
|
} |
93
|
|
|
$this->logContainer->logLevel = $logLevel; |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* ログ保存先パスを読み込む |
100
|
|
|
* @throws LoggerException |
101
|
|
|
*/ |
102
|
|
|
private function loadLogFilePath() |
103
|
|
|
{ |
104
|
|
|
if (!array_key_exists("path", $this->configMap)) { |
105
|
|
|
throw new LoggerException("Log path must be defined."); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$path = $this->getApplicationRoot() . "/" . $this->configMap["path"]; |
109
|
|
|
if (!file_exists(dirname($path))) { |
110
|
|
|
throw new LoggerException("Log directory does not exist: " . dirname($path)); |
111
|
|
|
} |
112
|
|
|
$this->logContainer->logPath = $path; |
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->logContainer->statusPath = preg_replace_callback('/(.*)\..+/', function ($matches) { |
|
|
|
|
115
|
|
|
return "$matches[1].status"; |
116
|
|
|
}, $this->logContainer->logPath); |
|
|
|
|
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* ログローテートサイクルを読み込む |
123
|
|
|
* @throws LoggerException |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
private function loadRotateCycle() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
if (array_key_exists("rotate_cycle", $this->configMap)) { |
128
|
|
|
$rotateCycle = $this->cycle2value($this->configMap["rotate_cycle"]); |
129
|
|
|
// 妥当なローテートサイクルか |
130
|
|
|
if ($rotateCycle === 0) { |
131
|
|
|
throw new LoggerException("Invalid log rotate cycle: " . $this->configMap["rotate_cycle"]); |
132
|
|
|
} |
133
|
|
|
$this->logContainer->rotateCycle = $rotateCycle; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* ログローテートサイズを読み込む |
141
|
|
|
* @throws LoggerException |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
private function loadRotateSize() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (array_key_exists("rotate_size", $this->configMap)) { |
146
|
|
|
$rotateSize = intval($this->configMap["rotate_size"]); |
147
|
|
|
// ローテートサイズが不正の場合(正の整数以外の値が設定された場合) |
148
|
|
|
if ($rotateSize <= 0) { |
149
|
|
|
throw new LoggerException("Invalid log rotate size: " . $this->configMap["rotate_size"]); |
150
|
|
|
} |
151
|
|
|
$this->logContainer->rotateSize = $rotateSize; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* アプリケーション名を読み込む |
159
|
|
|
*/ |
160
|
|
|
private function loadApplicationName() |
161
|
|
|
{ |
162
|
|
|
if (array_key_exists("application_name", $this->configMap)) { |
163
|
|
|
$this->logContainer->applicationName = $this->configMap["application_name"]; |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* ロガーフォーマットを読み込む |
171
|
|
|
*/ |
172
|
|
|
private function loadFormat() |
173
|
|
|
{ |
174
|
|
|
if (array_key_exists("format", $this->configMap)) { |
175
|
|
|
$this->logContainer->format = $this->configMap["format"]; |
|
|
|
|
176
|
|
|
} else { |
177
|
|
|
$this->logContainer->format = $this->defaultLoggerFormatter(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* ログローテートサイクルを時間に変換 |
185
|
|
|
* @param string ローテートサイクル |
186
|
|
|
* @return int ローテート時間 |
187
|
|
|
*/ |
188
|
|
|
private function cycle2value($cycle) |
189
|
|
|
{ |
190
|
|
|
$day_to_h = 24; |
191
|
|
|
$week_to_h = $day_to_h * 7; |
192
|
|
|
$month_to_h = $day_to_h * intval(date("t", time())); |
193
|
|
|
$year_to_h = $day_to_h * 365; |
194
|
|
|
|
195
|
|
|
$year = date("Y"); |
196
|
|
|
if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) { |
197
|
|
|
$year_to_h = $day_to_h * 366; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
switch (strtolower($cycle)) { |
201
|
|
|
case 'day': |
202
|
|
|
return $day_to_h; |
203
|
|
|
case 'week': |
204
|
|
|
return $week_to_h; |
205
|
|
|
case 'month': |
206
|
|
|
return $month_to_h; |
207
|
|
|
case 'year': |
208
|
|
|
return $year_to_h; |
209
|
|
|
default: |
210
|
|
|
return 0; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.