sfVarLogger.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfVarLogger logs messages within its instance for later use.
  11. *
  12. * @package symfony
  13. * @subpackage log
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfVarLogger.class.php 14173 2008-12-18 12:49:57Z Kris.Wallsmith $
  16. */
  17. class sfVarLogger extends sfLogger
  18. {
  19. protected
  20. $logs = array(),
  21. $xdebugLogging = false;
  22. /**
  23. * Initializes this logger.
  24. *
  25. * Available options:
  26. *
  27. * - xdebug_logging: Whether to add xdebug trace to the logs (false by default).
  28. *
  29. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  30. * @param array $options An array of options.
  31. *
  32. * @return Boolean true, if initialization completes successfully, otherwise false.
  33. */
  34. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  35. {
  36. $this->xdebugLogging = isset($options['xdebug_logging']) ? $options['xdebug_logging'] : false;
  37. // disable xdebug when an HTTP debug session exists (crashes Apache, see #2438)
  38. if (isset($_GET['XDEBUG_SESSION_START']) || isset($_COOKIE['XDEBUG_SESSION']))
  39. {
  40. $this->xdebugLogging = false;
  41. }
  42. return parent::initialize($dispatcher, $options);
  43. }
  44. /**
  45. * Gets the logs.
  46. *
  47. * Each log entry has the following attributes:
  48. *
  49. * * priority
  50. * * time
  51. * * message
  52. * * type
  53. * * debugStack
  54. *
  55. * @return array An array of logs
  56. */
  57. public function getLogs()
  58. {
  59. return $this->logs;
  60. }
  61. /**
  62. * Returns all the types in the logs.
  63. *
  64. * @return array An array of types
  65. */
  66. public function getTypes()
  67. {
  68. $types = array();
  69. foreach ($this->logs as $log)
  70. {
  71. if (!in_array($log['type'], $types))
  72. {
  73. $types[] = $log['type'];
  74. }
  75. }
  76. sort($types);
  77. return $types;
  78. }
  79. /**
  80. * Returns all the priorities in the logs.
  81. *
  82. * @return array An array of priorities
  83. */
  84. public function getPriorities()
  85. {
  86. $priorities = array();
  87. foreach ($this->logs as $log)
  88. {
  89. if (!in_array($log['priority'], $types))
  90. {
  91. $priorities[] = $log['priority'];
  92. }
  93. }
  94. sort($priorities);
  95. return $priorities;
  96. }
  97. /**
  98. * Returns the highest priority in the logs.
  99. *
  100. * @return integer The highest priority
  101. */
  102. public function getHighestPriority()
  103. {
  104. $priority = 1000;
  105. foreach ($this->logs as $log)
  106. {
  107. if ($log['priority'] < $priority)
  108. {
  109. $priority = $log['priority'];
  110. }
  111. }
  112. return $priority;
  113. }
  114. /**
  115. * Logs a message.
  116. *
  117. * @param string $message Message
  118. * @param string $priority Message priority
  119. */
  120. protected function doLog($message, $priority)
  121. {
  122. // get log type in {}
  123. $type = 'sfOther';
  124. if (preg_match('/^\s*{([^}]+)}\s*(.+?)$/s', $message, $matches))
  125. {
  126. $type = $matches[1];
  127. $message = $matches[2];
  128. }
  129. $this->logs[] = array(
  130. 'priority' => $priority,
  131. 'priority_name' => $this->getPriorityName($priority),
  132. 'time' => time(),
  133. 'message' => $message,
  134. 'type' => $type,
  135. 'debug_stack' => $this->getXDebugStack(),
  136. );
  137. }
  138. /**
  139. * Returns the xdebug stack.
  140. *
  141. * @return array The xdebug stack as an array
  142. */
  143. protected function getXDebugStack()
  144. {
  145. // if we have xdebug and dev has not disabled the feature, add some stack information
  146. if (!$this->xdebugLogging || !function_exists('xdebug_get_function_stack'))
  147. {
  148. return array();
  149. }
  150. $debugStack = array();
  151. foreach (xdebug_get_function_stack() as $i => $stack)
  152. {
  153. if (
  154. (isset($stack['function']) && !in_array($stack['function'], array('emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'log')))
  155. || !isset($stack['function'])
  156. )
  157. {
  158. $tmp = '';
  159. if (isset($stack['function']))
  160. {
  161. $tmp .= sprintf('in "%s" ', $stack['function']);
  162. }
  163. $tmp .= sprintf('from "%s" line %s', $stack['file'], $stack['line']);
  164. $debugStack[] = $tmp;
  165. }
  166. }
  167. return $debugStack;
  168. }
  169. }