/analytics/core/Log.php

https://gitlab.com/yasminmostfa/thomas-site · PHP · 247 lines · 102 code · 21 blank · 124 comment · 6 complexity · 5640acd6c1345cc951501822ad7a2d3e MD5 · raw file

  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. namespace Piwik;
  9. use Monolog\Logger;
  10. use Piwik\Container\StaticContainer;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * Logging utility class.
  14. *
  15. * Log entries are made with a message and log level. The logging utility will tag each
  16. * log entry with the name of the plugin that's doing the logging. If no plugin is found,
  17. * the name of the current class is used.
  18. *
  19. * You can log messages using one of the public static functions (eg, 'error', 'warning',
  20. * 'info', etc.).
  21. *
  22. * Currently, Piwik supports the following logging backends:
  23. *
  24. * - **screen**: logging to the screen
  25. * - **file**: logging to a file
  26. * - **database**: logging to Piwik's MySQL database
  27. *
  28. * Messages logged in the console will always be logged to the console output.
  29. *
  30. * ### Logging configuration
  31. *
  32. * The logging utility can be configured by manipulating the INI config options in the
  33. * `[log]` section.
  34. *
  35. * The following configuration options can be set:
  36. *
  37. * - `log_writers[]`: This is an array of log writer IDs. The three log writers provided
  38. * by Piwik core are **file**, **screen** and **database**. You can
  39. * get more by installing plugins. The default value is **screen**.
  40. * - `log_level`: The current log level. Can be **ERROR**, **WARN**, **INFO**, **DEBUG**,
  41. * or **VERBOSE**. Log entries made with a log level that is as or more
  42. * severe than the current log level will be outputted. Others will be
  43. * ignored. The default level is **WARN**.
  44. * - `logger_file_path`: For the file log writer, specifies the path to the log file
  45. * to log to or a path to a directory to store logs in. If a
  46. * directory, the file name is piwik.log. Can be relative to
  47. * Piwik's root dir or an absolute path. Defaults to **tmp/logs**.
  48. *
  49. *
  50. * @deprecated Inject and use Psr\Log\LoggerInterface instead of this class.
  51. * @see \Psr\Log\LoggerInterface
  52. */
  53. class Log extends Singleton
  54. {
  55. // log levels
  56. const NONE = 0;
  57. const ERROR = 1;
  58. const WARN = 2;
  59. const INFO = 3;
  60. const DEBUG = 4;
  61. const VERBOSE = 5;
  62. // config option names
  63. const LOG_LEVEL_CONFIG_OPTION = 'log_level';
  64. const LOG_WRITERS_CONFIG_OPTION = 'log_writers';
  65. const LOGGER_FILE_PATH_CONFIG_OPTION = 'logger_file_path';
  66. const STRING_MESSAGE_FORMAT_OPTION = 'string_message_format';
  67. /**
  68. * The backtrace string to use when testing.
  69. *
  70. * @var string
  71. */
  72. public static $debugBacktraceForTests;
  73. /**
  74. * Singleton instance.
  75. *
  76. * @var Log
  77. */
  78. private static $instance;
  79. /**
  80. * @var LoggerInterface
  81. */
  82. private $logger;
  83. public static function getInstance()
  84. {
  85. if (self::$instance === null) {
  86. self::$instance = StaticContainer::get(__CLASS__);
  87. }
  88. return self::$instance;
  89. }
  90. public static function unsetInstance()
  91. {
  92. self::$instance = null;
  93. }
  94. public static function setSingletonInstance($instance)
  95. {
  96. self::$instance = $instance;
  97. }
  98. /**
  99. * @param LoggerInterface $logger
  100. */
  101. public function __construct(LoggerInterface $logger)
  102. {
  103. $this->logger = $logger;
  104. }
  105. /**
  106. * Logs a message using the ERROR log level.
  107. *
  108. * @param string $message The log message. This can be a sprintf format string.
  109. * @param ... mixed Optional sprintf params.
  110. * @api
  111. *
  112. * @deprecated Inject and call Psr\Log\LoggerInterface::error() instead.
  113. * @see \Psr\Log\LoggerInterface::error()
  114. */
  115. public static function error($message /* ... */)
  116. {
  117. self::logMessage(Logger::ERROR, $message, array_slice(func_get_args(), 1));
  118. }
  119. /**
  120. * Logs a message using the WARNING log level.
  121. *
  122. * @param string $message The log message. This can be a sprintf format string.
  123. * @param ... mixed Optional sprintf params.
  124. * @api
  125. *
  126. * @deprecated Inject and call Psr\Log\LoggerInterface::warning() instead.
  127. * @see \Psr\Log\LoggerInterface::warning()
  128. */
  129. public static function warning($message /* ... */)
  130. {
  131. self::logMessage(Logger::WARNING, $message, array_slice(func_get_args(), 1));
  132. }
  133. /**
  134. * Logs a message using the INFO log level.
  135. *
  136. * @param string $message The log message. This can be a sprintf format string.
  137. * @param ... mixed Optional sprintf params.
  138. * @api
  139. *
  140. * @deprecated Inject and call Psr\Log\LoggerInterface::info() instead.
  141. * @see \Psr\Log\LoggerInterface::info()
  142. */
  143. public static function info($message /* ... */)
  144. {
  145. self::logMessage(Logger::INFO, $message, array_slice(func_get_args(), 1));
  146. }
  147. /**
  148. * Logs a message using the DEBUG log level.
  149. *
  150. * @param string $message The log message. This can be a sprintf format string.
  151. * @param ... mixed Optional sprintf params.
  152. * @api
  153. *
  154. * @deprecated Inject and call Psr\Log\LoggerInterface::debug() instead.
  155. * @see \Psr\Log\LoggerInterface::debug()
  156. */
  157. public static function debug($message /* ... */)
  158. {
  159. self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1));
  160. }
  161. /**
  162. * Logs a message using the VERBOSE log level.
  163. *
  164. * @param string $message The log message. This can be a sprintf format string.
  165. * @param ... mixed Optional sprintf params.
  166. * @api
  167. *
  168. * @deprecated Inject and call Psr\Log\LoggerInterface::debug() instead (the verbose level doesn't exist in the PSR standard).
  169. * @see \Psr\Log\LoggerInterface::debug()
  170. */
  171. public static function verbose($message /* ... */)
  172. {
  173. self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1));
  174. }
  175. /**
  176. * @param int $logLevel
  177. * @deprecated Will be removed, log levels are now applied on each Monolog handler.
  178. */
  179. public function setLogLevel($logLevel)
  180. {
  181. }
  182. /**
  183. * @deprecated Will be removed, log levels are now applied on each Monolog handler.
  184. */
  185. public function getLogLevel()
  186. {
  187. }
  188. private function doLog($level, $message, $parameters = array())
  189. {
  190. // To ensure the compatibility with PSR-3, the message must be a string
  191. if ($message instanceof \Exception) {
  192. $parameters['exception'] = $message;
  193. $message = $message->getMessage();
  194. }
  195. if (is_object($message) || is_array($message) || is_resource($message)) {
  196. $this->logger->warning('Trying to log a message that is not a string', array(
  197. 'exception' => new \InvalidArgumentException('Trying to log a message that is not a string')
  198. ));
  199. return;
  200. }
  201. $this->logger->log($level, $message, $parameters);
  202. }
  203. private static function logMessage($level, $message, $parameters)
  204. {
  205. self::getInstance()->doLog($level, $message, $parameters);
  206. }
  207. public static function getMonologLevel($level)
  208. {
  209. switch ($level) {
  210. case self::ERROR:
  211. return Logger::ERROR;
  212. case self::WARN:
  213. return Logger::WARNING;
  214. case self::INFO:
  215. return Logger::INFO;
  216. case self::DEBUG:
  217. return Logger::DEBUG;
  218. case self::VERBOSE:
  219. return Logger::DEBUG;
  220. case self::NONE:
  221. default:
  222. // Highest level possible, need to do better in the future...
  223. return Logger::EMERGENCY;
  224. }
  225. }
  226. }