cvs: php4 /pear Log.php Makefile.in /pear/Log composite.php file.php mcal.php observer.php sql.php syslog.php

From: Date: Thu, 07 Dec 2000 01:06:58 +0000
Subject: cvs: php4 /pear Log.php Makefile.in /pear/Log composite.php file.php mcal.php observer.php sql.php syslog.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-2987@lists.php.net to get a copy of this message
chagenbu Wed Dec 6 17:06:58 2000 EDT Added files: /php4/pear Log.php /php4/pear/Log composite.php file.php mcal.php observer.php sql.php syslog.php Modified files: /php4/pear Makefile.in Log: Contribute Horde's Log:: abstraction class to PEAR.

Index: php4/pear/Makefile.in diff -u php4/pear/Makefile.in:1.50 php4/pear/Makefile.in:1.51 --- php4/pear/Makefile.in:1.50 Mon Nov 6 14:33:09 2000 +++ php4/pear/Makefile.in Wed Dec 6 17:06:58 2000 @@ -16,6 +16,7 @@ DB \ File \ HTML \ + Log \ Mail \ Net \ Payment \ @@ -42,6 +43,13 @@ File/SearchReplace.php \ HTML/Form.php \ HTTP.php \ + Log.php + Log/composite.php \ + Log/file.php \ + Log/mcal.php \ + Log/observer.php \ + Log/sql.php \ + Log/syslog.php \ Mail.php \ Mail/sendmail.php \ Mail/smtp.php \ Index: php4/pear/Log.php +++ php4/pear/Log.php <?php // $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $ /** * The Log:: class implements both an abstraction for various logging * mechanisms and the Subject end of a Subject-Observer pattern. * * @author Chuck Hagenbuch <chuck@horde.org> * @author Jon Parise <jon@csh.rit.edu> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log { // {{{ properties /** Boolean indicating whether or not the log connection is currently open. */ var $opened = false; /** String holding the identifier that will be stored along with each logged message. */ var $ident = ''; /** Array holding all Log_observer objects that wish to be notified of any messages that we handle. */ var $listeners = array(); // }}} // {{{ factory() /** * Attempts to return a concrete Log instance of $log_type. * * @param $log_type The type of concrete Log subclass to return. * Attempt to dynamically include the code for this * subclass. Currently, valid values are 'syslog', * 'sql', 'file', and 'mcal'. * * @param $log_name (optional) The name of the actually log file, * table, or other specific store to use. Defaults * to an empty string, with which the subclass will * attempt to do something intelligent. * * @param $ident (optional) The indentity reported to the log system. * * @param $conf (optional) A hash containing any additional * configuration information that a subclass might need. * * @return The newly created concrete Log instance, or an * false on an error. */ function factory ($log_type, $log_name = '', $ident = '', $conf = array()) { $log_type = strtolower($log_type); $classfile = dirname(__FILE__) . '/Log/' . $log_type . '.php'; if (@is_readable($classfile)) { include_once $classfile; $class = 'Log_' . $log_type; return new $class($log_name, $ident, $conf); } else { return false; } } // }}} // {{{ singleton() /** * Attempts to return a reference to a concrete Log instance of * $log_type, only creating a new instance if no log instance with * the same parameters currently exists. * * You should use this if there are multiple places you might * create a logger, you don't want to create multiple loggers, and * you don't want to check for the existance of one each time. The * singleton pattern does all the checking work for you. * * <b>You MUST call this method with the $var = &Log::singleton() * syntax. Without the ampersand (&) in front of the method name, * you will not get a reference, you will get a copy.</b> * * @param $log_type The type of concrete Log subclass to return. * Attempt to dynamically include the code for * this subclass. Currently, valid values are * 'syslog', 'sql', 'file', and 'mcal'. * * @param $log_name (optional) The name of the actually log file, * table, or other specific store to use. Defaults * to an empty string, with which the subclass will * attempt to do something intelligent. * * @param $ident (optional) The identity reported to the log system. * * @param $conf (optional) A hash containing any additional * configuration information that a subclass might need. * * @return The concrete Log reference, or false on an error. */ function &singleton ($log_type, $log_name = '', $ident = '', $conf = array()) { static $instances; if (!isset($instances)) $instances = array(); $signature = md5($log_type . '][' . $log_name . '][' . $ident . '][' . implode('][', $conf)); if (!isset($instances[$signature])) { $instances[$signature] = Log::factory($log_type, $log_name, $ident, $conf); } return $instances[$signature]; } // }}} // {{{ priorityToString() /** * Returns the string representation of a LOG_* integer constant. * * @param $priority The LOG_* integer constant. * * @return The string representation of $priority. */ function priorityToString ($priority) { $priorities = array( LOG_EMERG => 'emergency', LOG_ALERT => 'alert', LOG_CRIT => 'critical', LOG_ERR => 'error', LOG_WARNING => 'warning', LOG_NOTICE => 'notice', LOG_INFO => 'info', LOG_DEBUG => 'debug' ); return $priorities[$priority]; } // }}} // {{{ attach() /** * Adds a Log_observer instance to the list of observers that are * be notified when a message is logged. * * @param $logObserver The Log_observer instance to be added to * the $listeners array. */ function attach (&$logObserver) { if (!is_object($logObserver)) return false; $logObserver->_listenerID = uniqid(rand()); $this->listeners[$logObserver->_listenerID] = &$logObserver; } // }}} // {{{ detach() /** * Removes a Log_observer instance from the list of observers. * * @param $logObserver The Log_observer instance to be removed * from the $listeners array. */ function detach ($logObserver) { if (isset($this->listeners[$logObserver->_listenerID])) unset($this->listeners[$logObserver->_listenerID]); } // }}} // {{{ notifyAll() /** * Sends any Log_observer objects listening to this Log the message * that was just logged. * * @param $messageOb The data structure holding all relevant log * information - the message, the priority, what * log this is, etc. */ function notifyAll ($messageOb) { reset($this->listeners); foreach ($this->listeners as $listener) { if ($messageOb['priority'] <= $listener->priority) $listener->notify($messageOb); } } // }}} // {{{ isComposite() /** * @return a Boolean: true if this is a composite class, false * otherwise. The composite subclass overrides this to return * true. */ function isComposite () { return false; } // }}} } ?> Index: php4/pear/Log/composite.php +++ php4/pear/Log/composite.php <?php // $Horde: horde/lib/Log/composite.php,v 1.2 2000/06/28 21:36:13 jon Exp $ /** * The Log_composite:: class implements a Composite pattern which * allows multiple Log implementations to get sent the same events. * * @author Chuck Hagenbuch <chuck@horde.org> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_composite { // {{{ properties /** Array holding all Log instances which should be sent events sent to the composite. */ var $children = array(); // }}} // {{{ constructor /** * Constructs a new composite Log object. * * @param $log_name (optional) This is ignored. * @param $ident (optional) This is ignored. * @param $conf (optional) This is ignored. */ function Log_composite ($log_name = false, $ident = false, $conf = false) { } // }}} // {{{ open() /** * Open the log connections of each and every child of this * composite. */ function open () { if (!$this->opened) { reset($this->children); foreach ($this->children as $child) { $child->open(); } } } // }}} // {{{ close() /** * If we've gone ahead and opened each child, go through and close * each child. */ function close () { if ($this->opened) { reset($this->children); foreach ($this->children as $child) { $child->close(); } } } // }}} // {{{ log() /** * Sends $message and $priority to every child of this composite. * * @param $message The textual message to be logged. * @param $priority (optional) The priority of the message. Valid * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, * and LOG_DEBUG. The default is LOG_INFO. */ function log ($message, $priority = LOG_INFO) { reset($this->children); foreach ($this->children as $child) { $child->log($message, $priority); } $this->notifyAll(array('priority' => $priority, 'message' => $message)); } // }}} // {{{ isComposite() /** * @return a Boolean: true if this is a composite class, false * otherwise. Always returns true since this is the composite * subclass. */ function isComposite () { return true; } // }}} // {{{ addChild() /** * Add a Log instance to the list of children that messages sent * to us should be passed on to. * * @param $child The Log instance to add. */ function addChild (&$child) { if (!is_object($child)) return false; $child->_childID = uniqid(rand()); $this->children[$child->_listenerID] = &$child; } // }}} // {{{ removeChild() /** * Remove a Log instance from the list of children that messages * sent to us should be passed on to. * * @param $child The Log instance to remove. */ function removeChild ($child) { if (isset($this->children[$child->_childID])) unset($this->children[$child->_childID]); } // }}} } ?> Index: php4/pear/Log/file.php +++ php4/pear/Log/file.php <?php // $Horde: horde/lib/Log/file.php,v 1.4 2000/06/28 21:36:13 jon Exp $ /** * The Log_file class is a concrete implementation of the Log:: * abstract class which writes message to a text file. * * @author Jon Parise <jon@csh.rit.edu> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_file extends Log { // {{{ properties /** String holding the filename of the logfile. */ var $filename = ''; /** Integer holding the file handle. */ var $fp = ''; // }}} // {{{ constructor /** * Constructs a new logfile object. * * @param $log_name The filename of the logfile. * @param $ident (optional) The identity string. * @param $conf (optional) The configuration array. */ function Log_file ($log_name, $ident = '', $conf = false) { $this->filename = $log_name; $this->ident = $ident; } // }}} // {{{ open() /** * Opens the logfile for appending, if it has not already been opened. * If the file doesn't already exist, attempt to create it. This is * implicitly called by log(), if necessary. */ function open () { if (!$this->opened) { $this->fp = fopen($this->filename, 'a'); $this->opened = true; } } // }}} // {{{ close() /** * Closes the logfile, if it is open. */ function close () { if ($this->opened) { fclose($this->fp); $this->opened = false; } } // }}} // {{{ log() /** * Writes $message to the currently open logfile. Calls open(), if * necessary. Also, passes the message along to any Log_observer * instances that are observing this Log. * * @param $message The textual message to be logged. * @param $priority (optional) The priority of the message. Valid * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and * LOG_DEBUG. The default is LOG_INFO. */ function log ($message, $priority = LOG_INFO) { if (!$this->opened) $this->open(); $entry = sprintf("%s %s [%s] %s\n", strftime("%b %d %T"), $this->ident, Log::priorityToString($priority), $message); if ($this->fp) fwrite($this->fp, $entry); $this->notifyAll(array('priority' => $priority, 'message' => $message)); } // }}} } ?> Index: php4/pear/Log/mcal.php +++ php4/pear/Log/mcal.php <?php // $Horde: horde/lib/Log/mcal.php,v 1.2 2000/06/28 21:36:13 jon Exp $ /** * The Log_mcal class is a concrete implementation of the Log:: * abstract class which sends messages to a local or remote calendar * store accessed through MCAL. * * @author Chuck Hagenbuch <chuck@horde.org> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_mcal extends Log { // {{{ properties /** String holding the calendar specification to connect to. */ var $calendar = '{localhost/mstore}'; /** String holding the username to use. */ var $username = ''; /** String holding the password to use. */ var $password = ''; /** Integer holding the options to pass to the calendar stream. */ var $options = 0; /** ResourceID of the MCAL stream. */ var $stream = ''; /** Integer holding the log facility to use. */ var $name = LOG_SYSLOG; // }}} // {{{ constructor /** * Constructs a new Log_mcal object. * * @param $log_name (optional) The category to use for our events. * @param $ident (optional) The identity string. * @param $conf (optional) The configuration array. */ function Log_mcal ($log_name = LOG_SYSLOG, $ident = '', $conf = false) { $this->name = $log_name; $this->ident = $ident; $this->calendar = $conf['calendar']; $this->username = $conf['username']; $this->password = $conf['password']; $this->options = $conf['options']; } // }}} // {{{ open() /** * Opens a calendar stream, if it has not already been * opened. This is implicitly called by log(), if necessary. */ function open () { if (!$this->opened) { $this->stream = mcal_open($this->calendar, $this->username, $this->password, $this->options); $this->opened = true; } } // }}} // {{{ close() /** * Closes the calendar stream, if it is open. */ function close () { if ($this->opened) { mcal_close($this->stream); $this->opened = false; } } // }}} // {{{ log() /** * Logs $message and associated information to the currently open * calendar stream. Calls open() if necessary. Also passes the * message along to any Log_observer instances that are observing * this Log. * * @param $message The textual message to be logged. * @param $priority (optional) The priority of the message. Valid * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, * and LOG_DEBUG. The default is LOG_INFO. */ function log ($message, $priority = LOG_INFO) { if (!$this->opened) $this->open(); $date_str = date('Y:n:j:G:i:s'); $dates = explode(':', $date_str); mcal_event_init($this->stream); mcal_event_set_title($this->stream, $this->ident); mcal_event_set_category($this->stream, $this->name); mcal_event_set_description($this->stream, $message); mcal_event_add_attribute($this->stream, 'priority', $priority); mcal_event_set_start($this->stream, $dates[0], $dates[1], $dates[2], $dates[3], $dates[4], $dates[5]); mcal_event_set_end($this->stream, $dates[0], $dates[1], $dates[2], $dates[3], $dates[4], $dates[5]); mcal_append_event($this->stream); $this->notifyAll(array('priority' => $priority, 'message' => $message)); } // }}} } ?> Index: php4/pear/Log/observer.php +++ php4/pear/Log/observer.php <?php // $Horde: horde/lib/Log/observer.php,v 1.5 2000/06/28 21:36:13 jon Exp $ /** * The Log_observer:: class implements the Observer end of a * Subject-Observer pattern for watching log activity and taking * actions on exceptional events. * * @author Chuck Hagenbuch <chuck@horde.org> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_observer { // {{{ properties /** The minimum priority level of message that we want to hear about. 0 (LOG_EMERG) is the highest priority, so we will only hear messages with an integer priority value less than or equal to ours. It defaults to LOG_INFO, which listens to everything except LOG_DEBUG. */ var $priority = LOG_INFO; // }}} // {{{ constructor /** * Basic Log_observer instance that just prints out messages * received. */ function Log_observer ($priority = LOG_INFO) { $this->priority = $priority; } // }}} // {{{ factory() /** * Attempts to return a concrete Log_observer instance of the * specified type. * * @param $observer_type The type of concrate Log_observer subclass * to return. We will attempt to dynamically * include the code for this subclass. * @return The newly created concrete Log_observer * instance, or an false on an error. */ function factory ($observer_type, $priority = LOG_INFO) { $classfile = substr(__FILE__, 0, -(strlen(basename(__FILE__)))) . 'Log/' . $observer_type . '.php'; if (file_exists($classfile)) { include_once $classfile; $class = 'Log_' . $observer_type; return new $observer_type($priority); } else { return false; } } // }}} // {{{ notify() /** * This is a stub method to make sure that LogObserver classes do * something when they are notified of a message. The default * behavior is to just print the message, which is obviously not * desireable in practically any situation - which is why you need * to override this method. :) * * @param $messageOb A hash containing all information - the text * message itself, the priority, what log it came * from, etc. */ function notify ($messageOb) { print_r($messageOb); } // }}} } ?> Index: php4/pear/Log/sql.php +++ php4/pear/Log/sql.php <?php // $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $ require_once 'DB.php'; /** * The Log_sql class is a concrete implementation of the Log:: * abstract class which sends messages to an SQL server. Each entry * occupies a separate row in the database. * * This implementation uses PHP's PEAR database abstraction layer. * * CREATE TABLE log_table ( * unixtime int NOT NULL, * ident char(16) NOT NULL, * priority int, * message varchar(200), * primary key (unixtime, ident) * ); * * @author Jon Parise <jon@csh.rit.edu> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_sql extends Log { // {{{ properties /** Array containing the dsn information. */ var $dsn = ''; /** Object holding the database handle. */ var $db = ''; /** String holding the database table to use. */ var $table = 'log_table'; /** Boolean indicating the current connection state. */ var $opened = false; // }}} // {{{ constructor /** * Constructs a new sql logging object. * * @param $log_name The target SQL table. * @param $ident (optional) The identification field. * @param $conf The connection configuration array. */ function Log_sql($log_name, $ident = '', $conf) { $this->table = $log_name; $this->ident = $ident; $this->dsn = $conf['dsn']; } // }}} // {{{ open() /** * Opens a connection to the database, if it has not already * been opened. This is implicitly called by log(), if necessary. * * @return True on success, false on failure. */ function open() { if (!$this->opened) { $this->db = &DB::connect($this->dsn, true); if (DB::isError($this->db) || DB::isWarning($this->db)) { return false; } $this->opened = true; } return true; } // }}} // {{{ close() /** * Closes the connection to the database, if it is open. * * @return True on success, false on failure. */ function close() { if ($this->opened) { $this->opened = false; return $this->db->disconnect(); } return true; } // }}} // {{{ log() /** * Inserts $message to the currently open database. Calls open(), * if necessary. Also passes the message along to any Log_observer * instances that are observing this Log. * * @param $message The textual message to be logged. * @param $priority (optional) The priority of the message. Valid * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, * LOG_ERR, * LOG_WARNING, LOG_NOTICE, LOG_INFO, * and LOG_DEBUG. The default is LOG_INFO. */ function log($message, $priority = LOG_INFO) { if (!$this->opened) $this->open(); $timestamp = time(); $q = "insert into $this->table values($timestamp, '$this->ident', $priority, '$message')"; $this->db->query($q); $this->notifyAll(array('priority' => $priority, 'message' => $message)); } // }}} } ?> Index: php4/pear/Log/syslog.php +++ php4/pear/Log/syslog.php <?php // $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $ /** * The Log_syslog class is a concrete implementation of the Log:: * abstract class which sends messages to syslog on UNIX-like machines * (PHP emulates this with the Event Log on Windows machines). * * @author Chuck Hagenbuch <chuck@horde.org> * @version $Revision: 1.1 $ * @since Horde 1.3 */ class Log_syslog extends Log { // {{{ properties /** Integer holding the log facility to use. */ var $name = LOG_SYSLOG; // }}} // {{{ constructor /** * Constructs a new syslog object. * * @param $log_name (optional) The syslog facility. * @param $ident (optional) The identity string. * @param $conf (optional) The configuration array. */ function Log_syslog ($log_name = LOG_SYSLOG, $ident = '', $conf = false) { $this->name = $log_name; $this->ident = $ident; } // }}} // {{{ open() /** * Opens a connection to the system logger, if it has not already * been opened. This is implicitly called by log(), if necessary. */ function open () { if (!$this->opened) { openlog($this->ident, LOG_PID, $this->name); $this->opened = true; } } // }}} // {{{ close() /** * Closes the connection to the system logger, if it is open. */ function close () { if ($this->opened) { closelog(); $this->opened = false; } } // }}} // {{{ log() /** * Sends $message to the currently open syslog * connection. Calls * open() if necessary. Also passes the message along to any Log_observer * instances that are observing this Log. * * @param $message The textual message to be logged. * @param $priority (optional) The priority of the message. Valid * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, * and LOG_DEBUG. The default is LOG_INFO. */ function log ($message, $priority = LOG_INFO) { if (!$this->opened) $this->open(); syslog($priority, $message); $this->notifyAll(array('priority' => $priority, 'message' => $message)); } // }}} } ?>
« previous php.cvs (#2987) next »