PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Log/Log.php

https://github.com/piotr-cz/joomla-framework
PHP | 324 lines | 136 code | 30 blank | 158 comment | 18 complexity | 2e54e9751f710626fca20560eda92001 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Part of the Joomla Framework Log Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Log;
  9. /**
  10. * Joomla! Log Class
  11. *
  12. * This class hooks into the global log configuration settings to allow for user configured
  13. * logging events to be sent to where the user wishes them to be sent. On high load sites
  14. * Syslog is probably the best (pure PHP function), then the text file based loggers (CSV, W3c
  15. * or plain Formattedtext) and finally MySQL offers the most features (e.g. rapid searching)
  16. * but will incur a performance hit due to INSERT being issued.
  17. *
  18. * @since 1.0
  19. */
  20. class Log
  21. {
  22. /**
  23. * All log priorities.
  24. *
  25. * @var integer
  26. * @since 1.0
  27. */
  28. const ALL = 30719;
  29. /**
  30. * The system is unusable.
  31. *
  32. * @var integer
  33. * @since 1.0
  34. */
  35. const EMERGENCY = 1;
  36. /**
  37. * Action must be taken immediately.
  38. *
  39. * @var integer
  40. * @since 1.0
  41. */
  42. const ALERT = 2;
  43. /**
  44. * Critical conditions.
  45. *
  46. * @var integer
  47. * @since 1.0
  48. */
  49. const CRITICAL = 4;
  50. /**
  51. * Error conditions.
  52. *
  53. * @var integer
  54. * @since 1.0
  55. */
  56. const ERROR = 8;
  57. /**
  58. * Warning conditions.
  59. *
  60. * @var integer
  61. * @since 1.0
  62. */
  63. const WARNING = 16;
  64. /**
  65. * Normal, but significant condition.
  66. *
  67. * @var integer
  68. * @since 1.0
  69. */
  70. const NOTICE = 32;
  71. /**
  72. * Informational message.
  73. *
  74. * @var integer
  75. * @since 1.0
  76. */
  77. const INFO = 64;
  78. /**
  79. * Debugging message.
  80. *
  81. * @var integer
  82. * @since 1.0
  83. */
  84. const DEBUG = 128;
  85. /**
  86. * The global Log instance.
  87. *
  88. * @var Log
  89. * @since 1.0
  90. */
  91. protected static $instance;
  92. /**
  93. * Container for AbstractLogger configurations.
  94. *
  95. * @var array
  96. * @since 1.0
  97. */
  98. protected $configurations = array();
  99. /**
  100. * Container for AbstractLogger objects.
  101. *
  102. * @var array
  103. * @since 1.0
  104. */
  105. protected $loggers = array();
  106. /**
  107. * Lookup array for loggers.
  108. *
  109. * @var array
  110. * @since 1.0
  111. */
  112. protected $lookup = array();
  113. /**
  114. * Constructor.
  115. *
  116. * @since 1.0
  117. */
  118. protected function __construct()
  119. {
  120. }
  121. /**
  122. * Method to add an entry to the log.
  123. *
  124. * @param mixed $entry The LogEntry object to add to the log or the message for a new LogEntry object.
  125. * @param integer $priority Message priority.
  126. * @param string $category Type of entry
  127. * @param string $date Date of entry (defaults to now if not specified or blank)
  128. *
  129. * @return void
  130. *
  131. * @since 1.0
  132. */
  133. public static function add($entry, $priority = self::INFO, $category = '', $date = null)
  134. {
  135. // Automatically instantiate the singleton object if not already done.
  136. if (empty(self::$instance))
  137. {
  138. self::setInstance(new self);
  139. }
  140. // If the entry object isn't a LogEntry object let's make one.
  141. if (!($entry instanceof LogEntry))
  142. {
  143. $entry = new LogEntry((string) $entry, $priority, $category, $date);
  144. }
  145. self::$instance->addLogEntry($entry);
  146. }
  147. /**
  148. * Add a logger to the Log instance. Loggers route log entries to the correct files/systems to be logged.
  149. *
  150. * @param array $options The object configuration array.
  151. * @param integer $priorities Message priority
  152. * @param array $categories Types of entry
  153. * @param boolean $exclude If true, all categories will be logged except those in the $categories array
  154. *
  155. * @return void
  156. *
  157. * @since 1.0
  158. */
  159. public static function addLogger(array $options, $priorities = self::ALL, $categories = array(), $exclude = false)
  160. {
  161. // Automatically instantiate the singleton object if not already done.
  162. if (empty(self::$instance))
  163. {
  164. self::setInstance(new self);
  165. }
  166. // The default logger is the formatted text log file.
  167. if (empty($options['logger']))
  168. {
  169. $options['logger'] = 'formattedtext';
  170. }
  171. $options['logger'] = strtolower($options['logger']);
  172. // Special case - if a Closure object is sent as the callback (in case of Logger\Callback)
  173. // Closure objects are not serializable so swap it out for a unique id first then back again later
  174. if (isset($options['callback']) && is_a($options['callback'], 'closure'))
  175. {
  176. $callback = $options['callback'];
  177. $options['callback'] = spl_object_hash($options['callback']);
  178. }
  179. // Generate a unique signature for the Log instance based on its options.
  180. $signature = md5(serialize($options));
  181. // Now that the options array has been serialized, swap the callback back in
  182. if (isset($callback))
  183. {
  184. $options['callback'] = $callback;
  185. }
  186. // Register the configuration if it doesn't exist.
  187. if (empty(self::$instance->configurations[$signature]))
  188. {
  189. self::$instance->configurations[$signature] = $options;
  190. }
  191. self::$instance->lookup[$signature] = (object) array(
  192. 'priorities' => $priorities,
  193. 'categories' => array_map('strtolower', (array) $categories),
  194. 'exclude' => (bool) $exclude
  195. );
  196. }
  197. /**
  198. * Returns a reference to the a Log object, only creating it if it doesn't already exist.
  199. * Note: This is principally made available for testing and internal purposes.
  200. *
  201. * @param Log $instance The logging object instance to be used by the static methods.
  202. *
  203. * @return void
  204. *
  205. * @since 1.0
  206. */
  207. public static function setInstance($instance)
  208. {
  209. if (($instance instanceof Log) || $instance === null)
  210. {
  211. self::$instance = & $instance;
  212. }
  213. }
  214. /**
  215. * Method to add an entry to the appropriate loggers.
  216. *
  217. * @param LogEntry $entry The LogEntry object to send to the loggers.
  218. *
  219. * @return void
  220. *
  221. * @since 1.0
  222. * @throws \RuntimeException
  223. */
  224. protected function addLogEntry(LogEntry $entry)
  225. {
  226. // Find all the appropriate loggers based on priority and category for the entry.
  227. $loggers = $this->findLoggers($entry->priority, $entry->category);
  228. foreach ((array) $loggers as $signature)
  229. {
  230. // Attempt to instantiate the logger object if it doesn't already exist.
  231. if (empty($this->loggers[$signature]))
  232. {
  233. $class = '\\Joomla\\Log\\Logger\\' . ucfirst($this->configurations[$signature]['logger']);
  234. if (class_exists($class))
  235. {
  236. $this->loggers[$signature] = new $class($this->configurations[$signature]);
  237. }
  238. else
  239. {
  240. throw new \RuntimeException('Unable to create a Joomla\\Log\\AbstractLogger instance: ' . $class);
  241. }
  242. }
  243. // Add the entry to the logger.
  244. $this->loggers[$signature]->addEntry(clone($entry));
  245. }
  246. }
  247. /**
  248. * Method to find the loggers to use based on priority and category values.
  249. *
  250. * @param integer $priority Message priority.
  251. * @param string $category Type of entry
  252. *
  253. * @return array The array of loggers to use for the given priority and category values.
  254. *
  255. * @since 1.0
  256. */
  257. protected function findLoggers($priority, $category)
  258. {
  259. $loggers = array();
  260. // Sanitize inputs.
  261. $priority = (int) $priority;
  262. $category = strtolower($category);
  263. // Let's go iterate over the loggers and get all the ones we need.
  264. foreach ((array) $this->lookup as $signature => $rules)
  265. {
  266. // Check to make sure the priority matches the logger.
  267. if ($priority & $rules->priorities)
  268. {
  269. if ($rules->exclude)
  270. {
  271. // If either there are no set categories or the category (including the empty case) is not in the list of excluded categories, add this logger.
  272. if (empty($rules->categories) || !in_array($category, $rules->categories))
  273. {
  274. $loggers[] = $signature;
  275. }
  276. }
  277. else
  278. {
  279. // If either there are no set categories (meaning all) or the specific category is set, add this logger.
  280. if (empty($category) || empty($rules->categories) || in_array($category, $rules->categories))
  281. {
  282. $loggers[] = $signature;
  283. }
  284. }
  285. }
  286. }
  287. return $loggers;
  288. }
  289. }