forked from markrogoyski/simplelog-php
/
Logger.php
391 lines (359 loc) · 12.1 KB
/
Logger.php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace SimpleLog;
use Psr\Log\LogLevel;
/**
* Simple Logger
* Powerful PSR-3 logging so easy it's simple!
*
* Implements PHP Standard Recommendation interface: PSR-3 \Psr\Log\LoggerInterface
*
* Log the following severities: debug, info, notice, warning, error, critical, alert, emergency.
* Log format: YYYY-mm-dd HH:ii:ss.uuuuuu [loglevel] [channel] [pid:##] Log message content {"Optional":"JSON Contextual Support Data"} {"Optional":"Exception Data"}
*
* Standard usage - default options:
* $logger = new SimpleLog\Logger('logfile.log', 'channelname');
* $logger->info('Normal informational event happened.');
* $logger->error('Something bad happened.', ['key1' => 'value that gives context', 'key2' => 'some more context', 'exception' => $e]);
*
* Optional constructor option: Set default lowest log level (Example error and above):
* $logger = new SimpleLog\Logger('logfile.log', 'channelname', \Psr\Log\LogLevel::ERROR);
* $logger->error('This will get logged');
* $logger->info('This is below the minimum log level and will not get logged');
*
* To log an exception, set as data context array key 'exception'
* $logger->error('Something exceptional happened.', ['exception' => $e]);
*
* To set output to standard out (STDOUT) as well as a log file:
* $logger->setOutput(true);
*
* To change the channel after construction:
* $logger->setChannel('newname')
*/
class Logger implements \Psr\Log\LoggerInterface
{
/**
* File name and path of log file.
* @var string
*/
private $log_file;
/**
* Log channel--namespace for log lines.
* Used to identify and correlate groups of similar log lines.
* @var string
*/
private $channel;
/**
* Lowest log level to log.
* @var int
*/
private $log_level;
/**
* Whether to log to standard out.
* @var bool
*/
private $stdout;
/**
* Log fields separated by tabs to form a TSV (CSV with tabs).
*/
const TAB = "\t";
/**
* Special minimum log level which will not log any log levels.
*/
const LOG_LEVEL_NONE = 'none';
/**
* Log level hierachy
*/
protected static $levels = [
self::LOG_LEVEL_NONE => -1,
LogLevel::DEBUG => 0,
LogLevel::INFO => 1,
LogLevel::NOTICE => 2,
LogLevel::WARNING => 3,
LogLevel::ERROR => 4,
LogLevel::CRITICAL => 5,
LogLevel::ALERT => 6,
LogLevel::EMERGENCY => 7,
];
/**
* Logger constructor
*
* @param string $log_file File name and path of log file.
* @param string $channel Logger channel associated with this logger.
* @param string $log_level (optional) Lowest log level to log.
*/
public function __construct($log_file, $channel, $log_level = LogLevel::DEBUG)
{
$this->log_file = $log_file;
$this->channel = $channel;
$this->stdout = false;
$this->setLogLevel($log_level);
}
/**
* Set the lowest log level to log.
*
* @param string $log_level
*/
public function setLogLevel($log_level)
{
if (!array_key_exists($log_level, self::$levels)) {
throw new \DomainException("Log level $log_level is not a valid log level. Must be one of (" . implode(', ', array_keys(self::$levels)) . ')');
}
$this->log_level = self::$levels[$log_level];
}
/**
* Set the log channel which identifies the log line.
*
* @param string $channel
*/
public function setChannel($channel)
{
$this->channel = $channel;
}
/**
* Set the standard out option on or off.
* If set to true, log lines will also be printed to standard out.
*
* @param bool $stdout
*/
public function setOutput($stdout)
{
$this->stdout = $stdout;
}
/**
* Log a debug message.
* Fine-grained informational events that are most useful to debug an application.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function debug($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::DEBUG)) {
$this->log(LogLevel::DEBUG, $message, $data);
}
}
/**
* Log an info message.
* Interesting events and informational messages that highlight the progress of the application at coarse-grained level.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*/
public function info($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::INFO)) {
$this->log(LogLevel::INFO, $message, $data);
}
}
/**
* Log an notice message.
* Normal but significant events.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function notice($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::NOTICE)) {
$this->log(LogLevel::NOTICE, $message, $data);
}
}
/**
* Log a warning message.
* Exceptional occurrences that are not errors--undesirable things that are not necessarily wrong.
* Potentially harmful situations which still allow the application to continue running.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function warning($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::WARNING)) {
$this->log(LogLevel::WARNING, $message, $data);
}
}
/**
* Log an error message.
* Error events that might still allow the application to continue running.
* Runtime errors that do not require immediate action but should typically be logged and monitored.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*/
public function error($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::ERROR)) {
$this->log(LogLevel::ERROR, $message, $data);
}
}
/**
* Log a critical condition.
* Application components being unavailable, unexpected exceptions, etc.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function critical($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::CRITICAL)) {
$this->log(LogLevel::CRITICAL, $message, $data);
}
}
/**
* Log an alert.
* This should trigger an email or SMS alert and wake you up.
* Example: Entire site down, database unavailable, etc.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function alert($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::ALERT)) {
$this->log(LogLevel::ALERT, $message, $data);
}
}
/**
* Log an emergency.
* System is unsable.
* This should trigger an email or SMS alert and wake you up.
*
* @param string $message Content of log event.
* @param array $data Associative array of contextual support data that goes with the log event.
*
* @throws \Exception
*/
public function emergency($message = '', array $data = null)
{
if ($this->logAtThisLevel(LogLevel::EMERGENCY)) {
$this->log(LogLevel::EMERGENCY, $message, $data);
}
}
/**
* Log a message.
* Generic log routine that all severity levels use to log an event.
*
* @param string $level
* @param string $message Content of log event.
* @param array $data Potentially multidimensional associative array of support data that goes with the log event.
*
*/
public function log($level, $message = '', array $data = null)
{
try {
// Build log line
$time = $this->getTime();
$pid = getmypid();
list($exception, $data) = $this->handleException($data);
$dataString = $data ? json_encode($data, \JSON_UNESCAPED_SLASHES) : '{}';
$log_line = $this->formatLogLine($level, $pid, $message, $dataString, $exception);
// Log to file
$fh = fopen($this->log_file, 'a');
fwrite($fh, $log_line);
fclose($fh);
// Log to stdout if option set to do so.
if ($this->stdout) {
print($log_line);
}
} catch (\Throwable $e) {
throw new \RuntimeException("Could not open log file {$this->log_file} for writing to SimpleLog channel {$this->channel}!", 0, $e);
}
}
/**
* Determine if the logger should log at a certain log level.
*
* @param string $level
*
* @return bool True if we log at this level; false otherwise.
*/
private function logAtThisLevel($level)
{
return self::$levels[$level] >= $this->log_level;
}
/**
* Handle an exception in the data context array.
* If an exception is included in the data context array, extract it.
*
* @param array $data
*
* @return array [exception, data (without exception)]
*/
private function handleException(array $data = null)
{
if (isset($data['exception']) && $data['exception'] instanceof \Throwable) {
$exception = $data['exception'];
$exception_data = $this->buildExceptionData($exception);
unset($data['exception']);
} else {
$exception_data = '{}';
}
return [$exception_data, $data];
}
/**
* Build the exception log data.
*
* @param \Throwable $e
*
* @return string JSON {message, code, file, line, trace}
*/
private function buildExceptionData(\Throwable $e)
{
return json_encode(
[
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace()
],
\JSON_UNESCAPED_SLASHES
);
}
/**
* Format the log line.
* YYYY-mm-dd HH:ii:ss.uuuuuu [loglevel] [channel] [pid:##] Log message content {"Optional":"JSON Contextual Support Data"} {"Optional":"Exception Data"}
*
* @param string $level
* @param int $pid
* @param string $message
* @param string $data
* @param string $exception_data
*
* @return string
*/
private function formatLogLine($level, $pid, $message, $data, $exception_data)
{
return
$this->getTime() . self::TAB .
"[$level]" . self::TAB .
"[{$this->channel}]" . self::TAB .
"[pid:$pid]" . self::TAB .
str_replace(\PHP_EOL, ' ', trim($message)) . self::TAB .
str_replace(\PHP_EOL, ' ', $data) . self::TAB .
str_replace(\PHP_EOL, ' ', $exception_data) . \PHP_EOL;
}
/**
* Get current date time.
* Format: YYYY-mm-dd HH:ii:ss.uuuuuu
* Microsecond precision.
*
* @return string Date time
*/
private function getTime()
{
$microtime = microtime(true);
$microtime_formated = sprintf("%06d", ($microtime - floor($microtime)) * 1000000);
$dt = new \DateTime(date('Y-m-d H:i:s.' . $microtime_formated, $microtime));
return $dt->format('Y-m-d H:i:s.u');
}
}