cvs: pear /Log Log.php README composite.php file.php mcal.php observer.php package.xml sql.php syslog.php
| From: | Jon Parise | Date: | Wed, 16 May 2001 01:17:34 +0000 |
| Subject: | cvs: pear /Log Log.php README composite.php file.php mcal.php observer.php package.xml sql.php syslog.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-146@lists.php.net to get a copy of this message | ||
jon Tue May 15 18:17:34 2001 EDT
Added files:
/pear/Log Log.php README composite.php file.php mcal.php
observer.php package.xml sql.php syslog.php
Log:
Adding the initial release of the Log package.
Index: pear/Log/Log.php +++ pear/Log/Log.php <?php // $Id: Log.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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(); /** * 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 = 'Log/' . $log_type . '.php'; if (@include_once $classfile) { $class = 'Log_' . $log_type; return new $class($log_name, $ident, $conf); } else { return false; } } /** * 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]; } /** * 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]; } /** * 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; } /** * 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]); } /** * 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); } } /** * @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: pear/Log/composite.php +++ pear/Log/composite.php <?php // $Id: composite.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** Array holding all Log instances which should be sent events sent to the composite. */ var $children = array(); /** * 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 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(); } } } /** * 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(); } } } /** * 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)); } /** * @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; } /** * 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; } /** * 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: pear/Log/file.php +++ pear/Log/file.php <?php // $Id: file.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** String holding the filename of the logfile. */ var $filename = ''; /** Integer holding the file handle. */ var $fp = ''; /** * 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; } /** * 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; } } /** * Closes the logfile, if it is open. */ function close() { if ($this->opened) { fclose($this->fp); $this->opened = false; } } /** * 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: pear/Log/mcal.php +++ pear/Log/mcal.php <?php // $Id: mcal.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * 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']; } /** * 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; } } /** * Closes the calendar stream, if it is open. */ function close() { if ($this->opened) { mcal_close($this->stream); $this->opened = false; } } /** * 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: pear/Log/observer.php +++ pear/Log/observer.php <?php // $Id: observer.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * Basic Log_observer instance that just prints out messages * received. */ function Log_observer($priority = LOG_INFO) { $this->priority = $priority; } /** * 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; } } /** * 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: pear/Log/package.xml +++ pear/Log/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <Package> <Name>Log</Name> <Summary>Logging utilities</Summary> <Maintainer> <Initials>JCP</Initials> <Name>Jon Parise</Name> <Email>jon@php.net</Email> </Maintainer> <Release> <Version>1.0</Version> <Date>2001-05-15</Date> <Notes> This is the initial independent release of the Log package. </Notes> </Release> <FileList> <Dir>/</Dir> <File>Log.php</File> <Dir>Log</Dir> <File>composite.php</File> <File>file.php</File> <File>mcal.php</File> <File>observer.php</File> <File>sql.php</File> <File>syslog.php</File> </FileList> </Package> Index: pear/Log/sql.php +++ pear/Log/sql.php <?php // $Id: sql.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * 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']; } /** * 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; } /** * 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; } /** * 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: pear/Log/syslog.php +++ pear/Log/syslog.php <?php // $Id: syslog.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** Integer holding the log facility to use. */ var $name = LOG_SYSLOG; /** * 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; } /** * 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; } } /** * Closes the connection to the system logger, if it is open. */ function close() { if ($this->opened) { closelog(); $this->opened = false; } } /** * 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)); } } ?>
Index: pear/Log/Log.php +++ pear/Log/Log.php <?php // $Id: Log.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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(); /** * 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 = 'Log/' . $log_type . '.php'; if (@include_once $classfile) { $class = 'Log_' . $log_type; return new $class($log_name, $ident, $conf); } else { return false; } } /** * 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]; } /** * 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]; } /** * 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; } /** * 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]); } /** * 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); } } /** * @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: pear/Log/composite.php +++ pear/Log/composite.php <?php // $Id: composite.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** Array holding all Log instances which should be sent events sent to the composite. */ var $children = array(); /** * 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 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(); } } } /** * 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(); } } } /** * 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)); } /** * @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; } /** * 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; } /** * 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: pear/Log/file.php +++ pear/Log/file.php <?php // $Id: file.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** String holding the filename of the logfile. */ var $filename = ''; /** Integer holding the file handle. */ var $fp = ''; /** * 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; } /** * 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; } } /** * Closes the logfile, if it is open. */ function close() { if ($this->opened) { fclose($this->fp); $this->opened = false; } } /** * 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: pear/Log/mcal.php +++ pear/Log/mcal.php <?php // $Id: mcal.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * 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']; } /** * 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; } } /** * Closes the calendar stream, if it is open. */ function close() { if ($this->opened) { mcal_close($this->stream); $this->opened = false; } } /** * 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: pear/Log/observer.php +++ pear/Log/observer.php <?php // $Id: observer.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * Basic Log_observer instance that just prints out messages * received. */ function Log_observer($priority = LOG_INFO) { $this->priority = $priority; } /** * 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; } } /** * 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: pear/Log/package.xml +++ pear/Log/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <Package> <Name>Log</Name> <Summary>Logging utilities</Summary> <Maintainer> <Initials>JCP</Initials> <Name>Jon Parise</Name> <Email>jon@php.net</Email> </Maintainer> <Release> <Version>1.0</Version> <Date>2001-05-15</Date> <Notes> This is the initial independent release of the Log package. </Notes> </Release> <FileList> <Dir>/</Dir> <File>Log.php</File> <Dir>Log</Dir> <File>composite.php</File> <File>file.php</File> <File>mcal.php</File> <File>observer.php</File> <File>sql.php</File> <File>syslog.php</File> </FileList> </Package> Index: pear/Log/sql.php +++ pear/Log/sql.php <?php // $Id: sql.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** 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; /** * 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']; } /** * 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; } /** * 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; } /** * 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: pear/Log/syslog.php +++ pear/Log/syslog.php <?php // $Id: syslog.php,v 1.1 2001/05/16 01:17:34 jon Exp $ // $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 { /** Integer holding the log facility to use. */ var $name = LOG_SYSLOG; /** * 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; } /** * 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; } } /** * Closes the connection to the system logger, if it is open. */ function close() { if ($this->opened) { closelog(); $this->opened = false; } } /** * 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)); } } ?>