Changeset 146
- Timestamp:
- 01/24/07 17:03:47 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/util/log/log.php (added)
- trunk/src/main/php/net/stubbles/util/log/stubBaseLogData.php (added)
- trunk/src/main/php/net/stubbles/util/log/stubFileLogAppender.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/util/log/stubLogAppender.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/util/log/stubLogData.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/util/log/stubLogDataFactory.php (added)
- trunk/src/main/php/net/stubbles/util/log/stubLogger.php (modified) (11 diffs)
- trunk/src/test/php/net/stubbles/util (added)
- trunk/src/test/php/net/stubbles/util/UtilTestSuite.php (added)
- trunk/src/test/php/net/stubbles/util/log (added)
- trunk/src/test/php/net/stubbles/util/log/stubBaseLogDataTestCase.php (added)
- trunk/src/test/php/net/stubbles/util/log/stubFileLogAppenderTestCase.php (added)
- trunk/src/test/php/net/stubbles/util/log/stubLoggerTestCase.php (added)
- trunk/src/test/run.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/util/log/stubFileLogAppender.php
r143 r146 1 1 <?php 2 /** 3 * A log appenders that writes log data to files. 4 * 5 * @author Frank Kleine <mikey@stubbles.net> 6 * @package stubbles 7 * @subpackage util_log 8 */ 9 stubClassLoader::load('net.stubbles.util.log.stubLogAppender', 10 'net.stubbles.util.log.stubLogData' 11 ); 12 /** 13 * A log appenders that writes log data to files. 14 * 15 * This log appender writes the log data into a logfile using the error_log() 16 * function of PHP. The logfile name will be [target]-[Y-m-d].log where target 17 * is the return value of stubLogData::getTarget(). 18 * 19 * @package stubbles 20 * @subpackage util_log 21 * @uses http://php.net/error_log 22 */ 2 23 class stubFileLogAppender extends stubBaseObject implements stubLogAppender 3 24 { 25 /** 26 * the directory to write the logfiles into 27 * 28 * @var string 29 */ 30 protected $logDir = ''; 31 32 /** 33 * constructor 34 * 35 * The log director may have placeholders: {Y} will be replaced with the 36 * current year, {M} will be replaced with the current month, e.g. 37 * /path/to/logs/{Y}/{M} would become /path/to/logs/2007/01. 38 * 39 * @param string $logDir the directory to write the logfiles into 40 */ 41 public function __construct($logDir) 42 { 43 $this->logDir = $logDir; 44 } 45 46 /** 47 * builds the log directory 48 * 49 * @return string 50 */ 51 protected function buildLogDir() 52 { 53 return str_replace('{Y}', date('Y'), str_replace('{M}', date('m'), $this->logDir)); 54 } 55 56 /** 57 * append the log data to the log target 58 * 59 * The basename of the logfile will be [target]-[Y-m-d].log where target 60 * is the return value of stubLogData::getTarget(). 61 * 62 * @param stubLogData $logData 63 */ 4 64 public function append(stubLogData $logData) 5 65 { 6 $logFile = $logData->getTarget() . '-' . date('Y-m-d') . '.log'; 7 error_log($logData->get(), 3, $logFile); 66 $logDir = $this->buildLogDir(); 67 if (file_exists($logDir) == false) { 68 mkdir($logDir, 0700, true); 69 } 70 71 error_log($logData->get() . "\n", 3, $logDir . '/' . $logData->getTarget() . '-' . date('Y-m-d') . '.log'); 8 72 } 9 73 74 /** 75 * finalize the log target 76 */ 10 77 public function finalize() 11 78 { trunk/src/main/php/net/stubbles/util/log/stubLogAppender.php
r143 r146 1 1 <?php 2 /** 3 * Interface for log appenders. 4 * 5 * @author Frank Kleine <mikey@stubbles.net> 6 * @package stubbles 7 * @subpackage util_log 8 */ 9 /** 10 * Interface for log appenders. 11 * 12 * A log appender takes log data and writes it to the target. The target can be 13 * a file, a database or anything else. 14 * 15 * @package stubbles 16 * @subpackage util_log 17 */ 2 18 interface stubLogAppender 3 19 { 20 /** 21 * append the log data to the log target 22 * 23 * @param stubLogData $logData 24 */ 4 25 public function append(stubLogData $logData); 5 26 27 /** 28 * finalize the log target 29 * 30 * This will be called in case a logger is destroyed and can be used 31 * to close file or database handlers or to write the log data if 32 * append() just collects the data. 33 */ 6 34 public function finalize(); 7 35 } trunk/src/main/php/net/stubbles/util/log/stubLogData.php
r143 r146 1 1 <?php 2 /** 3 * Interface for log data. 4 * 5 * @author Frank Kleine <mikey@stubbles.net> 6 * @package stubbles 7 * @subpackage util_log 8 */ 9 /** 10 * Interface for log data. 11 * 12 * The log data object is used to collect the data to log. 13 * 14 * @package stubbles 15 * @subpackage util_log 16 */ 2 17 interface stubLogData 3 18 { 19 /** 20 * default seperator to be used to seperate the log fields 21 */ 22 const SEPERATOR = '|'; 23 24 /** 25 * adds data to the log object 26 * 27 * Each call to this method will add a new field. 28 * 29 * @param string $data 30 */ 4 31 public function addData($data); 5 32 33 /** 34 * returns the whole log data as one line 35 * 36 * @return string 37 */ 6 38 public function get(); 7 39 40 /** 41 * returns the level of the log data 42 * 43 * @return int 44 * @see stubLogger::LEVEL_* 45 */ 8 46 public function getLevel(); 9 47 48 /** 49 * returns the target where the log data should go to 50 * 51 * How the target is interpreted depends on the log appender which 52 * takes the log data. A file log appender might use this as the basename 53 * of a file, while a database log appender might use this as the name 54 * of the table to write the log data into. Therefore it is advisable to 55 * only use ascii characters, numbers and underscores to be sure that the 56 * log appender will not mess up the log data. 57 * 58 * @return string 59 */ 10 60 public function getTarget(); 11 61 } trunk/src/main/php/net/stubbles/util/log/stubLogger.php
r143 r146 1 1 <?php 2 class stubLogger 2 /** 3 * Class for logging. 4 * 5 * @author Frank Kleine <mikey@stubbles.net> 6 * @package stubbles 7 * @subpackage util_log 8 */ 9 stubClassLoader::load('net.stubbles.util.log.stubLogAppender', 10 'net.stubbles.util.log.stubLogData' 11 ); 12 /** 13 * Class for logging. 14 * 15 * The logger is the interface to log data into differant targets. The logger 16 * itself does not know where to write the log data - it just uses log appenders 17 * which in turn do the real work. A logger is a collection of such appenders. 18 * Even if it looks like a singleton it is possible to have differant instances 19 * of a logger. Each logger uses a differant id and can be applied on differant 20 * logging levels. For instance, you may have one logger that is applicable for 21 * debug logging and another one that is used for the other error levels. 22 * 23 * @package stubbles 24 * @subpackage util_log 25 */ 26 class stubLogger extends stubBaseObject 3 27 { 4 28 /** … … 27 51 const LEVEL_ERROR = 8; 28 52 /** 53 * log level: all 54 */ 55 const LEVEL_ALL = 15; 56 /** 29 57 * list of available logger instances 30 58 * … … 51 79 protected $logAppender = array(); 52 80 53 public function __static() 54 { 55 # create all loggers as defined in configuration 56 } 57 58 protected final function __construct($id, $level) 81 /** 82 * constructor 83 * 84 * If no level is specified the logger will be applicable for all log levels. 85 * 86 * @param string $id id of the logger 87 * @param int $level optional level where the logger is applicable for 88 */ 89 protected final function __construct($id, $level = self::LEVEL_ALL) 59 90 { 60 91 $this->id = $id; 61 if (null !== $level) { 62 $this->level = $level; 63 } else { 64 $this->level = self::LEVEL_DEBUG + self::LEVEL_INFO + self::LEVEL_WARN + self::LEVEL_ERROR; 65 } 66 } 67 68 protected final function __destruct() 92 $this->level = $level; 93 } 94 95 /** 96 * destructor 97 * 98 * Calls all log appenders that they should finalize their work. 99 */ 100 public final function __destruct() 69 101 { 70 102 foreach ($this->logAppender as $logAppender) { … … 73 105 } 74 106 75 public static function getInstance($id = self::DEFAULT_ID, $level = null) 107 /** 108 * returns a logger instance 109 * 110 * If the logger with the given id already exists this will be returned, 111 * else a new logger will be created using the given level and returned. 112 * If no level is specified and a new logger is created it will be 113 * applicable for all levels. 114 * 115 * @param string $id id of the logger 116 * @param int $level optional level where the logger is applicable for 117 * @return stubLogger 118 */ 119 public static function getInstance($id = self::DEFAULT_ID, $level = self::LEVEL_ALL) 76 120 { 77 121 if (isset(self::$instances[$id]) == false) { … … 82 126 } 83 127 128 /** 129 * destroys the logger with the given id 130 * 131 * @param string $id id of the logger 132 */ 84 133 public static function destroyInstance($id) 85 134 { … … 89 138 } 90 139 140 /** 141 * clone is not supported 142 */ 91 143 protected final function __clone() 92 144 { … … 94 146 } 95 147 148 /** 149 * returns the id of the logger 150 * 151 * @return string 152 */ 96 153 public function getId() 97 154 { … … 99 156 } 100 157 158 /** 159 * returns the level where the logger is applicable for 160 * 161 * @return int 162 */ 101 163 public function getLevel() 102 164 { … … 104 166 } 105 167 168 /** 169 * checks whether the logger is applicable for the given level 170 * 171 * @param int $level 172 * @return bool 173 */ 106 174 public function isApplicable($level) 107 175 { 176 if (self::LEVEL_ALL == $level && self::LEVEL_ALL > $this->level) { 177 return false; 178 } 179 108 180 return (($this->level & $level) != 0); 109 181 } 110 182 183 /** 184 * adds a log appender to the logger 185 * 186 * A log appender is responsible for writing the log data. 187 * 188 * @param stubLogAppender $logAppender 189 */ 111 190 public function addLogAppender(stubLogAppender $logAppender) 112 191 { … … 114 193 } 115 194 195 /** 196 * log data to all loggers created 197 * 198 * @param stubLogData $logData contains the data to log 199 */ 116 200 public static function logToAll(stubLogData $logData) 117 201 { 118 202 foreach (self::$instances as $logger) { 119 if ( $logger->isApplicable($logData->getLevel()) == false) {203 if (null === $logger || $logger->isApplicable($logData->getLevel()) == false) { 120 204 continue; 121 205 } … … 125 209 } 126 210 211 /** 212 * sends log data to all registered log appenders 213 * 214 * @param stubLogData $logData contains the data to log 215 */ 127 216 public function log(stubLogData $logData) 128 217 { trunk/src/test/run.php
r121 r146 33 33 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/helper/HelperTestSuite.php'); 34 34 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/reflection/ReflectionTestSuite.php'); 35 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/util/UtilTestSuite.php'); 35 36 #$testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php'); 36 37 $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/xml/XMLTestSuite.php');
