[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/error/ -> log.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: log.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package        Joomla.Framework
   5   * @subpackage    Error
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Joomla! Logging class
  20   *
  21   * This class is designed to build log files based on the
  22   * W3C specification at: http://www.w3.org/TR/WD-logfile.html
  23   *
  24   * @package     Joomla.Framework
  25   * @subpackage    Error
  26   * @since        1.5
  27   */
  28  class JLog extends JObject
  29  {
  30      /**
  31       * Log File Pointer
  32       * @var    resource
  33       */
  34      var $_file;
  35  
  36      /**
  37       * Log File Path
  38       * @var    string
  39       */
  40      var $_path;
  41  
  42      /**
  43       * Log Format
  44       * @var    string
  45       */
  46      var $_format = "{DATE}\t{TIME}\t{LEVEL}\t{C-IP}\t{STATUS}\t{COMMENT}";
  47  
  48      /**
  49       * Constructor
  50       *
  51       * @access    protected
  52       * @param    string    $path        Log file path
  53       * @param    array    $options    Log file options
  54       * @since    1.5
  55       */
  56  	function __construct($path, $options)
  57      {
  58          // Set default values
  59          $this->_path = $path;
  60          $this->setOptions($options);
  61      }
  62  
  63      /**
  64       * Returns a reference to the global log object, only creating it
  65       * if it doesn't already exist.
  66       *
  67       * This method must be invoked as:
  68       *         <pre>  $log = & JLog::getInstance();</pre>
  69       *
  70       * @access    public
  71       * @static
  72       * @return    object    The JLog object.
  73       * @since    1.5
  74       */
  75      function & getInstance($file = 'error.php', $options = null, $path = null)
  76      {
  77          static $instances;
  78  
  79          // Set default path if not set
  80          if (!$path)
  81          {
  82              $config =& JFactory::getConfig();
  83              $path = $config->getValue('config.log_path');
  84          }
  85  
  86          jimport('joomla.filesystem.path');
  87          $path = JPath :: clean($path . DS . $file);
  88          $sig = md5($path);
  89  
  90          if (!isset ($instances)) {
  91              $instances = array ();
  92          }
  93  
  94          if (empty ($instances[$sig])) {
  95              $instances[$sig] = new JLog($path, $options);
  96          }
  97  
  98          return $instances[$sig];
  99      }
 100  
 101      /**
 102       * Set log file options
 103       *
 104       * @access    public
 105       * @param    array    $options    Associative array of options to set
 106       * @return    boolean                True if successful
 107       * @since    1.5
 108       */
 109  	function setOptions($options) {
 110  
 111          if (isset ($options['format'])) {
 112              $this->_format = $options['format'];
 113          }
 114          return true;
 115      }
 116  
 117  	function addEntry($entry)
 118      {
 119          // Set some default field values if not already set.
 120          $date =& JFactory::getDate();
 121          if (!isset ($entry['date'])) {
 122  
 123              $entry['date'] = $date->toFormat("%Y-%m-%d");
 124          }
 125          if (!isset ($entry['time'])) {
 126  
 127              $entry['time'] = $date->toFormat("%H:%M:%S");
 128          }
 129          if (!isset ($entry['c-ip'])) {
 130              $entry['c-ip'] = $_SERVER['REMOTE_ADDR'];
 131          }
 132  
 133          // Ensure that the log entry keys are all uppercase
 134          $entry = array_change_key_case($entry, CASE_UPPER);
 135  
 136          // Find all fields in the format string
 137          $fields = array ();
 138          $regex = "/{(.*?)}/i";
 139          preg_match_all($regex, $this->_format, $fields);
 140  
 141          // Fill in the field data
 142          $line = $this->_format;
 143          for ($i = 0; $i < count($fields[0]); $i++)
 144          {
 145              $line = str_replace($fields[0][$i], (isset ($entry[$fields[1][$i]])) ? $entry[$fields[1][$i]] : "-", $line);
 146          }
 147  
 148          // Write the log entry line
 149          if ($this->_openLog())
 150          {
 151              if (!fputs($this->_file, "\n" . $line)) {
 152                  return false;
 153              }
 154          } else {
 155              return false;
 156          }
 157          return true;
 158      }
 159  
 160      /**
 161       * Open the log file pointer and create the file if it doesn't exist
 162       *
 163       * @access     public
 164       * @return     boolean    True on success
 165       * @since    1.5
 166       */
 167  	function _openLog()
 168      {
 169          // Only open if not already opened...
 170          if (is_resource($this->_file)) {
 171              return true;
 172          }
 173  
 174          $now =& JFactory::getDate();
 175          $date = $now->toMySQL();
 176  
 177          if (!file_exists($this->_path))
 178          {
 179              jimport("joomla.filesystem.folder");
 180              if (!JFolder :: create(dirname($this->_path))) {
 181                  return false;
 182              }
 183              $header[] = "#<?php die('Direct Access To Log Files Not Permitted'); ?>";
 184              $header[] = "#Version: 1.0";
 185              $header[] = "#Date: " . $date;
 186  
 187              // Prepare the fields string
 188              $fields = str_replace("{", "", $this->_format);
 189              $fields = str_replace("}", "", $fields);
 190              $fields = strtolower($fields);
 191              $header[] = "#Fields: " . $fields;
 192  
 193              // Prepare the software string
 194              $version = new JVersion();
 195              $header[] = "#Software: " . $version->getLongVersion();
 196  
 197              $head = implode("\n", $header);
 198          } else {
 199              $head = false;
 200          }
 201  
 202          if (!$this->_file = fopen($this->_path, "a")) {
 203              return false;
 204          }
 205          if ($head)
 206          {
 207              if (!fputs($this->_file, $head)) {
 208                  return false;
 209              }
 210          }
 211  
 212          // If we opened the file lets make sure we close it
 213          register_shutdown_function(array(&$this,'_closeLog'));
 214          return true;
 215      }
 216  
 217      /**
 218       * Close the log file pointer
 219       *
 220       * @access     public
 221       * @return     boolean    True on success
 222       * @since    1.5
 223       */
 224  	function _closeLog()
 225      {
 226          if (is_resource($this->_file)) {
 227              fclose($this->_file);
 228          }
 229          return true;
 230      }
 231  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1