[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * This file is part of the DebugBar package. 4 * 5 * (c) 2013 Maxime Bouroumeau-Fuseau 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 namespace DebugBar\Bridge; 12 13 use BasicLogger; 14 use DebugBar\DataCollector\AssetProvider; 15 use DebugBar\DataCollector\DataCollector; 16 use DebugBar\DataCollector\Renderable; 17 use Propel; 18 use PropelConfiguration; 19 use PropelPDO; 20 use Psr\Log\LogLevel; 21 use Psr\Log\LoggerInterface; 22 23 /** 24 * A Propel logger which acts as a data collector 25 * 26 * http://propelorm.org/ 27 * 28 * Will log queries and display them using the SQLQueries widget. 29 * You can provide a LoggerInterface object to forward non-query related message to. 30 * 31 * Example: 32 * <code> 33 * $debugbar->addCollector(new PropelCollector($debugbar['messages'])); 34 * PropelCollector::enablePropelProfiling(); 35 * </code> 36 */ 37 class PropelCollector extends DataCollector implements BasicLogger, Renderable, AssetProvider 38 { 39 protected $logger; 40 41 protected $statements = array(); 42 43 protected $accumulatedTime = 0; 44 45 protected $peakMemory = 0; 46 47 /** 48 * Sets the needed configuration option in propel to enable query logging 49 * 50 * @param PropelConfiguration $config Apply profiling on a specific config 51 */ 52 public static function enablePropelProfiling(PropelConfiguration $config = null) 53 { 54 if ($config === null) { 55 $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT); 56 } 57 $config->setParameter('debugpdo.logging.details.method.enabled', true); 58 $config->setParameter('debugpdo.logging.details.time.enabled', true); 59 $config->setParameter('debugpdo.logging.details.mem.enabled', true); 60 $allMethods = array( 61 'PropelPDO::__construct', // logs connection opening 62 'PropelPDO::__destruct', // logs connection close 63 'PropelPDO::exec', // logs a query 64 'PropelPDO::query', // logs a query 65 'PropelPDO::beginTransaction', // logs a transaction begin 66 'PropelPDO::commit', // logs a transaction commit 67 'PropelPDO::rollBack', // logs a transaction rollBack (watch out for the capital 'B') 68 'DebugPDOStatement::execute', // logs a query from a prepared statement 69 ); 70 $config->setParameter('debugpdo.logging.methods', $allMethods, false); 71 } 72 73 /** 74 * @param LoggerInterface $logger A logger to forward non-query log lines to 75 * @param PropelPDO $conn Bound this collector to a connection only 76 */ 77 public function __construct(LoggerInterface $logger = null, PropelPDO $conn = null) 78 { 79 if ($conn) { 80 $conn->setLogger($this); 81 } else { 82 Propel::setLogger($this); 83 } 84 $this->logger = $logger; 85 $this->logQueriesToLogger = false; 86 } 87 88 public function setLogQueriesToLogger($enable = true) 89 { 90 $this->logQueriesToLogger = $enable; 91 return $this; 92 } 93 94 public function isLogQueriesToLogger() 95 { 96 return $this->logQueriesToLogger; 97 } 98 99 public function emergency($m) 100 { 101 $this->log($m, Propel::LOG_EMERG); 102 } 103 104 public function alert($m) 105 { 106 $this->log($m, Propel::LOG_ALERT); 107 } 108 109 public function crit($m) 110 { 111 $this->log($m, Propel::LOG_CRIT); 112 } 113 114 public function err($m) 115 { 116 $this->log($m, Propel::LOG_ERR); 117 } 118 119 public function warning($m) 120 { 121 $this->log($m, Propel::LOG_WARNING); 122 } 123 124 public function notice($m) 125 { 126 $this->log($m, Propel::LOG_NOTICE); 127 } 128 129 public function info($m) 130 { 131 $this->log($m, Propel::LOG_INFO); 132 } 133 134 public function debug($m) 135 { 136 $this->log($m, Propel::LOG_DEBUG); 137 } 138 139 public function log($message, $severity = null) 140 { 141 if (strpos($message, 'DebugPDOStatement::execute') !== false) { 142 list($sql, $duration_str) = $this->parseAndLogSqlQuery($message); 143 if (!$this->logQueriesToLogger) { 144 return; 145 } 146 $message = "$sql ($duration_str)"; 147 } 148 149 if ($this->logger !== null) { 150 $this->logger->log($this->convertLogLevel($severity), $message); 151 } 152 } 153 154 /** 155 * Converts Propel log levels to PSR log levels 156 * 157 * @param int $level 158 * @return string 159 */ 160 protected function convertLogLevel($level) 161 { 162 $map = array( 163 Propel::LOG_EMERG => LogLevel::EMERGENCY, 164 Propel::LOG_ALERT => LogLevel::ALERT, 165 Propel::LOG_CRIT => LogLevel::CRITICAL, 166 Propel::LOG_ERR => LogLevel::ERROR, 167 Propel::LOG_WARNING => LogLevel::WARNING, 168 Propel::LOG_NOTICE => LogLevel::NOTICE, 169 Propel::LOG_DEBUG => LogLevel::DEBUG 170 ); 171 return $map[$level]; 172 } 173 174 /** 175 * Parse a log line to extract query information 176 * 177 * @param string $message 178 */ 179 protected function parseAndLogSqlQuery($message) 180 { 181 $parts = explode('|', $message, 4); 182 $sql = trim($parts[3]); 183 184 $duration = 0; 185 if (preg_match('/([0-9]+\.[0-9]+)/', $parts[1], $matches)) { 186 $duration = (float) $matches[1]; 187 } 188 189 $memory = 0; 190 if (preg_match('/([0-9]+\.[0-9]+) ([A-Z]{1,2})/', $parts[2], $matches)) { 191 $memory = (float) $matches[1]; 192 if ($matches[2] == 'KB') { 193 $memory *= 1024; 194 } elseif ($matches[2] == 'MB') { 195 $memory *= 1024 * 1024; 196 } 197 } 198 199 $this->statements[] = array( 200 'sql' => $sql, 201 'is_success' => true, 202 'duration' => $duration, 203 'duration_str' => $this->formatDuration($duration), 204 'memory' => $memory, 205 'memory_str' => $this->formatBytes($memory) 206 ); 207 $this->accumulatedTime += $duration; 208 $this->peakMemory = max($this->peakMemory, $memory); 209 return array($sql, $this->formatDuration($duration)); 210 } 211 212 public function collect() 213 { 214 return array( 215 'nb_statements' => count($this->statements), 216 'nb_failed_statements' => 0, 217 'accumulated_duration' => $this->accumulatedTime, 218 'accumulated_duration_str' => $this->formatDuration($this->accumulatedTime), 219 'peak_memory_usage' => $this->peakMemory, 220 'peak_memory_usage_str' => $this->formatBytes($this->peakMemory), 221 'statements' => $this->statements 222 ); 223 } 224 225 public function getName() 226 { 227 return 'propel'; 228 } 229 230 public function getWidgets() 231 { 232 return array( 233 "propel" => array( 234 "icon" => "bolt", 235 "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget", 236 "map" => "propel", 237 "default" => "[]" 238 ), 239 "propel:badge" => array( 240 "map" => "propel.nb_statements", 241 "default" => 0 242 ) 243 ); 244 } 245 246 public function getAssets() 247 { 248 return array( 249 'css' => 'widgets/sqlqueries/widget.css', 250 'js' => 'widgets/sqlqueries/widget.js' 251 ); 252 } 253 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |