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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Piwik\Container\StaticContainer;
use Piwik\Db;
/**
* Logging utility class.
*
* Log entries are made with a message and log level. The logging utility will tag each
* log entry with the name of the plugin that's doing the logging. If no plugin is found,
* the name of the current class is used.
*
* You can log messages using one of the public static functions (eg, 'error', 'warning',
* 'info', etc.). Messages logged with the **error** level will **always** be logged to
* the screen, regardless of whether the [log] log_writer config option includes the
* screen writer.
*
* Currently, Piwik supports the following logging backends:
*
* - **screen**: logging to the screen
* - **file**: logging to a file
* - **database**: logging to Piwik's MySQL database
*
* ### Logging configuration
*
* The logging utility can be configured by manipulating the INI config options in the
* `[log]` section.
*
* The following configuration options can be set:
*
* - `log_writers[]`: This is an array of log writer IDs. The three log writers provided
* by Piwik core are **file**, **screen** and **database**. You can
* get more by installing plugins. The default value is **screen**.
* - `log_level`: The current log level. Can be **ERROR**, **WARN**, **INFO**, **DEBUG**,
* or **VERBOSE**. Log entries made with a log level that is as or more
* severe than the current log level will be outputted. Others will be
* ignored. The default level is **WARN**.
* - `log_only_when_cli`: 0 or 1. If 1, logging is only enabled when Piwik is executed
* in the command line (for example, by the core:archive command
* script). Default: 0.
* - `log_only_when_debug_parameter`: 0 or 1. If 1, logging is only enabled when the
* `debug` query parameter is 1. Default: 0.
* - `logger_file_path`: For the file log writer, specifies the path to the log file
* to log to or a path to a directory to store logs in. If a
* directory, the file name is piwik.log. Can be relative to
* Piwik's root dir or an absolute path. Defaults to **tmp/logs**.
*
* ### Custom message formatting
*
* If you'd like to format log messages differently for different backends, you can
* implement a new `Piwik\Log\Formatter\Formatter`.
*
* If you don't care about the backend when formatting an object, implement a `__toString()`
* in the custom class.
*
* ### Custom log writers
*
* New logging backends can be added via the {@hook Log.getAvailableWriters}` event. A log
* writer is just a callback that accepts log entry information (such as the message,
* level, etc.), so any backend could conceivably be used (including existing PSR3
* backends).
*
* ### Examples
*
* **Basic logging**
*
* Log::error("This log message will end up on the screen and in a file.")
* Log::verbose("This log message uses %s params, but %s will only be called if the"
* . " configured log level includes %s.", "sprintf", "sprintf", "verbose");
*
* **Logging objects**
*
* class MyDebugInfo
* {
* // ...
*
* public function __toString()
* {
* return // ...
* }
* }
*
* try {
* $myThirdPartyServiceClient->doSomething();
* } catch (Exception $unexpectedError) {
* $debugInfo = new MyDebugInfo($unexpectedError, $myThirdPartyServiceClient);
* Log::debug($debugInfo);
* }
*/
class Log extends Singleton
{
// log levels
const NONE = 0;
const ERROR = 1;
const WARN = 2;
const INFO = 3;
const DEBUG = 4;
const VERBOSE = 5;
// config option names
const LOG_LEVEL_CONFIG_OPTION = 'log_level';
const LOG_WRITERS_CONFIG_OPTION = 'log_writers';
const LOGGER_FILE_PATH_CONFIG_OPTION = 'logger_file_path';
const STRING_MESSAGE_FORMAT_OPTION = 'string_message_format';
const GET_AVAILABLE_WRITERS_EVENT = 'Log.getAvailableWriters';
/**
* The backtrace string to use when testing.
*
* @var string
*/
public static $debugBacktraceForTests;
/**
* Singleton instance.
*
* @var Log
*/
private static $instance;
/**
* The current logging level. Everything of equal or greater priority will be logged.
* Everything else will be ignored.
*
* @var int
*/
private $currentLogLevel = self::WARN;
/**
* Processors process log messages before they are being sent to backends.
*
* @var callable[]
*/
private $processors = array();
/**
* The array of handlers called when logging a message. Each handler writes a log
* message to a logging backend.
*
* @var HandlerInterface[]
*/
private $handlers = array();
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = StaticContainer::getContainer()->get(__CLASS__);
}
return self::$instance;
}
public static function unsetInstance()
{
self::$instance = null;
}
public static function setSingletonInstance($instance)
{
self::$instance = $instance;
}
/**
* @param HandlerInterface[] $handlers
* @param int $logLevel
* @param callable[] $processors
*/
public function __construct(array $handlers, $logLevel, array $processors)
{
$this->handlers = $handlers;
$this->currentLogLevel = $logLevel;
$this->processors = $processors;
}
/**
* Logs a message using the ERROR log level.
*
* _Note: Messages logged with the ERROR level are always logged to the screen in addition
* to configured writers._
*
* @param string $message The log message. This can be a sprintf format string.
* @param ... mixed Optional sprintf params.
* @api
*/
public static function error($message /* ... */)
{
self::logMessage(self::ERROR, $message, array_slice(func_get_args(), 1));
}
/**
* Logs a message using the WARNING log level.
*
* @param string $message The log message. This can be a sprintf format string.
* @param ... mixed Optional sprintf params.
* @api
*/
public static function warning($message /* ... */)
{
self::logMessage(self::WARN, $message, array_slice(func_get_args(), 1));
}
/**
* Logs a message using the INFO log level.
*
* @param string $message The log message. This can be a sprintf format string.
* @param ... mixed Optional sprintf params.
* @api
*/
public static function info($message /* ... */)
{
self::logMessage(self::INFO, $message, array_slice(func_get_args(), 1));
}
/**
* Logs a message using the DEBUG log level.
*
* @param string $message The log message. This can be a sprintf format string.
* @param ... mixed Optional sprintf params.
* @api
*/
public static function debug($message /* ... */)
{
self::logMessage(self::DEBUG, $message, array_slice(func_get_args(), 1));
}
/**
* Logs a message using the VERBOSE log level.
*
* @param string $message The log message. This can be a sprintf format string.
* @param ... mixed Optional sprintf params.
* @api
*/
public static function verbose($message /* ... */)
{
self::logMessage(self::VERBOSE, $message, array_slice(func_get_args(), 1));
}
/**
* @param int $logLevel
* @deprecated We should remove the log level from this class entirely
*/
public function setLogLevel($logLevel)
{
$this->currentLogLevel = $logLevel;
// Apply the log level to all handlers
$logLevel = self::getMonologLevel($logLevel);
foreach ($this->handlers as $handler) {
if ($handler instanceof AbstractHandler) {
$handler->setLevel($logLevel);
}
}
}
public function getLogLevel()
{
return $this->currentLogLevel;
}
private function doLog($level, $message, $parameters = array())
{
// To ensure the compatibility with PSR-3, the message must be a string
if ($message instanceof \Exception) {
$parameters['exception'] = $message;
$message = $message->getMessage();
}
// Create a record similar to Monolog to ease future transition
$record = array(
'message' => $message,
'context' => $parameters,
'channel' => 'piwik',
'level' => $this->getMonologLevel($level),
'level_name' => self::getStringLevel($level),
'time' => new \DateTime(),
'extra' => array(),
);
foreach ($this->processors as $processor) {
$record = $processor($record);
}
foreach ($this->handlers as $handler) {
if ($handler->isHandling($record)) {
$handler->handle($record);
}
}
}
private static function logMessage($level, $message, $parameters)
{
self::getInstance()->doLog($level, $message, $parameters);
}
private function getStringLevel($level)
{
static $levelToName = array(
Log::NONE => 'NONE',
Log::ERROR => 'ERROR',
Log::WARN => 'WARN',
Log::INFO => 'INFO',
Log::DEBUG => 'DEBUG',
Log::VERBOSE => 'VERBOSE'
);
return $levelToName[$level];
}
public static function getMonologLevel($level)
{
switch ($level) {
case self::ERROR:
return Logger::ERROR;
case self::WARN:
return Logger::WARNING;
case self::INFO:
return Logger::INFO;
case self::DEBUG:
return Logger::DEBUG;
case self::VERBOSE:
return Logger::DEBUG;
case self::NONE:
default:
// Highest level possible, need to do better in the future...
return Logger::EMERGENCY;
}
}
}
|