home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Log.php < prev    next >
Encoding:
PHP Script  |  2001-11-13  |  6.9 KB  |  197 lines

  1. <?php
  2. // $Id: Log.php,v 1.2.6.1 2001/11/13 01:26:39 ssb Exp $
  3. // $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
  4.  
  5. /**
  6.  * The Log:: class implements both an abstraction for various logging
  7.  * mechanisms and the Subject end of a Subject-Observer pattern.
  8.  *
  9.  * @author  Chuck Hagenbuch <chuck@horde.org>
  10.  * @author  Jon Parise <jon@csh.rit.edu>
  11.  * @version $Revision: 1.2.6.1 $
  12.  * @since   Horde 1.3
  13.  */
  14. class Log {
  15.  
  16.     // {{{ properties
  17.  
  18.     /** Boolean indicating whether or not the log connection is
  19.         currently open. */
  20.     var $opened = false;
  21.  
  22.     /** String holding the identifier that will be stored along with
  23.         each logged message. */
  24.     var $ident = '';
  25.  
  26.     /** Array holding all Log_observer objects that wish to be notified
  27.         of any messages that we handle. */
  28.     var $listeners = array();
  29.  
  30.     // }}}
  31.  
  32.     // {{{ factory()
  33.     /**
  34.      * Attempts to return a concrete Log instance of $log_type.
  35.      * 
  36.      * @param $log_type The type of concrete Log subclass to return.
  37.      *                  Attempt to dynamically include the code for this
  38.      *                  subclass. Currently, valid values are 'syslog',
  39.      *                  'sql', 'file', and 'mcal'.
  40.      *
  41.      * @param $log_name (optional) The name of the actually log file,
  42.      *                  table, or other specific store to use. Defaults
  43.      *                  to an empty string, with which the subclass will
  44.      *                  attempt to do something intelligent.
  45.      *
  46.      * @param $ident    (optional) The indentity reported to the log system.
  47.      *
  48.      * @param $conf     (optional) A hash containing any additional
  49.      *                  configuration information that a subclass might need.
  50.      * 
  51.      * @return          The newly created concrete Log instance, or an
  52.      *                  false on an error.
  53.      */
  54.     function &factory($log_type, $log_name = '', $ident = '', $conf = array())
  55.     {
  56.         $log_type = strtolower($log_type);
  57.         $classfile = 'Log/' . $log_type . '.php';
  58.         @include_once $classfile;
  59.         $class = 'Log_' . $log_type;
  60.         if (class_exists($class)) {
  61.             return new $class($log_name, $ident, $conf);
  62.         } else {
  63.             return false;
  64.         }
  65.     }
  66.     // }}}
  67.  
  68.     // {{{ singleton()
  69.     /**
  70.      * Attempts to return a reference to a concrete Log instance of
  71.      * $log_type, only creating a new instance if no log instance with
  72.      * the same parameters currently exists.
  73.      *
  74.      * You should use this if there are multiple places you might
  75.      * create a logger, you don't want to create multiple loggers, and
  76.      * you don't want to check for the existance of one each time. The
  77.      * singleton pattern does all the checking work for you.
  78.      *
  79.      * <b>You MUST call this method with the $var = &Log::singleton()
  80.      * syntax. Without the ampersand (&) in front of the method name,
  81.      * you will not get a reference, you will get a copy.</b>
  82.      * 
  83.      * @param $log_type The type of concrete Log subclass to return.
  84.      *                  Attempt to dynamically include the code for
  85.      *                  this subclass. Currently, valid values are
  86.      *                  'syslog', 'sql', 'file', and 'mcal'.
  87.      *
  88.      * @param $log_name (optional) The name of the actually log file,
  89.      *                  table, or other specific store to use.  Defaults
  90.      *                  to an empty string, with which the subclass will
  91.      *                  attempt to do something intelligent.
  92.      *
  93.      * @param $ident    (optional) The identity reported to the log system.
  94.      *
  95.      * @param $conf     (optional) A hash containing any additional
  96.      *                  configuration information that a subclass might need.
  97.      * 
  98.      * @return          The concrete Log reference, or false on an error.
  99.      */
  100.     function &singleton ($log_type, $log_name = '', $ident = '', $conf = array()) {
  101.         static $instances;
  102.         if (!isset($instances)) $instances = array();
  103.         
  104.         $signature = md5($log_type . '][' . $log_name . '][' . $ident . '][' . implode('][', $conf));
  105.         if (!isset($instances[$signature])) {
  106.             $instances[$signature] = &Log::factory($log_type, $log_name, $ident, $conf);
  107.         }
  108.         return $instances[$signature];
  109.     }
  110.     // }}}
  111.  
  112.     // {{{ priorityToString()
  113.     /**
  114.      * Returns the string representation of a LOG_* integer constant.
  115.      *
  116.      * @param $priority The LOG_* integer constant.
  117.      *
  118.      * @return          The string representation of $priority.
  119.      */
  120.     function priorityToString ($priority) {
  121.         $priorities = array(
  122.             LOG_EMERG   => 'emergency',
  123.             LOG_ALERT   => 'alert',
  124.             LOG_CRIT    => 'critical',
  125.             LOG_ERR     => 'error',
  126.             LOG_WARNING => 'warning',
  127.             LOG_NOTICE  => 'notice',
  128.             LOG_INFO    => 'info',
  129.             LOG_DEBUG   => 'debug'
  130.         );
  131.         return $priorities[$priority];
  132.     }
  133.     // }}}
  134.  
  135.     // {{{ attach()
  136.     /**
  137.      * Adds a Log_observer instance to the list of observers that are
  138.      * be notified when a message is logged.
  139.      *  
  140.      * @param $logObserver  The Log_observer instance to be added to
  141.      *                      the $listeners array.
  142.      */
  143.     function attach (&$logObserver) {
  144.         if (!is_object($logObserver))
  145.             return false;
  146.         
  147.         $logObserver->_listenerID = uniqid(rand());
  148.         
  149.         $this->listeners[$logObserver->_listenerID] = &$logObserver;
  150.     }
  151.     // }}}
  152.  
  153.     // {{{ detach()
  154.     /**
  155.      * Removes a Log_observer instance from the list of observers.
  156.      *
  157.      * @param $logObserver  The Log_observer instance to be removed
  158.      *                      from the $listeners array.
  159.      */
  160.     function detach ($logObserver) {
  161.         if (isset($this->listeners[$logObserver->_listenerID]))
  162.             unset($this->listeners[$logObserver->_listenerID]);
  163.     }
  164.     // }}}
  165.  
  166.     // {{{ notifyAll()
  167.     /**
  168.      * Sends any Log_observer objects listening to this Log the message
  169.      * that was just logged.
  170.      *
  171.      * @param $messageOb    The data structure holding all relevant log
  172.      *                      information - the message, the priority, what
  173.      *                      log this is, etc.
  174.      */
  175.     function notifyAll ($messageOb) {
  176.         reset($this->listeners);
  177.         foreach ($this->listeners as $listener) {
  178.             if ($messageOb['priority'] <= $listener->priority)
  179.                 $listener->notify($messageOb);
  180.         }
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ isComposite()
  185.     /**
  186.      * @return a Boolean: true if this is a composite class, false
  187.      * otherwise. The composite subclass overrides this to return
  188.      * true.
  189.      */
  190.     function isComposite () {
  191.         return false;
  192.     }
  193.     // }}}
  194. }
  195.  
  196. ?>
  197.