-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerConfigurationManager.php
More file actions
247 lines (217 loc) · 6.62 KB
/
LoggerConfigurationManager.php
File metadata and controls
247 lines (217 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace WebStream\Log;
use WebStream\Container\Container;
use WebStream\Exception\Extend\LoggerException;
use WebStream\IO\File;
use WebStream\IO\Writer\SimpleFileWriter;
/**
* LoggerConfigurationManager
* @author Ryuichi Tanaka
* @since 2016/01/29
* @version 0.7
*/
class LoggerConfigurationManager
{
use LoggerUtils;
/**
* @var Container ログ設定コンテナ
*/
private Container $logContainer;
/**
* @var Container IOコンテナ
*/
private Container $ioContainer;
/**
* @var array<string> ログ設定情報
*/
private $configMap;
/**
* Constructor
* @param mixed $config ログ設定
* @throws LoggerException
*/
public function __construct($config)
{
if (is_array($config)) {
$configMap = $config;
} else {
$configMap = parse_ini_file($config);
if ($configMap === null) {
throw new LoggerException("Log config file does not exist: " . $config);
}
}
$this->logContainer = new Container(false);
$this->ioContainer = new Container();
$this->ioContainer->file = function () use ($configMap) {
if (!array_key_exists("path", $configMap)) {
throw new LoggerException("Log path must be defined.");
}
return new File($configMap["path"]);
};
$this->ioContainer->fileWriter = function () use ($configMap) {
return new SimpleFileWriter($configMap["path"]);
};
$this->configMap = $configMap;
}
/**
* 設定を読み込む
* @throws LoggerException
*/
public function load()
{
$this->loadLogLevel()
->loadLogFilePath()
->loadRotateCycle()
->loadRotateSize()
->loadApplicationName()
->loadFormat();
}
/**
* ログ設定を返却する
* @return Container ログ設定
*/
public function getConfig()
{
return $this->logContainer;
}
/**
* ログレベルを読み込む
* @throws LoggerException
*/
private function loadLogLevel()
{
if (!array_key_exists("level", $this->configMap)) {
throw new LoggerException("Log level must be defined.");
}
$logLevel = $this->toLogLevelValue($this->configMap["level"]);
$this->logContainer->logLevel = $logLevel;
return $this;
}
/**
* ログ保存先パスを読み込む
* @throws LoggerException
*/
private function loadLogFilePath()
{
$file = $this->ioContainer->file;
if (!($file->exists() && $file->isFile())) {
$this->ioContainer->fileWriter->write("");
}
$this->logContainer->logPath = $file->getAbsoluteFilePath();
$this->logContainer->statusPath = preg_replace_callback('/(.*)\..+/', function ($matches) {
return "$matches[1].status";
}, $this->logContainer->logPath);
return $this;
}
/**
* ログローテートサイクルを読み込む
* @throws LoggerException
*/
private function loadRotateCycle()
{
if (array_key_exists("rotate_cycle", $this->configMap)) {
$this->logContainer->rotateCycle = $this->cycle2value($this->configMap["rotate_cycle"]);
}
return $this;
}
/**
* ログローテートサイズを読み込む
* @throws LoggerException
*/
private function loadRotateSize()
{
if (array_key_exists("rotate_size", $this->configMap)) {
$rotateSize = intval($this->configMap["rotate_size"]);
// ローテートサイズが不正の場合(正の整数以外の値が設定された場合)
if ($rotateSize <= 0) {
throw new LoggerException("Invalid log rotate size: " . $this->configMap["rotate_size"]);
}
$this->logContainer->rotateSize = $rotateSize;
}
return $this;
}
/**
* アプリケーション名を読み込む
*/
private function loadApplicationName()
{
if (array_key_exists("application_name", $this->configMap) && !empty($this->configMap["application_name"])) {
$this->logContainer->applicationName = $this->configMap["application_name"];
}
return $this;
}
/**
* ロガーフォーマットを読み込む
*/
private function loadFormat()
{
if (array_key_exists("format", $this->configMap)) {
$this->logContainer->format = $this->configMap["format"];
} else {
$this->logContainer->format = $this->defaultLoggerFormatter();
}
return $this;
}
/**
* ログローテートサイクルを時間に変換
* @param string ローテートサイクル
* @return int ローテート時間
* @throws LoggerException
*/
private function cycle2value($cycle)
{
$day_to_h = 24;
$week_to_h = $day_to_h * 7;
$month_to_h = $day_to_h * intval(date("t", time()));
$year_to_h = $day_to_h * 365;
$year = date("Y");
if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) {
$year_to_h = $day_to_h * 366;
}
switch (strtolower($cycle)) {
case 'day':
return $day_to_h;
case 'week':
return $week_to_h;
case 'month':
return $month_to_h;
case 'year':
return $year_to_h;
default:
throw new LoggerException("Invalid log rotate cycle: " . $cycle);
}
}
/**
* ログレベルを数値に変換
* ログレベルはWebStream独自、PSR-3両方対応
* @param string ログレベル文字列
* @throws LoggerException
* @return int ログレベル数値
*/
private function toLogLevelValue(string $level)
{
switch (strtolower($level)) {
case 'debug':
return 1;
case 'info':
return 2;
case 'notice': // PSR-3
return 3;
case 'warn':
case 'warning': // PSR-3
return 4;
case 'error':
return 5;
case 'critical': // PSR-3
return 6;
case 'alert': // PSR-3
return 7;
case 'emergency': // PSR-3
return 8;
case 'fatal':
return 9;
default:
throw new LoggerException("Undefined log level: $level");
}
}
}