1 | <?php |
||
13 | class StaticLogger |
||
14 | { |
||
15 | /** |
||
16 | * Emergency: system is unusable |
||
17 | */ |
||
18 | const EMERGENCY = 'emergency'; |
||
19 | /** |
||
20 | * Alert: action must be taken immediately |
||
21 | */ |
||
22 | const ALERT = 'alert'; |
||
23 | /** |
||
24 | * Critical: critical conditions |
||
25 | */ |
||
26 | const CRITICAL = 'critical'; |
||
27 | /** |
||
28 | * Error: error conditions |
||
29 | */ |
||
30 | const ERROR = 'error'; |
||
31 | /** |
||
32 | * Warning: warning conditions |
||
33 | */ |
||
34 | const WARNING = 'warning'; |
||
35 | /** |
||
36 | * Notice: normal but significant condition |
||
37 | */ |
||
38 | const NOTICE = 'notice'; |
||
39 | /** |
||
40 | * Informational: informational messages |
||
41 | */ |
||
42 | const INFO = 'info'; |
||
43 | /** |
||
44 | * Debug: debug-level messages |
||
45 | */ |
||
46 | const DEBUG = 'debug'; |
||
47 | |||
48 | /** |
||
49 | * @var handler |
||
50 | */ |
||
51 | private static $handler; |
||
52 | private static $silence = false; |
||
53 | |||
54 | private static $valid_levels = array( |
||
55 | "EMERGENCY", |
||
56 | "ALERT", |
||
57 | "CRITICAL", |
||
58 | "ERROR", |
||
59 | "WARNING", |
||
60 | "NOTICE", |
||
61 | "INFO", |
||
62 | "DEBUG" |
||
63 | ); |
||
64 | |||
65 | /** |
||
66 | * Sets the handler (e.g. a File) to be used to log messages. |
||
67 | * |
||
68 | * @param handler $handler The handler to be used to write a log message. |
||
69 | */ |
||
70 | 16 | public static function setHandler(handler $handler) |
|
74 | |||
75 | /** |
||
76 | * @param string $level Must contain a valid log level: |
||
77 | * "emergency" |
||
78 | * "alert" |
||
79 | * "critical" |
||
80 | * "error" |
||
81 | * "warning" |
||
82 | * "notice" |
||
83 | * "info" |
||
84 | * "debug" |
||
85 | * @param string $message Message to be logged |
||
86 | * @param array $context PSR-3 context for log entry |
||
87 | * |
||
88 | * @return string Returns the log entry string. |
||
89 | */ |
||
90 | 31 | public static function log($level, $message, array $context = array()) |
|
113 | |||
114 | /** |
||
115 | * Generates a call to the log method given a supplied $level. |
||
116 | * |
||
117 | * @param string $level Must contain a valid log level: |
||
118 | * "emergency" |
||
119 | * "alert" |
||
120 | * "critical" |
||
121 | * "error" |
||
122 | * "warning" |
||
123 | * "notice" |
||
124 | * "info" |
||
125 | * "debug" |
||
126 | * Not specified inherently but rather derived from the overload. |
||
127 | * @param array $log_arguments The arguments passed to the log method by the __callStatic framework |
||
128 | * @return string The entry logged to the handler. |
||
129 | * |
||
130 | * @see StaticLogger::log |
||
131 | * @see StaticLogger::validateLogLevel |
||
132 | */ |
||
133 | 8 | public static function __callStatic($level, array $log_arguments) |
|
147 | |||
148 | /** |
||
149 | * Globally silences all logging. |
||
150 | */ |
||
151 | 2 | public static function silenceLogging() |
|
155 | |||
156 | /** |
||
157 | * Globally unsliences all logging. |
||
158 | */ |
||
159 | 19 | public static function unSilenceLogging() |
|
163 | |||
164 | /** |
||
165 | * Returns the current state of the StaticLogger's "silence" property |
||
166 | * |
||
167 | * @return bool The current state of silence |
||
168 | */ |
||
169 | 3 | public static function getSilence() |
|
173 | |||
174 | /** |
||
175 | * Validates that a given $level is a valid PSR-3 log level |
||
176 | * |
||
177 | * @param string $level The log level being validated |
||
178 | * @throws \InvalidArgumentException Generates an Invalid Argument Exception if the supplied level is invalid |
||
179 | * |
||
180 | * @see StaticLogger::valid_levels |
||
181 | */ |
||
182 | 31 | private static function validateLogLevel($level) |
|
190 | |||
191 | /** |
||
192 | * Processes context as supplied by the log method, replacing templated strings with data from the context array |
||
193 | * or processing exception data via checkContextException. |
||
194 | * |
||
195 | * Context array elements with the "exception" key are processed by pulling Exception line, file, and message into |
||
196 | * the log message provided appropriate templates within the message: {line}, {file}, and {message}, respectively. |
||
197 | * Note that any line, file, or message templates provided outside of an exception will be overwritten by context |
||
198 | * processing, and so the order in which the context array data are stacked is relevant. |
||
199 | * |
||
200 | * |
||
201 | * |
||
202 | * @param string $message The original log message to be processed with context. |
||
203 | * @param array $context An array of key => value pairs with additional data to be inserted into the log |
||
204 | * message provided {templated text} (i.e. surrounded by curly braces. |
||
205 | * |
||
206 | * @return string The processed log message |
||
207 | */ |
||
208 | 30 | private static function processContext($message, array $context = array()) |
|
230 | |||
231 | /** |
||
232 | * Determines whether a context array contains an Exception. |
||
233 | * |
||
234 | * @param array $context PSR-3 context array. |
||
235 | * @return bool Returns true if the context array contains an element identified by an "exception" key |
||
236 | * AND the value that corresponds with the "exception" key is an Exception. |
||
237 | * Returns false otherwise. |
||
238 | */ |
||
239 | 30 | private static function checkContextException(array $context = array()) |
|
249 | } |
||
250 |