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