[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Log/Logger/ -> DatabaseLogger.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Log\Logger;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Log\LogEntry;
  14  use Joomla\CMS\Log\Logger;
  15  use Joomla\Database\DatabaseDriver;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Joomla! MySQL Database Log class
  23   *
  24   * This class is designed to output logs to a specific MySQL database table. Fields in this
  25   * table are based on the Syslog style of log output. This is designed to allow quick and
  26   * easy searching.
  27   *
  28   * @since  1.7.0
  29   */
  30  class DatabaseLogger extends Logger
  31  {
  32      /**
  33       * The name of the database driver to use for connecting to the database.
  34       *
  35       * @var    string
  36       * @since  1.7.0
  37       */
  38      protected $driver = 'mysqli';
  39  
  40      /**
  41       * The host name (or IP) of the server with which to connect for the logger.
  42       *
  43       * @var    string
  44       * @since  1.7.0
  45       */
  46      protected $host = '127.0.0.1';
  47  
  48      /**
  49       * The database server user to connect as for the logger.
  50       *
  51       * @var    string
  52       * @since  1.7.0
  53       */
  54      protected $user = 'root';
  55  
  56      /**
  57       * The password to use for connecting to the database server.
  58       *
  59       * @var    string
  60       * @since  1.7.0
  61       */
  62      protected $password = '';
  63  
  64      /**
  65       * The name of the database table to use for the logger.
  66       *
  67       * @var    string
  68       * @since  1.7.0
  69       */
  70      protected $database = 'logging';
  71  
  72      /**
  73       * The database table to use for logging entries.
  74       *
  75       * @var    string
  76       * @since  1.7.0
  77       */
  78      protected $table = 'jos_';
  79  
  80      /**
  81       * The database driver object for the logger.
  82       *
  83       * @var    DatabaseDriver
  84       * @since  1.7.0
  85       */
  86      protected $db;
  87  
  88      /**
  89       * Constructor.
  90       *
  91       * @param   array  &$options  Log object options.
  92       *
  93       * @since   1.7.0
  94       */
  95      public function __construct(array &$options)
  96      {
  97          // Call the parent constructor.
  98          parent::__construct($options);
  99  
 100          // If both the database object and driver options are empty we want to use the system database connection.
 101          if (empty($this->options['db_driver'])) {
 102              $this->db = Factory::getDbo();
 103              $this->driver = null;
 104              $this->host = null;
 105              $this->user = null;
 106              $this->password = null;
 107              $this->database = null;
 108              $this->prefix = null;
 109          } else {
 110              $this->db = null;
 111              $this->driver = (empty($this->options['db_driver'])) ? 'mysqli' : $this->options['db_driver'];
 112              $this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
 113              $this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
 114              $this->password = (empty($this->options['db_pass'])) ? '' : $this->options['db_pass'];
 115              $this->database = (empty($this->options['db_database'])) ? 'logging' : $this->options['db_database'];
 116              $this->prefix = (empty($this->options['db_prefix'])) ? 'jos_' : $this->options['db_prefix'];
 117          }
 118  
 119          // The table name is independent of how we arrived at the connection object.
 120          $this->table = (empty($this->options['db_table'])) ? '#__log_entries' : $this->options['db_table'];
 121      }
 122  
 123      /**
 124       * Method to add an entry to the log.
 125       *
 126       * @param   LogEntry  $entry  The log entry object to add to the log.
 127       *
 128       * @return  void
 129       *
 130       * @since   1.7.0
 131       * @throws  \RuntimeException
 132       */
 133      public function addEntry(LogEntry $entry)
 134      {
 135          // Connect to the database if not connected.
 136          if (empty($this->db)) {
 137              $this->connect();
 138          }
 139  
 140          // Convert the date.
 141          $entry->date = $entry->date->toSql(false, $this->db);
 142  
 143          $this->db->insertObject($this->table, $entry);
 144      }
 145  
 146      /**
 147       * Method to connect to the database server based on object properties.
 148       *
 149       * @return  void
 150       *
 151       * @since   1.7.0
 152       * @throws  \RuntimeException
 153       */
 154      protected function connect()
 155      {
 156          // Build the configuration object to use for DatabaseDriver.
 157          $options = array(
 158              'driver' => $this->driver,
 159              'host' => $this->host,
 160              'user' => $this->user,
 161              'password' => $this->password,
 162              'database' => $this->database,
 163              'prefix' => $this->prefix,
 164          );
 165  
 166          $this->db = DatabaseDriver::getInstance($options);
 167      }
 168  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer