1 | <?php |
||
20 | abstract class BaseLogger implements Logger { |
||
21 | |||
22 | /** |
||
23 | * Messages above this level will not be logged. |
||
24 | * @var integer |
||
25 | */ |
||
26 | protected $threshold; |
||
27 | |||
28 | /** |
||
29 | * Array of defined logging levels and their associated human-readable form. |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $levels; |
||
33 | |||
34 | public function __construct() { |
||
50 | |||
51 | /** |
||
52 | * Specify a threshold for logging messages. |
||
53 | * @param integer $level new logging threshold level. |
||
54 | */ |
||
55 | public function setThreshold( $level ) { |
||
59 | |||
60 | /** |
||
61 | * Get the current threshold. |
||
62 | * @return integer |
||
63 | */ |
||
64 | public function getThreshold() { |
||
67 | |||
68 | /** |
||
69 | * System is unusable. |
||
70 | * |
||
71 | * @param string $msg |
||
72 | * @param array $context |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function emergency( $msg, array $context = [] ) { |
||
76 | return $this->log(LogLevel::EMERGENCY, $msg, $context); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Action must be taken immediately. |
||
81 | * |
||
82 | * Example: Entire website down, database unavailable, etc. This should |
||
83 | * trigger the SMS alerts and wake you up. |
||
84 | * |
||
85 | * @param string $msg |
||
86 | * @param array $context |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function alert( $msg, array $context = [] ) { |
||
90 | return $this->log(LogLevel::ALERT, $msg, $context); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Critical conditions. |
||
95 | * |
||
96 | * Example: Application component unavailable, unexpected exception. |
||
97 | * |
||
98 | * @param string $msg |
||
99 | * @param array $context |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function critical( $msg, array $context = [] ) { |
||
103 | return $this->log(LogLevel::CRITICAL, $msg, $context); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Runtime errors that do not require immediate action but should typically |
||
108 | * be logged and monitored. |
||
109 | * |
||
110 | * @param string $msg |
||
111 | * @param array $context |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function error( $msg, array $context = [] ) { |
||
115 | return $this->log(LogLevel::ERROR, $msg, $context); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Exceptional occurrences that are not errors. |
||
120 | * |
||
121 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
122 | * that are not necessarily wrong. |
||
123 | * |
||
124 | * @param string $msg |
||
125 | * @param array $context |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function warning( $msg, array $context = [] ) { |
||
129 | return $this->log(LogLevel::WARNING, $msg, $context); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Normal but significant events. |
||
134 | * |
||
135 | * @param string $msg |
||
136 | * @param array $context |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function notice( $msg, array $context = [] ) { |
||
140 | return $this->log(LogLevel::NOTICE, $msg, $context); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Interesting events. |
||
145 | * |
||
146 | * Example: User logs in, SQL logs. |
||
147 | * |
||
148 | * @param string $msg |
||
149 | * @param array $context |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function info( $msg, array $context = [] ) { |
||
153 | return $this->log(LogLevel::INFO, $msg, $context); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Detailed debug information. |
||
158 | * |
||
159 | * @param string $msg |
||
160 | * @param array $context |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function debug( $msg, array $context = [] ) { |
||
164 | return $this->log(LogLevel::DEBUG, $msg, $context); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Log a message of a specific level. |
||
169 | * @param integer $level one of the LOG_* constants. |
||
170 | * @param string $msg the message to log. |
||
171 | * @param array $context array of context information. |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function log( $level, $msg, array $context = [] ) { |
||
188 | |||
189 | /** |
||
190 | * Send the message to the logger's output. |
||
191 | * @param string $msg |
||
192 | * @param integer $level |
||
193 | */ |
||
194 | abstract protected function output( $level, $msg ); |
||
195 | |||
196 | /** |
||
197 | * Make sure we have a valid level. |
||
198 | * @param integer|string $level a |
||
199 | * @return integer |
||
200 | */ |
||
201 | protected function getLevel( $level ) { |
||
209 | |||
210 | /** |
||
211 | * Builds a nice error message including the level and current date/time. |
||
212 | * @param integer $level one of the LOG_* constants. |
||
213 | * @param string $msg the message to log. |
||
214 | * @param array $context array of strings to be interpolated into the message |
||
215 | */ |
||
216 | protected function buildMessage( $level, $msg, $context = [] ) { |
||
225 | |||
226 | /** |
||
227 | * Replace the token placeholders in a message with the corresponding values from the content array. |
||
228 | * @param string $msg the log message |
||
229 | * @param array $context the values to be interpolated into the log message |
||
230 | * @return string the combined log message |
||
231 | */ |
||
232 | protected function interpolate( $msg, array $context ) { |
||
239 | |||
240 | /** |
||
241 | * A simple text token to indicate the level of a logged message |
||
242 | * @param integer $level |
||
243 | * @return string |
||
244 | */ |
||
245 | protected function getPrefix( $level ) { |
||
258 | |||
259 | } |
||
260 | |||
261 | // EOF |