-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerFormatter.php
More file actions
128 lines (111 loc) · 4.2 KB
/
LoggerFormatter.php
File metadata and controls
128 lines (111 loc) · 4.2 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
<?php
namespace WebStream\Log;
use WebStream\Container\Container;
/**
* LoggerFormatterクラス
* ログフォーマッタ処理を行う
* @author Ryuichi Tanaka
* @since 2015/12/06
* @version 0.7
*/
class LoggerFormatter
{
use LoggerUtils;
/**
* @var Container ログ設定コンテナ
*/
private Container $logConfig;
/**
* コンストラクタ
* @param Container $logConfig
*/
public function __construct(Container $logConfig)
{
$this->logConfig = $logConfig;
$this->compile();
}
/**
* フォーマット済みメッセージを返却する
* @param string メッセージ
* @param string ログレベル
* @return フォーマット済みメッセージ
*/
public function getFormattedMessage($message, $logLevel)
{
$formattedMessage = $this->logConfig->format;
// 日付
$formattedMessage = $this->compileDateTime($formattedMessage);
// ログレベル
$formattedMessage = $this->compileLogLevel($formattedMessage, $logLevel);
// メッセージ
$formattedMessage = preg_replace('/%m/', $message, $formattedMessage);
return $formattedMessage . PHP_EOL;
}
/**
* 固定の項目を埋め込む
*/
private function compile()
{
$this->logConfig->format = $this->compileApplicationName($this->logConfig->format, $this->logConfig->applicationName);
}
/**
* アプリケーション名項目を埋め込む
* @param string メッセージ
* @param string アプリケーション名
* @return 埋め込み済みメッセージ
*/
private function compileApplicationName($message, $applicationName)
{
// アプリケーション名
if ($applicationName !== null && preg_match('/%([0-9]{0,})c/', $this->logConfig->format, $matches)) {
$applicationName = $matches[1] !== null ? str_pad($applicationName, intval($matches[1]), ' ') : $applicationName;
$message = preg_replace('/%(?:[0-9]*)c/', $applicationName, $message);
}
return $message;
}
/**
* 日付項目を埋め込む
* @param string メッセージ
* @return 埋め込み済みメッセージ
*/
private function compileDateTime($message)
{
if (preg_match('/%([0-9]{0,})d(?:\{(.+?)\}){1}/', $message, $formatMatches)) {
$message = preg_replace('/%[0-9]*d/', '%d', $message);
$now = microtime(true);
$decimal = "000";
if (preg_match('/^[0-9]*\\.([0-9]+)$/', $now, $matches) === 1) {
$decimal = str_pad(substr($matches[1], 0, 3), 3, "0");
}
$dateTimeFormat = preg_replace('/(%f)/', $decimal, $formatMatches[2]);
$dateTime = strftime($dateTimeFormat, $now);
$dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
$message = preg_replace('/%d\{.+?\}/', $dateTime, $message);
} elseif (preg_match('/%([0-9]*)d/', $message, $formatMatches)) {
$message = preg_replace('/%[0-9]*d/', '%d', $message);
$dateTime = strftime($this->defaultDateTimeFormatter());
$dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
$message = preg_replace('/%d/', $dateTime, $message);
}
return $message;
}
/**
* ログレベル項目を埋め込む
* @param string メッセージ
* @param string ログレベル
* @return 埋め込み済みメッセージ
*/
private function compileLogLevel($message, $logLevel)
{
// ログレベル
if (preg_match('/%([0-9]*)L/', $message, $matches)) {
$upperLevel = strtoupper(empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' '));
$message = preg_replace('/%([0-9]*)L/', $upperLevel, $message);
}
if (preg_match('/%([0-9]*)l/', $message, $matches)) {
$lowerLevel = empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' ');
$message = preg_replace('/%([0-9]*)l/', $lowerLevel, $message);
}
return $message;
}
}