| 199 |
lars |
1 |
<?php namespace Clockwork\Request;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Helpers\Serializer;
|
|
|
4 |
use Clockwork\Helpers\StackTrace;
|
|
|
5 |
use Clockwork\Helpers\StackFilter;
|
|
|
6 |
|
|
|
7 |
// Data structure representing a log with timestamped messages
|
|
|
8 |
class Log
|
|
|
9 |
{
|
|
|
10 |
// Array of logged messages
|
|
|
11 |
public $messages = [];
|
|
|
12 |
|
|
|
13 |
// Create a new log, optionally with existing messages
|
|
|
14 |
public function __construct($messages = [])
|
|
|
15 |
{
|
|
|
16 |
$this->messages = $messages;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
// Log a new message, with a level and context, context can be used to override serializer defaults,
|
|
|
20 |
// $context['trace'] = true can be used to force collecting a stack trace
|
|
|
21 |
public function log($level = LogLevel::INFO, $message = null, array $context = [])
|
|
|
22 |
{
|
|
|
23 |
$trace = $this->hasTrace($context) ? $context['trace'] : StackTrace::get()->resolveViewName();
|
|
|
24 |
|
|
|
25 |
$this->messages[] = [
|
|
|
26 |
'message' => (new Serializer($context))->normalize($message),
|
|
|
27 |
'exception' => $this->formatException($context),
|
|
|
28 |
'context' => $this->formatContext($context),
|
|
|
29 |
'level' => $level,
|
|
|
30 |
'time' => microtime(true),
|
|
|
31 |
'trace' => (new Serializer(! empty($context['trace']) ? [ 'traces' => true ] : []))->trace($trace)
|
|
|
32 |
];
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function emergency($message, array $context = [])
|
|
|
36 |
{
|
|
|
37 |
$this->log(LogLevel::EMERGENCY, $message, $context);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function alert($message, array $context = [])
|
|
|
41 |
{
|
|
|
42 |
$this->log(LogLevel::ALERT, $message, $context);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function critical($message, array $context = [])
|
|
|
46 |
{
|
|
|
47 |
$this->log(LogLevel::CRITICAL, $message, $context);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function error($message, array $context = [])
|
|
|
51 |
{
|
|
|
52 |
$this->log(LogLevel::ERROR, $message, $context);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function warning($message, array $context = [])
|
|
|
56 |
{
|
|
|
57 |
$this->log(LogLevel::WARNING, $message, $context);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function notice($message, array $context = [])
|
|
|
61 |
{
|
|
|
62 |
$this->log(LogLevel::NOTICE, $message, $context);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public function info($message, array $context = [])
|
|
|
66 |
{
|
|
|
67 |
$this->log(LogLevel::INFO, $message, $context);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function debug($message, array $context = [])
|
|
|
71 |
{
|
|
|
72 |
$this->log(LogLevel::DEBUG, $message, $context);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Merge another log instance into the current log
|
|
|
76 |
public function merge(Log $log)
|
|
|
77 |
{
|
|
|
78 |
$this->messages = array_merge($this->messages, $log->messages);
|
|
|
79 |
|
|
|
80 |
return $this;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Sort the log messages by timestamp
|
|
|
84 |
public function sort()
|
|
|
85 |
{
|
|
|
86 |
usort($this->messages, function ($a, $b) { return $a['time'] * 1000 - $b['time'] * 1000; });
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// Get all messages as an array
|
|
|
90 |
public function toArray()
|
|
|
91 |
{
|
|
|
92 |
return $this->messages;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Format message context, removes exception and trace if we are serializing them
|
|
|
96 |
protected function formatContext($context)
|
|
|
97 |
{
|
|
|
98 |
if ($this->hasException($context)) unset($context['exception']);
|
|
|
99 |
if ($this->hasTrace($context)) unset($context['trace']);
|
|
|
100 |
|
|
|
101 |
return (new Serializer)->normalize($context);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Format exception if present in the context
|
|
|
105 |
protected function formatException($context)
|
|
|
106 |
{
|
|
|
107 |
if ($this->hasException($context)) {
|
|
|
108 |
return (new Serializer)->exception($context['exception']);
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Check if context has serializable trace
|
|
|
113 |
protected function hasTrace($context)
|
|
|
114 |
{
|
|
|
115 |
return ! empty($context['trace']) && $context['trace'] instanceof StackTrace && empty($context['raw']);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// Check if context has serializable exception
|
|
|
119 |
protected function hasException($context)
|
|
|
120 |
{
|
|
|
121 |
return ! empty($context['exception'])
|
|
|
122 |
&& ($context['exception'] instanceof \Throwable || $context['exception'] instanceof \Exception)
|
|
|
123 |
&& empty($context['raw']);
|
|
|
124 |
}
|
|
|
125 |
}
|