|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Bluz\Logger; |
|
12
|
|
|
|
|
13
|
|
|
use Bluz\Common\Options; |
|
14
|
|
|
use Psr\Log\AbstractLogger; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Logger |
|
18
|
|
|
* |
|
19
|
|
|
* @package Bluz\Logger |
|
20
|
|
|
* @author Taras Omelianenko <[email protected]> |
|
21
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Logger |
|
22
|
|
|
*/ |
|
23
|
|
|
class Logger extends AbstractLogger |
|
24
|
|
|
{ |
|
25
|
|
|
use Options; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var float start time |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $startTime; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var float part time |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $timer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var integer |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $memory = 0; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array list of alerts |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $alert = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array list of critical |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $critical = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array list of debug messages |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $debug = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var array list of emergency |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $emergency = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array list of errors |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $error = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var array list of info |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $info = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var array list of notices |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $notice = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var array list of warnings |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $warning = []; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Interpolates context values into the message placeholders |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $message |
|
86
|
|
|
* @param array $context |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
579 |
|
protected function interpolate($message, array $context = []) : string |
|
91
|
|
|
{ |
|
92
|
|
|
// build a replacement array with braces around the context keys |
|
93
|
579 |
|
$replace = []; |
|
94
|
579 |
|
foreach ($context as $key => $val) { |
|
95
|
|
|
$replace['{' . $key . '}'] = $val; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// interpolate replacement values into the message and return |
|
99
|
579 |
|
return strtr($message, $replace); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Log info message |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $message |
|
106
|
|
|
* @param array $context |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
579 |
|
public function info($message, array $context = []) |
|
111
|
|
|
{ |
|
112
|
579 |
|
$message = $this->interpolate($message, $context); |
|
113
|
|
|
|
|
114
|
579 |
|
if (!$this->startTime) { |
|
115
|
579 |
|
$this->startTime = $this->timer = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
579 |
|
$curTimer = microtime(true); |
|
119
|
579 |
|
$curMemory = memory_get_usage(); |
|
120
|
|
|
|
|
121
|
579 |
|
$key = sprintf( |
|
122
|
579 |
|
'%f :: %f :: %d', |
|
123
|
579 |
|
$curTimer - $this->startTime, |
|
124
|
579 |
|
$curTimer - $this->timer, |
|
125
|
579 |
|
$curMemory - $this->memory |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
579 |
|
$this->info[$key] = $message; |
|
129
|
|
|
|
|
130
|
579 |
|
$this->timer = $curTimer; |
|
131
|
579 |
|
$this->memory = $curMemory; |
|
132
|
579 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Logs with an arbitrary level |
|
136
|
|
|
* |
|
137
|
|
|
* @param mixed $level |
|
138
|
|
|
* @param string $message |
|
139
|
|
|
* @param array $context |
|
140
|
|
|
* |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
6 |
|
public function log($level, $message, array $context = []) |
|
144
|
|
|
{ |
|
145
|
6 |
|
$this->{$level}[] = $this->interpolate($message, $context); |
|
146
|
6 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get logs records by level |
|
150
|
|
|
* |
|
151
|
|
|
* @param $level |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function get($level) |
|
156
|
|
|
{ |
|
157
|
1 |
|
return $this->{$level}; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|