[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/Database/ -> Logger.php (source)

   1  <?php
   2  
   3  /**
   4   * Custom logger for YOURLS that logs debug messages and queries.
   5   *
   6   * Based on \Aura\Sql\Profiler\MemoryLogger
   7   *
   8   * @since 1.7.10
   9   */
  10  
  11  namespace YOURLS\Database;
  12  
  13  use Psr\Log\AbstractLogger;
  14  
  15  class Logger extends AbstractLogger {
  16      /**
  17       * Log messages.
  18       *
  19       * @var array
  20       */
  21      protected $messages = [];
  22  
  23      /**
  24       * Logs a message.
  25       *
  26       * @param string  $level    The log level (ie type of message)
  27       * @param string  $message  The log message.
  28       * @param array   $context  Data to interpolate into the message.
  29       *
  30       * The logger receives the following:
  31       *
  32       * From yourls_debug("something went wrong") :
  33       *    $level   : string 'debug'
  34       *    $message : string 'something went wrong'
  35       *    $context : array()
  36       * See yourls_debug() in includes/functions-debug.php
  37       *
  38       * From a query that triggers the internal logging of Aura SQL :
  39       *    $level   : string 'query'
  40       *    $message : string '{function} ({duration} seconds): {statement} {backtrace}'
  41       *               (which is the default $logFormat from Aura\Sql\Profiler\Profiler), we're not using it)
  42       *    $context : array(
  43       *               'function' => string 'perform'
  44       *               'duration' => float 0.0025360584259033
  45       *               'statement' => string 'SELECT `keyword`,`url` FROM `yourls_url` WHERE `url` LIKE (:url)'
  46       *               'values' => array('url' => '%rss%')
  47       *               )
  48       * See finish() in Aura\Sql\Profiler\Profiler
  49       *
  50       * @return void
  51       */
  52      public function log($level, $message, array $context = []) {
  53          // if it's an internal SQL query, format the message, otherwise store a string
  54          if($level === 'query') {
  55              $this->messages[] = sprintf(
  56                  'SQL %s: %s (%s s)',
  57                  $context['function'],
  58                  $this->pretty_format($context['statement'], $context['values']),
  59                  number_format($context['duration'], 5)
  60              );
  61          } else {
  62              $this->messages[] = (string)$message;
  63          }
  64      }
  65  
  66      /**
  67       * Returns the logged messages.
  68       *
  69       * @return array
  70       */
  71      public function getMessages() {
  72          return $this->messages;
  73      }
  74  
  75      /**
  76       * Format PDO statement with bind/values replacement
  77       *
  78       * This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value')
  79       * This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes)
  80       *
  81       * @since  1.7.3
  82       * @param  string $statement  SQL query with PDO style named placeholders
  83       * @param  array  $values     Optional array of values corresponding to placeholders
  84       * @return string             Readable SQL query with placeholders replaced
  85       */
  86      public function pretty_format($statement, array $values = array() ) {
  87          if (!$values) {
  88              return $statement;
  89          }
  90  
  91          return preg_replace_callback(
  92              '/:([^\s;)]*)/',
  93  
  94              /**
  95               * @param string $matches
  96               */
  97              function ($matches) use ($values) {
  98                  $replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : '';
  99                  if(is_array($replacement)) {
 100                      $replacement = implode(",", $replacement);
 101                  }
 102                  return "'$replacement'";
 103              },
 104              $statement
 105          );
 106      }
 107  
 108  }


Generated: Sat Feb 22 05:10:06 2025 Cross-referenced by PHPXref 0.7.1