123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfVarLogger logs messages within its instance for later use.
- *
- * @package symfony
- * @subpackage log
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfVarLogger.class.php 14173 2008-12-18 12:49:57Z Kris.Wallsmith $
- */
- class sfVarLogger extends sfLogger
- {
- protected
- $logs = array(),
- $xdebugLogging = false;
- /**
- * Initializes this logger.
- *
- * Available options:
- *
- * - xdebug_logging: Whether to add xdebug trace to the logs (false by default).
- *
- * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
- * @param array $options An array of options.
- *
- * @return Boolean true, if initialization completes successfully, otherwise false.
- */
- public function initialize(sfEventDispatcher $dispatcher, $options = array())
- {
- $this->xdebugLogging = isset($options['xdebug_logging']) ? $options['xdebug_logging'] : false;
- // disable xdebug when an HTTP debug session exists (crashes Apache, see #2438)
- if (isset($_GET['XDEBUG_SESSION_START']) || isset($_COOKIE['XDEBUG_SESSION']))
- {
- $this->xdebugLogging = false;
- }
- return parent::initialize($dispatcher, $options);
- }
- /**
- * Gets the logs.
- *
- * Each log entry has the following attributes:
- *
- * * priority
- * * time
- * * message
- * * type
- * * debugStack
- *
- * @return array An array of logs
- */
- public function getLogs()
- {
- return $this->logs;
- }
- /**
- * Returns all the types in the logs.
- *
- * @return array An array of types
- */
- public function getTypes()
- {
- $types = array();
- foreach ($this->logs as $log)
- {
- if (!in_array($log['type'], $types))
- {
- $types[] = $log['type'];
- }
- }
- sort($types);
- return $types;
- }
- /**
- * Returns all the priorities in the logs.
- *
- * @return array An array of priorities
- */
- public function getPriorities()
- {
- $priorities = array();
- foreach ($this->logs as $log)
- {
- if (!in_array($log['priority'], $types))
- {
- $priorities[] = $log['priority'];
- }
- }
- sort($priorities);
- return $priorities;
- }
- /**
- * Returns the highest priority in the logs.
- *
- * @return integer The highest priority
- */
- public function getHighestPriority()
- {
- $priority = 1000;
- foreach ($this->logs as $log)
- {
- if ($log['priority'] < $priority)
- {
- $priority = $log['priority'];
- }
- }
- return $priority;
- }
- /**
- * Logs a message.
- *
- * @param string $message Message
- * @param string $priority Message priority
- */
- protected function doLog($message, $priority)
- {
- // get log type in {}
- $type = 'sfOther';
- if (preg_match('/^\s*{([^}]+)}\s*(.+?)$/s', $message, $matches))
- {
- $type = $matches[1];
- $message = $matches[2];
- }
- $this->logs[] = array(
- 'priority' => $priority,
- 'priority_name' => $this->getPriorityName($priority),
- 'time' => time(),
- 'message' => $message,
- 'type' => $type,
- 'debug_stack' => $this->getXDebugStack(),
- );
- }
- /**
- * Returns the xdebug stack.
- *
- * @return array The xdebug stack as an array
- */
- protected function getXDebugStack()
- {
- // if we have xdebug and dev has not disabled the feature, add some stack information
- if (!$this->xdebugLogging || !function_exists('xdebug_get_function_stack'))
- {
- return array();
- }
- $debugStack = array();
- foreach (xdebug_get_function_stack() as $i => $stack)
- {
- if (
- (isset($stack['function']) && !in_array($stack['function'], array('emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'log')))
- || !isset($stack['function'])
- )
- {
- $tmp = '';
- if (isset($stack['function']))
- {
- $tmp .= sprintf('in "%s" ', $stack['function']);
- }
- $tmp .= sprintf('from "%s" line %s', $stack['file'], $stack['line']);
- $debugStack[] = $tmp;
- }
- }
- return $debugStack;
- }
- }
|