[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * This file is part of Aura for PHP. 5 * 6 * @license https://opensource.org/licenses/MIT MIT 7 * 8 */ 9 namespace Aura\Sql\Profiler; 10 11 use Aura\Sql\Exception; 12 use Psr\Log\LoggerInterface; 13 use Psr\Log\LogLevel; 14 15 /** 16 * 17 * Sends query profiles to a logger. 18 * 19 * @package Aura.Sql 20 * 21 */ 22 class Profiler implements ProfilerInterface 23 { 24 /** 25 * 26 * The current profile information. 27 * 28 * @var array 29 * 30 */ 31 protected array $context = []; 32 33 /** 34 * 35 * Log profile data through this interface. 36 * 37 * @var LoggerInterface 38 * 39 */ 40 protected LoggerInterface $logger; 41 42 /** 43 * 44 * Turns profile logging off and on. 45 * 46 * @var bool 47 * 48 * @see setActive() 49 * 50 */ 51 protected bool $active = false; 52 53 /** 54 * 55 * The log level for all messages. 56 * 57 * @var string 58 * 59 * @see setLogLevel() 60 * 61 */ 62 protected string $logLevel = LogLevel::DEBUG; 63 64 /** 65 * 66 * Sets the format for the log message, with placeholders. 67 * 68 * @var string 69 * 70 * @see setLogFormat() 71 * 72 */ 73 protected string $logFormat = "{function} ({duration} seconds): {statement} {backtrace}"; 74 75 /** 76 * 77 * Constructor. 78 * 79 * @param \Psr\Log\LoggerInterface|null $logger Record profiles through this interface. 80 */ 81 public function __construct(?LoggerInterface $logger = null) 82 { 83 $this->logger = $logger ?? new MemoryLogger(); 84 } 85 86 /** 87 * 88 * Enable or disable profiler logging. 89 * 90 * @param bool $active 91 * 92 */ 93 public function setActive(bool $active) 94 { 95 $this->active = $active; 96 } 97 98 /** 99 * 100 * Returns true if logging is active. 101 * 102 * @return bool 103 * 104 */ 105 public function isActive(): bool 106 { 107 return $this->active; 108 } 109 110 /** 111 * 112 * Returns the underlying logger instance. 113 * 114 * @return \Psr\Log\LoggerInterface 115 * 116 */ 117 public function getLogger(): LoggerInterface 118 { 119 return $this->logger; 120 } 121 122 /** 123 * 124 * Returns the level at which to log profile messages. 125 * 126 * @return string 127 * 128 */ 129 public function getLogLevel(): string 130 { 131 return $this->logLevel; 132 } 133 134 /** 135 * 136 * Level at which to log profile messages. 137 * 138 * @param string $logLevel A PSR LogLevel constant. 139 * 140 * @return void 141 */ 142 public function setLogLevel(string $logLevel): void 143 { 144 $this->logLevel = $logLevel; 145 } 146 147 /** 148 * 149 * Returns the log message format string, with placeholders. 150 * 151 * @return string 152 * 153 */ 154 public function getLogFormat(): string 155 { 156 return $this->logFormat; 157 } 158 159 /** 160 * 161 * Sets the log message format string, with placeholders. 162 * 163 * @param string $logFormat 164 * 165 * @return void 166 */ 167 public function setLogFormat(string $logFormat): void 168 { 169 $this->logFormat = $logFormat; 170 } 171 172 /** 173 * 174 * Starts a profile entry. 175 * 176 * @param string $function The function starting the profile entry. 177 * 178 * @return void 179 */ 180 public function start(string $function): void 181 { 182 if (! $this->active) { 183 return; 184 } 185 186 $this->context = [ 187 'function' => $function, 188 'start' => microtime(true), 189 ]; 190 } 191 192 /** 193 * 194 * Finishes and logs a profile entry. 195 * 196 * @param string|null $statement The statement being profiled, if any. 197 * 198 * @param array $values The values bound to the statement, if any. 199 * 200 * @return void 201 */ 202 public function finish(?string $statement = null, array $values = []): void 203 { 204 if (! $this->active) { 205 return; 206 } 207 208 $finish = microtime(true); 209 $e = new Exception(); 210 211 $this->context['finish'] = $finish; 212 $this->context['duration'] = $finish - $this->context['start']; 213 $this->context['statement'] = $statement; 214 $this->context['values'] = empty($values) ? '' : print_r($values, true); 215 $this->context['backtrace'] = $e->getTraceAsString(); 216 217 $this->logger->log($this->logLevel, $this->logFormat, $this->context); 218 219 $this->context = []; 220 } 221 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Apr 1 05:10:01 2025 | Cross-referenced by PHPXref 0.7.1 |