[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/logger/logger/logger.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/logger/config/loggerconfigloader.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/logger/layout/patternlayout.class.php" ); 6 7 /** 8 * \defgroup logger 9 * 10 * The logger package takes care of providing logging facilities for the pLog API. It is a very simple 11 * API but on the other hand, it follows some of the principles establishes by log4j and log4php, with 12 * a very similar system of layouts, formatters and appenders. 13 * 14 * See the documentation of the LoggerManager class for more information on how to use the class and how 15 * to format the messages 16 */ 17 18 /** 19 * \ingroup logger 20 * 21 * The logger manages a bunch of loggers configured in the config/logging.properties.php file. 22 * By default if no loggers are specified it will create one logger called "default". 23 * In order to define new loggers, the configuration file has to look like this: 24 * 25 * <pre> 26 * $config["logger_name"] = Array( 27 * "appender" => "name_of_the_appender_class", 28 * "layout" => "a pattern definition for the log messages", 29 * "file" => "file_for_fileappender", 30 * "prio" => "debug" ); 31 * </pre> 32 * 33 * Where "logger_name" is the identifer that we will use later on in the call to 34 * LoggerManager::getLogger() later on to retrieve a handle to this particular logger. 35 * 36 * The available appenders are: "file", "stdout", "null" 37 * The FileAppender class writes data to a log file, which must be specified via a 38 * property called "file" 39 * The StdoutAppender class writes data to the console 40 * The NullAppender does not write data anywhere, it's a "dumb" logger that does nothing. 41 * If you do not want a logfile to be written, but want to safe your 42 * configuration, just change the appender from 'file' to 'null' :-) 43 * 44 * Layout formats: 45 * PatternLayout: allows to specify a pattern for our messages. Patterns are specified as follows: 46 * <ul> 47 * <li><b>%c</b> - the class where message was logged</li> 48 * <li><b>%d</b> - current date</li> 49 * <li><b>%f</b> - the file where the message was logged</li> 50 * <li><b>%F</b> - the function where the message was 51 * logged</li> 52 * <li><b>%l</b> - the line where the message was logged</li> 53 * <li><b>%m</b> - the log message</li> 54 * <li><b>%n</b> - a newline</li> 55 * <li><b>%N</b> - the level name</li> 56 * <li><b>%p</b> - the level of priority</li> 57 * <li><b>%r</b> - a carriage return</li> 58 * <li><b>%t</b> - a horizontal tab</li> 59 * <li><b>%T</b> - a unix timestamp (seconds since January 1st, 1970)</li> 60 * </ul> 61 * 62 * The available priorities are: error > warn > info 63 * 64 * NOTE: There can only be one appender and one layout class per logger. 65 * 66 * In order to retrieve a handle to one of our loggers later on in our code, we should use 67 * LoggerManager::getLogger() 68 * 69 * <pre> 70 * $log =& LoggerManager::getLogger( "logger_name" ); 71 * $log->debug( $message ); 72 * ... 73 * $log->info( $message ); 74 * </pre> 75 */ 76 class LoggerManager 77 { 78 /** 79 * An associative array of loggers. 80 */ 81 var $loggers; 82 83 /** 84 * Create a new LogManager instance. 85 * This should never be called manually. 86 */ 87 function LoggerManager() 88 { 89 $this->loggers = array(); 90 91 $this->_loadLoggerConfig(); 92 } 93 94 /** 95 * @private 96 * Loads the configuration from the config file 97 */ 98 function _loadLoggerConfig() 99 { 100 // load the config file and see how many loggers are configued 101 $config = new LoggerConfigLoader(); 102 $loggers = $config->getConfiguredLoggers(); 103 104 if( count($loggers) == 0 ) { 105 // add a default logger 106 $loggers["default"] = Array( "layout" => "%d %N - [%f:%l (%c:%F)] %m%n", 107 "appender" => "stdout", 108 "prio" => "debug" ); 109 } 110 111 // loop through the loggers and configure them 112 foreach( $config->getConfiguredLoggers() as $logger ) { 113 // get the logger config properties 114 $properties = $config->getLoggerProperties( $logger ); 115 116 // create the logger 117 $layout = new PatternLayout( $config->getLoggerLayout( $logger )); 118 $appenderObject = $this->createAppenderInstance( $config->getLoggerAppender( $logger ), 119 $layout, $properties ); 120 121 // create the logger, set the appender and it to the list 122 $loggerObject = new Logger( $properties ); 123 $loggerObject->addAppender( $appenderObject ); 124 $this->addLogger( $logger, $loggerObject ); 125 } 126 } 127 128 /** 129 * dynamically loads a layout formatter 130 * 131 * @param appenderName 132 * @return a qLayout class 133 */ 134 function createAppenderInstance( $appender, $layout, $properties ) 135 { 136 $appenderClassName = $appender."appender"; 137 $appenderClassFile = PLOG_CLASS_PATH."class/logger/appender/".$appenderClassName.".class.php"; 138 139 // load the class but first check if it exists... 140 if( !File::isReadable( $appenderClassFile )) { 141 throw( new Exception( "Cannot find an appender suitable for appender type '$appender'" )); 142 die(); 143 } 144 145 // if so, load the class and create an object 146 lt_include( $appenderClassFile ); 147 $appender = new $appenderClassName( $layout, $properties ); 148 149 return( $appender ); 150 } 151 152 /** 153 * Add a logger. 154 * If a logger with the given name already exists, an error will be 155 * reported. 156 * 157 * @param string A logger name. 158 * @param Logger A Logger instance. 159 */ 160 function addLogger ($name, $logger) 161 { 162 if (isset($this->loggers[$name])) { 163 throw(new Exception("LogManager::addLogger: LogManager already contains logger " . $name)); 164 die(); 165 } 166 167 $this->loggers[$name] = $logger; 168 169 return; 170 } 171 172 /** 173 * Retrieve the single instance of LogManager. 174 * 175 * @return qLogManager A qLogManager instance. 176 */ 177 function &getInstance() 178 { 179 static $instance = NULL; 180 181 if ($instance === NULL) { 182 $instance = new LoggerManager(); 183 } 184 185 return $instance; 186 } 187 188 /** 189 * Retrieve a logger. 190 * If a name is not specified, the default logger is returned. 191 * 192 * @param string A logger name. 193 * @return Logger A Logger instance, if the given Logger exists, otherwise NULL. 194 */ 195 function &getLogger ($name = "default") 196 { 197 $instance =& LoggerManager::getInstance(); 198 199 if (isset($instance->loggers[$name])) { 200 return $instance->loggers[$name]; 201 } 202 203 return NULL; 204 } 205 } 206 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |