[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/log/loggers/ -> database.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Log
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.log.logger');
  13  
  14  /**
  15   * Joomla! MySQL Database Log class
  16   *
  17   * This class is designed to output logs to a specific MySQL database table. Fields in this
  18   * table are based on the SysLog style of log output. This is designed to allow quick and
  19   * easy searching.
  20   *
  21   * @package     Joomla.Platform
  22   * @subpackage  Log
  23   * @since       11.1
  24   */
  25  class JLoggerDatabase extends JLogger
  26  {
  27      /**
  28       * @var    string  The name of the database driver to use for connecting to the database.
  29       * @since  11.1
  30       */
  31      protected $driver = 'mysql';
  32  
  33      /**
  34       * @var    string  The host name (or IP) of the server with which to connect for the logger.
  35       * @since  11.1
  36       */
  37      protected $host = '127.0.0.1';
  38  
  39      /**
  40       * @var    string  The database server user to connect as for the logger.
  41       * @since  11.1
  42       */
  43      protected $user = 'root';
  44  
  45      /**
  46       * @var    string  The password to use for connecting to the database server.
  47       * @since  11.1
  48       */
  49      protected $password = '';
  50  
  51      /**
  52       * @var    string  The name of the database table to use for the logger.
  53       * @since  11.1
  54       */
  55      protected $database = 'logging';
  56  
  57      /**
  58       * @var    string  The database table to use for logging entries.
  59       * @since  11.1
  60       */
  61      protected $table = 'jos_';
  62  
  63      /**
  64       * @var    JDatabase  The database connection object for the logger.
  65       * @since  11.1
  66       */
  67      protected $dbo;
  68  
  69      /**
  70       * Constructor.
  71       *
  72       * @param   array  &$options  Log object options.
  73       *
  74       * @since   11.1
  75       * @throws  LogException
  76       */
  77  	public function __construct(array &$options)
  78      {
  79          // Call the parent constructor.
  80          parent::__construct($options);
  81  
  82          // If both the database object and driver options are empty we want to use the system database connection.
  83          if (empty($this->options['db_object']) && empty($this->options['db_driver']))
  84          {
  85              $this->dbo = JFactory::getDBO();
  86              $this->driver = JFactory::getConfig()->get('dbtype');
  87              $this->host = JFactory::getConfig()->get('host');
  88              $this->user = JFactory::getConfig()->get('user');
  89              $this->password = JFactory::getConfig()->get('password');
  90              $this->database = JFactory::getConfig()->get('db');
  91              $this->prefix = JFactory::getConfig()->get('dbprefix');
  92          }
  93          // We need to get the database connection settings from the configuration options.
  94          else
  95          {
  96              $this->driver = (empty($this->options['db_driver'])) ? 'mysql' : $this->options['db_driver'];
  97              $this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
  98              $this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
  99              $this->password = (empty($this->options['db_pass'])) ? '' : $this->options['db_pass'];
 100              $this->database = (empty($this->options['db_database'])) ? 'logging' : $this->options['db_database'];
 101              $this->prefix = (empty($this->options['db_prefix'])) ? 'jos_' : $this->options['db_prefix'];
 102          }
 103  
 104          // The table name is independent of how we arrived at the connection object.
 105          $this->table = (empty($this->options['db_table'])) ? '#__log_entries' : $this->options['db_table'];
 106      }
 107  
 108      /**
 109       * Method to add an entry to the log.
 110       *
 111       * @param   JLogEntry  $entry  The log entry object to add to the log.
 112       *
 113       * @return  void
 114       *
 115       * @since   11.1
 116       */
 117  	public function addEntry(JLogEntry $entry)
 118      {
 119          // Connect to the database if not connected.
 120          if (empty($this->dbo))
 121          {
 122              $this->connect();
 123          }
 124  
 125          // Convert the date.
 126          $entry->date = $entry->date->toSql();
 127  
 128          $this->dbo->insertObject($this->table, $entry);
 129      }
 130  
 131      /**
 132       * Method to connect to the database server based on object properties.
 133       *
 134       * @return  void
 135       *
 136       * @since   11.1
 137       * @throws  LogException
 138       */
 139  	protected function connect()
 140      {
 141          // Build the configuration object to use for JDatabase.
 142          $options = array(
 143              'driver' => $this->driver,
 144              'host' => $this->host,
 145              'user' => $this->user,
 146              'password' => $this->password,
 147              'database' => $this->database,
 148              'prefix' => $this->prefix);
 149  
 150          try
 151          {
 152              $db = JDatabase::getInstance($options);
 153  
 154              if ($db instanceof Exception)
 155              {
 156                  throw new LogException('Database Error: ' . (string) $db);
 157              }
 158  
 159              if ($db->getErrorNum() > 0)
 160              {
 161                  throw new LogException(JText::sprintf('JLIB_UTIL_ERROR_CONNECT_DATABASE', $db->getErrorNum(), $db->getErrorMsg()));
 162              }
 163  
 164              // Assign the database connector to the class.
 165              $this->dbo = $db;
 166          }
 167          catch (JDatabaseException $e)
 168          {
 169              throw new LogException($e->getMessage());
 170          }
 171      }
 172  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1