1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Log; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Anax default logger instance |
7
|
|
|
* |
8
|
|
|
* The message MUST be a string or object implementing __toString(). |
9
|
|
|
* |
10
|
|
|
* The message MAY contain placeholders in the form: {foo} where foo |
11
|
|
|
* will be replaced by the context data in key "foo". |
12
|
|
|
* |
13
|
|
|
* The context array can contain arbitrary data, the only assumption that |
14
|
|
|
* can be made by implementors is that if an Exception instance is given |
15
|
|
|
* to produce a stack trace, it MUST be in a key named "exception". |
16
|
|
|
* |
17
|
|
|
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
18
|
|
|
* for the full interface specification. |
19
|
|
|
*/ |
20
|
|
|
class Logger |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constans for loglevel. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
const EMERGENCY = 'emergency'; |
28
|
|
|
const ALERT = 'alert'; |
29
|
|
|
const CRITICAL = 'critical'; |
30
|
|
|
const ERROR = 'error'; |
31
|
|
|
const WARNING = 'warning'; |
32
|
|
|
const NOTICE = 'notice'; |
33
|
|
|
const INFO = 'info'; |
34
|
|
|
const DEBUG = 'debug'; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constans for loglevel. |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
private $context = 'development'; // production, development or debug |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Init the logger depending on its context production, development or debug. |
48
|
|
|
* |
49
|
|
|
* @param string $context as production, development or debug, default is development |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setContext($context = 'development') |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
switch ($context) { |
56
|
|
|
case 'production': |
57
|
|
|
break; |
58
|
|
|
|
59
|
|
|
case 'development': |
60
|
|
|
error_reporting(-1); // Report all type of errors |
61
|
|
|
ini_set('display_errors', 1); // Display all errors |
62
|
|
|
ini_set('output_buffering', 0); // Do not buffer output |
63
|
|
|
|
64
|
|
|
set_exception_handler(function ($exception) { |
65
|
|
|
echo "Anax: Uncaught exception: <p>" . |
66
|
|
|
$exception->getMessage() . "</p><pre>" . |
67
|
|
|
$exception->getTraceAsString() . "</pre>"; |
68
|
|
|
}); |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'debug': |
72
|
|
|
break; |
73
|
|
|
|
74
|
|
|
default: |
75
|
|
|
throw new \Exception('Unknown context.'); |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->context = $context; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Logs with an arbitrary level. |
87
|
|
|
* |
88
|
|
|
* @param mixed $level |
89
|
|
|
* @param string $message |
90
|
|
|
* @param array $context |
91
|
|
|
* @return null |
92
|
|
|
*/ |
93
|
|
|
public function log($level, $message, array $context = []) |
94
|
|
|
{ |
95
|
|
|
echo "Level: " . $level . "<br>" |
96
|
|
|
. "Message: " . $message . "<br>" |
97
|
|
|
. htmlentities(print_r($context, 1)) . "<br>"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* System is unusable. |
104
|
|
|
* |
105
|
|
|
* @param string $message |
106
|
|
|
* @param array $context |
107
|
|
|
* @return null |
108
|
|
|
*/ |
109
|
|
|
public function emergency($message, array $context = array()) |
110
|
|
|
{ |
111
|
|
|
$this->log(self::EMERGENCY, $message, $context); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Action must be taken immediately. |
118
|
|
|
* |
119
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
120
|
|
|
* trigger the SMS alerts and wake you up. |
121
|
|
|
* |
122
|
|
|
* @param string $message |
123
|
|
|
* @param array $context |
124
|
|
|
* @return null |
125
|
|
|
*/ |
126
|
|
|
public function alert($message, array $context = array()) |
127
|
|
|
{ |
128
|
|
|
$this->log(self::ALERT, $message, $context); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Critical conditions. |
135
|
|
|
* |
136
|
|
|
* Example: Application component unavailable, unexpected exception. |
137
|
|
|
* |
138
|
|
|
* @param string $message |
139
|
|
|
* @param array $context |
140
|
|
|
* @return null |
141
|
|
|
*/ |
142
|
|
|
public function critical($message, array $context = array()) |
143
|
|
|
{ |
144
|
|
|
$this->log(self::CRITICAL, $message, $context); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Runtime errors that do not require immediate action but should typically |
151
|
|
|
* be logged and monitored. |
152
|
|
|
* |
153
|
|
|
* @param string $message |
154
|
|
|
* @param array $context |
155
|
|
|
* @return null |
156
|
|
|
*/ |
157
|
|
|
public function error($message, array $context = array()) |
158
|
|
|
{ |
159
|
|
|
$this->log(self::ERROR, $message, $context); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Exceptional occurrences that are not errors. |
166
|
|
|
* |
167
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
168
|
|
|
* that are not necessarily wrong. |
169
|
|
|
* |
170
|
|
|
* @param string $message |
171
|
|
|
* @param array $context |
172
|
|
|
* @return null |
173
|
|
|
*/ |
174
|
|
|
public function warning($message, array $context = array()) |
175
|
|
|
{ |
176
|
|
|
$this->log(self::WARNING, $message, $context); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Normal but significant events. |
183
|
|
|
* |
184
|
|
|
* @param string $message |
185
|
|
|
* @param array $context |
186
|
|
|
* @return null |
187
|
|
|
*/ |
188
|
|
|
public function notice($message, array $context = array()) |
189
|
|
|
{ |
190
|
|
|
$this->log(self::NOTICE, $message, $context); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Interesting events. |
197
|
|
|
* |
198
|
|
|
* Example: User logs in, SQL logs. |
199
|
|
|
* |
200
|
|
|
* @param string $message |
201
|
|
|
* @param array $context |
202
|
|
|
* @return null |
203
|
|
|
*/ |
204
|
|
|
public function info($message, array $context = array()) |
205
|
|
|
{ |
206
|
|
|
$this->log(self::INFO, $message, $context); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Detailed debug information. |
213
|
|
|
* |
214
|
|
|
* @param string $message |
215
|
|
|
* @param array $context |
216
|
|
|
* @return null |
217
|
|
|
*/ |
218
|
|
|
public function debug($message, array $context = array()) |
219
|
|
|
{ |
220
|
|
|
$this->log(self::DEBUG, $message, $context); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|