sfWebDebugPanelLogs.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 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. * sfWebDebugPanelLogs adds a panel to the web debug toolbar with log messages.
  11. *
  12. * @package symfony
  13. * @subpackage debug
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWebDebugPanelLogs.class.php 12982 2008-11-13 17:25:10Z hartym $
  16. */
  17. class sfWebDebugPanelLogs extends sfWebDebugPanel
  18. {
  19. public function getTitle()
  20. {
  21. return '<img src="'.$this->webDebug->getOption('image_root_path').'/log.png" alt="Log" /> logs';
  22. }
  23. public function getPanelTitle()
  24. {
  25. return 'Logs';
  26. }
  27. public function getPanelContent()
  28. {
  29. $event = $this->webDebug->getEventDispatcher()->filter(new sfEvent($this, 'debug.web.filter_logs'), $this->webDebug->getLogger()->getLogs());
  30. $logs = $event->getReturnValue();
  31. $html = '<table class="sfWebDebugLogs">
  32. <tr>
  33. <th>#</th>
  34. <th>type</th>
  35. <th>message</th>
  36. </tr>'."\n";
  37. $line_nb = 0;
  38. foreach ($logs as $log)
  39. {
  40. $priority = $this->webDebug->getPriority($log['priority']);
  41. if (strpos($type = $log['type'], 'sf') === 0)
  42. {
  43. $type = substr($type, 2);
  44. }
  45. // xdebug information
  46. $debug_info = '';
  47. if (count($log['debug_stack']))
  48. {
  49. $debug_info .= '&nbsp;<a href="#" onclick="sfWebDebugToggle(\'debug_'.$line_nb.'\'); return false;"><img src="'.$this->webDebug->getOption('image_root_path').'/toggle.gif" alt="Toggle XDebug details" /></a><div class="sfWebDebugDebugInfo" id="debug_'.$line_nb.'" style="display:none">';
  50. foreach ($log['debug_stack'] as $i => $logLine)
  51. {
  52. $debug_info .= '#'.$i.' &raquo; '.$this->formatLogLine($logLine).'<br/>';
  53. }
  54. $debug_info .= "</div>\n";
  55. }
  56. ++$line_nb;
  57. $html .= sprintf("<tr class='sfWebDebugLogLine sfWebDebug%s %s'><td class=\"sfWebDebugLogNumber\">%s</td><td class=\"sfWebDebugLogType\">%s&nbsp;%s</td><td>%s%s</td></tr>\n",
  58. ucfirst($priority),
  59. $log['type'],
  60. $line_nb,
  61. '<img src="'.$this->webDebug->getOption('image_root_path').'/'.$priority.'.png" alt="'.ucfirst($priority).'"/>',
  62. $type,
  63. $this->formatLogLine($log['message']),
  64. $debug_info
  65. );
  66. }
  67. $html .= '</table>';
  68. $types = array();
  69. foreach ($this->webDebug->getLogger()->getTypes() as $type)
  70. {
  71. $types[] = '<a href="#" onclick="sfWebDebugToggleMessages(\''.$type.'\'); return false;">'.$type.'</a>';
  72. }
  73. return '
  74. <ul id="sfWebDebugLogMenu">
  75. <li><a href="#" onclick="sfWebDebugToggleAllLogLines(true, \'sfWebDebugLogLine\'); return false;">[all]</a></li>
  76. <li><a href="#" onclick="sfWebDebugToggleAllLogLines(false, \'sfWebDebugLogLine\'); return false;">[none]</a></li>
  77. <li><a href="#" onclick="sfWebDebugShowOnlyLogLines(\'info\'); return false;"><img src="'.$this->webDebug->getOption('image_root_path').'/info.png" alt="Show only infos" /></a></li>
  78. <li><a href="#" onclick="sfWebDebugShowOnlyLogLines(\'warning\'); return false;"><img src="'.$this->webDebug->getOption('image_root_path').'/warning.png" alt="Show only warnings" /></a></li>
  79. <li><a href="#" onclick="sfWebDebugShowOnlyLogLines(\'error\'); return false;"><img src="'.$this->webDebug->getOption('image_root_path').'/error.png" alt="Show only errors" /></a></li>
  80. <li>'.implode("</li>\n<li>", $types).'</li>
  81. </ul>
  82. <div id="sfWebDebugLogLines">'.$html.'</div>
  83. ';
  84. }
  85. /**
  86. * Formats a log line.
  87. *
  88. * @param string $logLine The log line to format
  89. *
  90. * @return string The formatted log lin
  91. */
  92. protected function formatLogLine($logLine)
  93. {
  94. static $constants;
  95. if (!$constants)
  96. {
  97. foreach (array('sf_app_dir', 'sf_root_dir', 'sf_symfony_lib_dir') as $constant)
  98. {
  99. $constants[realpath(sfConfig::get($constant)).DIRECTORY_SEPARATOR] = $constant.DIRECTORY_SEPARATOR;
  100. }
  101. }
  102. // escape HTML
  103. $logLine = htmlspecialchars($logLine, ENT_QUOTES, sfConfig::get('sf_charset'));
  104. // replace constants value with constant name
  105. $logLine = str_replace(array_keys($constants), array_values($constants), $logLine);
  106. $logLine = sfToolkit::pregtr($logLine, array('/&quot;(.+?)&quot;/s' => '"<span class="sfWebDebugLogInfo">\\1</span>"',
  107. '/^(.+?)\(\)\:/S' => '<span class="sfWebDebugLogInfo">\\1()</span>:',
  108. '/line (\d+)$/' => 'line <span class="sfWebDebugLogInfo">\\1</span>'));
  109. // special formatting for SQL lines
  110. $logLine = preg_replace('/\b(SELECT|FROM|AS|LIMIT|ASC|COUNT|DESC|WHERE|LEFT JOIN|INNER JOIN|RIGHT JOIN|ORDER BY|GROUP BY|IN|LIKE|DISTINCT|DELETE|INSERT|INTO|VALUES)\b/', '<span class="sfWebDebugLogInfo">\\1</span>', $logLine);
  111. // remove username/password from DSN
  112. if (strpos($logLine, 'DSN') !== false)
  113. {
  114. $logLine = preg_replace("/=&gt;\s+'?[^'\s,]+'?/", "=&gt; '****'", $logLine);
  115. }
  116. return $logLine;
  117. }
  118. }