[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/aura/sql/src/Profiler/ -> Profiler.php (source)

   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 $context = [];
  32  
  33      /**
  34       *
  35       * Log profile data through this interface.
  36       *
  37       * @var LoggerInterface
  38       *
  39       */
  40      protected $logger;
  41  
  42      /**
  43       *
  44       * Turns profile logging off and on.
  45       *
  46       * @var bool
  47       *
  48       * @see setActive()
  49       *
  50       */
  51      protected $active = false;
  52  
  53      /**
  54       *
  55       * The log level for all messages.
  56       *
  57       * @var string
  58       *
  59       * @see setLogLevel()
  60       *
  61       */
  62      protected $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 $logFormat = "{function} ({duration} seconds): {statement} {backtrace}";
  74  
  75      /**
  76       *
  77       * Constructor.
  78       *
  79       * @param LoggerInterface $logger Record profiles through this interface.
  80       *
  81       */
  82      public function __construct(LoggerInterface $logger = null)
  83      {
  84          if ($logger === null) {
  85              $logger = new MemoryLogger();
  86          }
  87          $this->logger = $logger;
  88      }
  89  
  90      /**
  91       *
  92       * Enable or disable profiler logging.
  93       *
  94       * @param bool $active
  95       *
  96       */
  97      public function setActive($active)
  98      {
  99          $this->active = (bool) $active;
 100      }
 101  
 102      /**
 103       *
 104       * Returns true if logging is active.
 105       *
 106       * @return bool
 107       *
 108       */
 109      public function isActive()
 110      {
 111          return $this->active;
 112      }
 113  
 114      /**
 115       *
 116       * Returns the underlying logger instance.
 117       *
 118       * @return \Psr\Log\LoggerInterface
 119       *
 120       */
 121      public function getLogger()
 122      {
 123          return $this->logger;
 124      }
 125  
 126      /**
 127       *
 128       * Returns the level at which to log profile messages.
 129       *
 130       * @return string
 131       *
 132       */
 133      public function getLogLevel()
 134      {
 135          return $this->logLevel;
 136      }
 137  
 138      /**
 139       *
 140       * Level at which to log profile messages.
 141       *
 142       * @param string $logLevel A PSR LogLevel constant.
 143       *
 144       * @return null
 145       *
 146       */
 147      public function setLogLevel($logLevel)
 148      {
 149          $this->logLevel = $logLevel;
 150      }
 151  
 152      /**
 153       *
 154       * Returns the log message format string, with placeholders.
 155       *
 156       * @return string
 157       *
 158       */
 159      public function getLogFormat()
 160      {
 161          return $this->logFormat;
 162      }
 163  
 164      /**
 165       *
 166       * Sets the log message format string, with placeholders.
 167       *
 168       * @param string $logFormat
 169       *
 170       * @return null
 171       *
 172       */
 173      public function setLogFormat($logFormat)
 174      {
 175          $this->logFormat = $logFormat;
 176      }
 177  
 178      /**
 179       *
 180       * Starts a profile entry.
 181       *
 182       * @param string $function The function starting the profile entry.
 183       *
 184       * @return null
 185       *
 186       */
 187      public function start($function)
 188      {
 189          if (! $this->active) {
 190              return;
 191          }
 192  
 193          $this->context = [
 194              'function' => $function,
 195              'start' => microtime(true),
 196          ];
 197      }
 198  
 199      /**
 200       *
 201       * Finishes and logs a profile entry.
 202       *
 203       * @param string $statement The statement being profiled, if any.
 204       *
 205       * @param array $values The values bound to the statement, if any.
 206       *
 207       * @return null
 208       *
 209       */
 210      public function finish($statement = null, array $values = [])
 211      {
 212          if (! $this->active) {
 213              return;
 214          }
 215  
 216          $finish = microtime(true);
 217          $e = new Exception();
 218  
 219          $this->context['finish'] = $finish;
 220          $this->context['duration'] = $finish - $this->context['start'];
 221          $this->context['statement'] = $statement;
 222          $this->context['values'] = empty($values) ? '' : print_r($values, true);
 223          $this->context['backtrace'] = $e->getTraceAsString();
 224  
 225          $this->logger->log($this->logLevel, $this->logFormat, $this->context);
 226  
 227          $this->context = [];
 228      }
 229  }


Generated: Sat Nov 9 05:10:01 2024 Cross-referenced by PHPXref 0.7.1