1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Log; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Container; |
5
|
|
|
use WebStream\Module\Utility\LoggerUtils; |
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 $logConfig; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* コンストラクタ |
25
|
|
|
* @param string 設定ファイルパス |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Container $logConfig) |
28
|
|
|
{ |
29
|
|
|
// $this->loadConfig($configPath); |
|
|
|
|
30
|
|
|
$this->logConfig = $logConfig; |
31
|
|
|
$this->compile(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* フォーマット済みメッセージを返却する |
36
|
|
|
* @param string メッセージ |
37
|
|
|
* @param string ログレベル |
38
|
|
|
* @return フォーマット済みメッセージ |
39
|
|
|
*/ |
40
|
|
|
public function getFormattedMessage($message, $logLevel) |
41
|
|
|
{ |
42
|
|
|
$formattedMessage = $this->logConfig->format; |
|
|
|
|
43
|
|
|
|
44
|
|
|
// 日付 |
45
|
|
|
$formattedMessage = $this->compileDateTime($formattedMessage); |
46
|
|
|
|
47
|
|
|
// ログレベル |
48
|
|
|
$formattedMessage = $this->compileLogLevel($formattedMessage, $logLevel); |
49
|
|
|
|
50
|
|
|
// メッセージ |
51
|
|
|
$formattedMessage = preg_replace('/%m/', $message, $formattedMessage); |
52
|
|
|
|
53
|
|
|
return $formattedMessage . "\n"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 固定の項目を埋め込む |
58
|
|
|
*/ |
59
|
|
|
private function compile() |
60
|
|
|
{ |
61
|
|
|
$this->logConfig->format = $this->compileApplicationName($this->logConfig->format, $this->logConfig->applicationName); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* アプリケーション名項目を埋め込む |
66
|
|
|
* @param string メッセージ |
67
|
|
|
* @param string アプリケーション名 |
68
|
|
|
* @return 埋め込み済みメッセージ |
69
|
|
|
*/ |
70
|
|
|
private function compileApplicationName($message, $applicationName) |
71
|
|
|
{ |
72
|
|
|
// アプリケーション名 |
73
|
|
|
if ($applicationName !== null && preg_match('/%([0-9]{0,})c/', $this->logConfig->format, $matches)) { |
|
|
|
|
74
|
|
|
$applicationName = $matches[1] !== null ? str_pad($applicationName, intval($matches[1]), ' ') : $applicationName; |
75
|
|
|
$message = preg_replace('/%(?:[0-9]{0,})c/', $applicationName, $message); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $message; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 日付項目を埋め込む |
83
|
|
|
* @param string メッセージ |
84
|
|
|
* @return 埋め込み済みメッセージ |
85
|
|
|
*/ |
86
|
|
|
private function compileDateTime($message) |
87
|
|
|
{ |
88
|
|
|
if (preg_match('/%([0-9]{0,})d(?:\{(.+?)\}){1}/', $message, $formatMatches)) { |
89
|
|
|
$message = preg_replace('/%[0-9]{0,}d/', '%d', $message); |
90
|
|
|
$now = microtime(true); |
91
|
|
|
$decimal = "000"; |
92
|
|
|
if (preg_match('/^[0-9]*\\.([0-9]+)$/', $now, $matches) === 1) { |
93
|
|
|
$decimal = str_pad(substr($matches[1], 0, 3), 3, "0"); |
94
|
|
|
} |
95
|
|
|
$dateTimeFormat = preg_replace('/(%f)/', $decimal, $formatMatches[2]); |
96
|
|
|
$dateTime = strftime($dateTimeFormat, $now); |
97
|
|
|
$dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' '); |
98
|
|
|
$message = preg_replace('/%d\{.+?\}/', $dateTime, $message); |
99
|
|
|
} elseif (preg_match('/%([0-9]{0,})d/', $message, $formatMatches)) { |
100
|
|
|
$message = preg_replace('/%[0-9]{0,}d/', '%d', $message); |
101
|
|
|
$dateTime = strftime($this->defaultDateTimeFormatter()); |
102
|
|
|
$dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' '); |
103
|
|
|
$message = preg_replace('/%d/', $dateTime, $message); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $message; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* ログレベル項目を埋め込む |
111
|
|
|
* @param string メッセージ |
112
|
|
|
* @param string ログレベル |
113
|
|
|
* @return 埋め込み済みメッセージ |
114
|
|
|
*/ |
115
|
|
|
private function compileLogLevel($message, $logLevel) |
116
|
|
|
{ |
117
|
|
|
// ログレベル |
118
|
|
View Code Duplication |
if (preg_match('/%([0-9]{0,})L/', $message, $matches)) { |
|
|
|
|
119
|
|
|
$upperLevel = strtoupper(empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' ')); |
120
|
|
|
$message = preg_replace('/%([0-9]{0,})L/', $upperLevel, $message); |
121
|
|
|
} |
122
|
|
View Code Duplication |
if (preg_match('/%([0-9]{0,})l/', $message, $matches)) { |
|
|
|
|
123
|
|
|
$lowerLevel = empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' '); |
124
|
|
|
$message = preg_replace('/%([0-9]{0,})l/', $lowerLevel, $message); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $message; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.