API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Log.php
257 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Monolog\Logger; |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | |
| 15 | /** |
| 16 | * Logging utility class. |
| 17 | * |
| 18 | * Log entries are made with a message and log level. The logging utility will tag each |
| 19 | * log entry with the name of the plugin that's doing the logging. If no plugin is found, |
| 20 | * the name of the current class is used. |
| 21 | * |
| 22 | * You can log messages using one of the public static functions (eg, 'error', 'warning', |
| 23 | * 'info', etc.). |
| 24 | * |
| 25 | * Currently, Piwik supports the following logging backends: |
| 26 | * |
| 27 | * - **screen**: logging to the screen |
| 28 | * - **file**: logging to a file |
| 29 | * - **database**: logging to Piwik's MySQL database |
| 30 | * |
| 31 | * Messages logged in the console will always be logged to the console output. |
| 32 | * |
| 33 | * ### Logging configuration |
| 34 | * |
| 35 | * The logging utility can be configured by manipulating the INI config options in the |
| 36 | * `[log]` section. |
| 37 | * |
| 38 | * The following configuration options can be set: |
| 39 | * |
| 40 | * - `log_writers[]`: This is an array of log writer IDs. The three log writers provided |
| 41 | * by Piwik core are **file**, **screen** and **database**. You can |
| 42 | * get more by installing plugins. The default value is **screen**. |
| 43 | * - `log_level`: The current log level. Can be **ERROR**, **WARN**, **INFO**, **DEBUG**, |
| 44 | * or **VERBOSE**. Log entries made with a log level that is as or more |
| 45 | * severe than the current log level will be outputted. Others will be |
| 46 | * ignored. The default level is **WARN**. |
| 47 | * - `logger_file_path`: For the file log writer, specifies the path to the log file |
| 48 | * to log to or a path to a directory to store logs in. If a |
| 49 | * directory, the file name is piwik.log. Can be relative to |
| 50 | * Piwik's root dir or an absolute path. Defaults to **tmp/logs**. |
| 51 | * |
| 52 | * |
| 53 | * @deprecated Inject and use Psr\Log\LoggerInterface instead of this class. |
| 54 | * @see \Psr\Log\LoggerInterface |
| 55 | */ |
| 56 | class Log extends Singleton |
| 57 | { |
| 58 | // log levels |
| 59 | const NONE = 0; |
| 60 | const ERROR = 1; |
| 61 | const WARN = 2; |
| 62 | const INFO = 3; |
| 63 | const DEBUG = 4; |
| 64 | const VERBOSE = 5; |
| 65 | |
| 66 | // config option names |
| 67 | const LOG_LEVEL_CONFIG_OPTION = 'log_level'; |
| 68 | const LOG_WRITERS_CONFIG_OPTION = 'log_writers'; |
| 69 | const LOGGER_FILE_PATH_CONFIG_OPTION = 'logger_file_path'; |
| 70 | const STRING_MESSAGE_FORMAT_OPTION = 'string_message_format'; |
| 71 | |
| 72 | /** |
| 73 | * The backtrace string to use when testing. |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
| 77 | public static $debugBacktraceForTests; |
| 78 | |
| 79 | /** |
| 80 | * Singleton instance. |
| 81 | * |
| 82 | * @var Log |
| 83 | */ |
| 84 | private static $instance; |
| 85 | |
| 86 | /** |
| 87 | * @var LoggerInterface |
| 88 | */ |
| 89 | private $logger; |
| 90 | |
| 91 | public static function getInstance() |
| 92 | { |
| 93 | if (self::$instance === null) { |
| 94 | self::$instance = StaticContainer::get(__CLASS__); |
| 95 | } |
| 96 | return self::$instance; |
| 97 | } |
| 98 | public static function unsetInstance() |
| 99 | { |
| 100 | self::$instance = null; |
| 101 | } |
| 102 | public static function setSingletonInstance($instance) |
| 103 | { |
| 104 | self::$instance = $instance; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param LoggerInterface $logger |
| 109 | */ |
| 110 | public function __construct(LoggerInterface $logger) |
| 111 | { |
| 112 | $this->logger = $logger; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Logs a message using the ERROR log level. |
| 117 | * |
| 118 | * @param string $message The log message. This can be a sprintf format string. |
| 119 | * @param ... mixed Optional sprintf params. |
| 120 | * @api |
| 121 | * |
| 122 | * @deprecated Inject and call Psr\Log\LoggerInterface::error() instead. |
| 123 | * @see \Psr\Log\LoggerInterface::error() |
| 124 | */ |
| 125 | public static function error($message /* ... */) |
| 126 | { |
| 127 | self::logMessage(Logger::ERROR, $message, array_slice(func_get_args(), 1)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Logs a message using the WARNING log level. |
| 132 | * |
| 133 | * @param string $message The log message. This can be a sprintf format string. |
| 134 | * @param ... mixed Optional sprintf params. |
| 135 | * @api |
| 136 | * |
| 137 | * @deprecated Inject and call Psr\Log\LoggerInterface::warning() instead. |
| 138 | * @see \Psr\Log\LoggerInterface::warning() |
| 139 | */ |
| 140 | public static function warning($message /* ... */) |
| 141 | { |
| 142 | self::logMessage(Logger::WARNING, $message, array_slice(func_get_args(), 1)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Logs a message using the INFO log level. |
| 147 | * |
| 148 | * @param string $message The log message. This can be a sprintf format string. |
| 149 | * @param ... mixed Optional sprintf params. |
| 150 | * @api |
| 151 | * |
| 152 | * @deprecated Inject and call Psr\Log\LoggerInterface::info() instead. |
| 153 | * @see \Psr\Log\LoggerInterface::info() |
| 154 | */ |
| 155 | public static function info($message /* ... */) |
| 156 | { |
| 157 | self::logMessage(Logger::INFO, $message, array_slice(func_get_args(), 1)); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Logs a message using the DEBUG log level. |
| 162 | * |
| 163 | * @param string $message The log message. This can be a sprintf format string. |
| 164 | * @param ... mixed Optional sprintf params. |
| 165 | * @api |
| 166 | * |
| 167 | * @deprecated Inject and call Psr\Log\LoggerInterface::debug() instead. |
| 168 | * @see \Psr\Log\LoggerInterface::debug() |
| 169 | */ |
| 170 | public static function debug($message /* ... */) |
| 171 | { |
| 172 | self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1)); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Logs a message using the VERBOSE log level. |
| 177 | * |
| 178 | * @param string $message The log message. This can be a sprintf format string. |
| 179 | * @param ... mixed Optional sprintf params. |
| 180 | * @api |
| 181 | * |
| 182 | * @deprecated Inject and call Psr\Log\LoggerInterface::debug() instead (the verbose level doesn't exist in the PSR standard). |
| 183 | * @see \Psr\Log\LoggerInterface::debug() |
| 184 | */ |
| 185 | public static function verbose($message /* ... */) |
| 186 | { |
| 187 | self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1)); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param int $logLevel |
| 192 | * @deprecated Will be removed, log levels are now applied on each Monolog handler. |
| 193 | */ |
| 194 | public function setLogLevel($logLevel) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @deprecated Will be removed, log levels are now applied on each Monolog handler. |
| 200 | */ |
| 201 | public function getLogLevel() |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | private function doLog($level, $message, $parameters = array()) |
| 206 | { |
| 207 | // To ensure the compatibility with PSR-3, the message must be a string |
| 208 | if ($message instanceof \Exception) { |
| 209 | $parameters['exception'] = $message; |
| 210 | $message = $message->getMessage(); |
| 211 | } |
| 212 | |
| 213 | if (is_object($message) || is_array($message) || is_resource($message)) { |
| 214 | $this->logger->warning('Trying to log a message that is not a string', array( |
| 215 | 'exception' => new \InvalidArgumentException('Trying to log a message that is not a string') |
| 216 | )); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | $this->logger->log($level, $message, $parameters); |
| 221 | } |
| 222 | |
| 223 | private static function logMessage($level, $message, $parameters) |
| 224 | { |
| 225 | self::getInstance()->doLog($level, $message, $parameters); |
| 226 | } |
| 227 | |
| 228 | public static function getMonologLevel($level) |
| 229 | { |
| 230 | switch ($level) { |
| 231 | case self::ERROR: |
| 232 | return Logger::ERROR; |
| 233 | case self::WARN: |
| 234 | return Logger::WARNING; |
| 235 | case self::INFO: |
| 236 | return Logger::INFO; |
| 237 | case self::DEBUG: |
| 238 | return Logger::DEBUG; |
| 239 | case self::VERBOSE: |
| 240 | return Logger::DEBUG; |
| 241 | case self::NONE: |
| 242 | default: |
| 243 | // Highest level possible, need to do better in the future... |
| 244 | return Logger::EMERGENCY; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public static function getMonologLevelIfValid($level) |
| 249 | { |
| 250 | $level = strtoupper($level); |
| 251 | if (!empty($level) && defined('Piwik\Log::'.strtoupper($level))) { |
| 252 | return self::getMonologLevel(constant('Piwik\Log::'.strtoupper($level))); |
| 253 | } |
| 254 | return null; |
| 255 | } |
| 256 | } |
| 257 |