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

Debug toolbar