Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/utilities/log.php

Documentation is available at log.php

  1. <?php
  2. /**
  3.  * @version        $Id: log.php 6775 2007-03-05 03:20:15Z friesengeist $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Utilities
  6.  * @copyright    Copyright (C) 2005 - 2007 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.  * @author        Louis Landry <[email protected]>
  25.  * @package     Joomla.Framework
  26.  * @subpackage    Utilities
  27.  * @since        1.5
  28.  */
  29. class JLog extends JObject
  30. {
  31.     /**
  32.      * Log File Pointer
  33.      * @var    resource 
  34.      */
  35.     var $_file;
  36.  
  37.     /**
  38.      * Log File Path
  39.      * @var    string 
  40.      */
  41.     var $_path;
  42.  
  43.     /**
  44.      * Log Format
  45.      * @var    string 
  46.      */
  47.     var $_format = "{DATE}\t{TIME}\t{LEVEL}\t{C-IP}\t{STATUS}\t{COMMENT}";
  48.  
  49.     /**
  50.      * Constructor
  51.      *
  52.      * @access    protected
  53.      * @param    string    $path        Log file path
  54.      * @param    array    $options    Log file options
  55.      * @since    1.5
  56.      */
  57.     function __construct($path$options)
  58.     {
  59.         // Set default values
  60.         $this->_path = $path;
  61.         $this->setOptions($options);
  62.     }
  63.  
  64.     /**
  65.      * Returns a reference to the global log object, only creating it
  66.      * if it doesn't already exist.
  67.      *
  68.      * This method must be invoked as:
  69.      *         <pre>  $log = & JLog::getInstance();</pre>
  70.      *
  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.             $config =JFactory::getConfig();
  82.             $path $config->getValue('config.log_path');
  83.         }
  84.  
  85.         jimport('joomla.filesystem.path');
  86.         $path JPath :: clean($path DS $file);
  87.         $sig md5($path);
  88.  
  89.         if (!isset ($instances)) {
  90.             $instances array ();
  91.         }
  92.  
  93.         if (empty ($instances[$sig])) {
  94.             $instances[$signew JLog($path$options);
  95.         }
  96.  
  97.         return $instances[$sig];
  98.     }
  99.  
  100.     /**
  101.      * Set log file options
  102.      *
  103.      * @access    public
  104.      * @param    array    $options    Associative array of options to set
  105.      * @return    boolean                True if successful
  106.      * @since    1.5
  107.      */
  108.     function setOptions($options{
  109.  
  110.         if (isset ($options['format'])) {
  111.             $this->_format = $options['format'];
  112.         }
  113.         return true;
  114.     }
  115.  
  116.     function addEntry($entry)
  117.     {
  118.         // Set some default field values if not already set.
  119.         if (!isset ($entry['date'])) {
  120.             $entry['date'date("Y-m-d");
  121.         }
  122.         if (!isset ($entry['time'])) {
  123.             $entry['time'date("H:i:s");
  124.         }
  125.         if (!isset ($entry['c-ip'])) {
  126.             $entry['c-ip'$_SERVER['REMOTE_ADDR'];
  127.         }
  128.  
  129.         // Ensure that the log entry keys are all uppercase
  130.         $entry array_change_key_case($entryCASE_UPPER);
  131.  
  132.         // Find all fields in the format string
  133.         $fields array ();
  134.         $regex "/{(.*?)}/i";
  135.         preg_match_all($regex$this->_format$fields);
  136.  
  137.         // Fill in the field data
  138.         $line $this->_format;
  139.         for ($i 0$i count($fields[0])$i++)
  140.         {
  141.             $line str_replace($fields[0][$i](isset ($entry[$fields[1][$i]])) $entry[$fields[1][$i]] "-"$line);
  142.         }
  143.  
  144.         // Write the log entry line
  145.         if ($this->_openLog())
  146.         {
  147.             if (!fputs($this->_file"\n" $line)) {
  148.                 return false;
  149.             }
  150.         else {
  151.             return false;
  152.         }
  153.         return true;
  154.     }
  155.  
  156.     /**
  157.      * Open the log file pointer and create the file if it doesn't exist
  158.      *
  159.      * @access     public
  160.      * @return     boolean    True on success
  161.      * @since    1.5
  162.      */
  163.     function _openLog()
  164.     {
  165.         // Only open if not already opened...
  166.         if (is_resource($this->_file)) {
  167.             return true;
  168.         }
  169.  
  170.         $date date("Y-m-d");
  171.         $time date("H:i:s");
  172.         if (!file_exists($this->_path))
  173.         {
  174.             jimport("joomla.filesystem.folder");
  175.             if (!JFolder :: create(dirname($this->_path))) {
  176.                 return false;
  177.             }
  178.             $header["#<?php die(); ?>";
  179.             $header["#Version: 1.0";
  180.             $header["#Date: " $date " " $time;
  181.  
  182.             // Prepare the fields string
  183.             $fields str_replace("{"""$this->_format);
  184.             $fields str_replace("}"""$fields);
  185.             $fields strtolower($fields);
  186.             $header["#Fields: " $fields;
  187.  
  188.             // Prepare the software string
  189.             jimport("joomla.version");
  190.             $version new JVersion();
  191.             $header["#Software: " $version->getLongVersion();
  192.  
  193.             $head implode("\n"$header);
  194.         else {
  195.             $head false;
  196.         }
  197.  
  198.         if (!$this->_file = fopen($this->_path"a")) {
  199.             return false;
  200.         }
  201.         if ($head)
  202.         {
  203.             if (!fputs($this->_file$head)) {
  204.                 return false;
  205.             }
  206.         }
  207.  
  208.         // If we opened the file lets make sure we close it
  209.         register_shutdown_function(array(&$this,'_closeLog'));
  210.         return true;
  211.     }
  212.  
  213.     /**
  214.      * Close the log file pointer
  215.      *
  216.      * @access     public
  217.      * @return     boolean    True on success
  218.      * @since    1.5
  219.      */
  220.     function _closeLog()
  221.     {
  222.         if (is_resource($this->_file)) {
  223.             fclose($this->_file);
  224.         }
  225.         return true;
  226.     }
  227. }
  228. ?>

Documentation generated on Mon, 05 Mar 2007 21:09:58 +0000 by phpDocumentor 1.3.1