| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
stubClassLoader::load('net::stubbles::util::log::stubLogAppender', |
|---|
| 10 |
'net::stubbles::util::log::stubLogData' |
|---|
| 11 |
); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
class stubLogger extends stubBaseObject |
|---|
| 27 |
{ |
|---|
| 28 |
|
|---|
| 29 |
* default logger id |
|---|
| 30 |
*/ |
|---|
| 31 |
const DEFAULT_ID = 'default'; |
|---|
| 32 |
|
|---|
| 33 |
* log level: no logging |
|---|
| 34 |
*/ |
|---|
| 35 |
const LEVEL_NONE = 0; |
|---|
| 36 |
|
|---|
| 37 |
* log level: debug only |
|---|
| 38 |
*/ |
|---|
| 39 |
const LEVEL_DEBUG = 1; |
|---|
| 40 |
|
|---|
| 41 |
* log level: info data only |
|---|
| 42 |
*/ |
|---|
| 43 |
const LEVEL_INFO = 2; |
|---|
| 44 |
|
|---|
| 45 |
* log level: warnings only |
|---|
| 46 |
*/ |
|---|
| 47 |
const LEVEL_WARN = 4; |
|---|
| 48 |
|
|---|
| 49 |
* log level: errors only |
|---|
| 50 |
*/ |
|---|
| 51 |
const LEVEL_ERROR = 8; |
|---|
| 52 |
|
|---|
| 53 |
* log level: all |
|---|
| 54 |
*/ |
|---|
| 55 |
const LEVEL_ALL = 15; |
|---|
| 56 |
|
|---|
| 57 |
* list of available logger instances |
|---|
| 58 |
* |
|---|
| 59 |
* @var array<stubLogger> |
|---|
| 60 |
*/ |
|---|
| 61 |
protected static $instances = array(); |
|---|
| 62 |
|
|---|
| 63 |
* id of the logger |
|---|
| 64 |
* |
|---|
| 65 |
* @var string |
|---|
| 66 |
*/ |
|---|
| 67 |
protected $id; |
|---|
| 68 |
|
|---|
| 69 |
* log level where this logger is for |
|---|
| 70 |
* |
|---|
| 71 |
* @var int |
|---|
| 72 |
*/ |
|---|
| 73 |
protected $level; |
|---|
| 74 |
|
|---|
| 75 |
* list of log appenders to log data to |
|---|
| 76 |
* |
|---|
| 77 |
* @var array<stubLogAppender> |
|---|
| 78 |
*/ |
|---|
| 79 |
protected $logAppender = array(); |
|---|
| 80 |
|
|---|
| 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) |
|---|
| 90 |
{ |
|---|
| 91 |
$this->id = $id; |
|---|
| 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() |
|---|
| 101 |
{ |
|---|
| 102 |
foreach ($this->logAppender as $logAppender) { |
|---|
| 103 |
$logAppender->finalize(); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 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) |
|---|
| 120 |
{ |
|---|
| 121 |
if (isset(self::$instances[$id]) == false) { |
|---|
| 122 |
self::$instances[$id] = new self($id, $level); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
return self::$instances[$id]; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
* returns a list of existing instances |
|---|
| 130 |
* |
|---|
| 131 |
* @return array<string> |
|---|
| 132 |
*/ |
|---|
| 133 |
public static function getInstanceList() |
|---|
| 134 |
{ |
|---|
| 135 |
return array_keys(self::$instances); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
* destroys the logger with the given id |
|---|
| 140 |
* |
|---|
| 141 |
* @param string $id id of the logger |
|---|
| 142 |
*/ |
|---|
| 143 |
public static function destroyInstance($id) |
|---|
| 144 |
{ |
|---|
| 145 |
if (isset(self::$instances[$id]) == true) { |
|---|
| 146 |
self::$instances[$id] = null; |
|---|
| 147 |
unset(self::$instances[$id]); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
* clone is not supported |
|---|
| 153 |
* |
|---|
| 154 |
* @throws stubLoggerException |
|---|
| 155 |
*/ |
|---|
| 156 |
protected final function __clone() |
|---|
| 157 |
{ |
|---|
| 158 |
throw new stubLoggerException('Cloning a logger is not allowed.'); |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
* returns the id of the logger |
|---|
| 163 |
* |
|---|
| 164 |
* @return string |
|---|
| 165 |
*/ |
|---|
| 166 |
public function getId() |
|---|
| 167 |
{ |
|---|
| 168 |
return $this->id; |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
* returns the level where the logger is applicable for |
|---|
| 173 |
* |
|---|
| 174 |
* @return int |
|---|
| 175 |
*/ |
|---|
| 176 |
public function getLevel() |
|---|
| 177 |
{ |
|---|
| 178 |
return $this->level; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* checks whether the logger is applicable for the given level |
|---|
| 183 |
* |
|---|
| 184 |
* @param int $level |
|---|
| 185 |
* @return bool |
|---|
| 186 |
*/ |
|---|
| 187 |
public function isApplicable($level) |
|---|
| 188 |
{ |
|---|
| 189 |
if (self::LEVEL_ALL == $level && self::LEVEL_ALL > $this->level) { |
|---|
| 190 |
return false; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
return (($this->level & $level) != 0); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
* adds a log appender to the logger |
|---|
| 198 |
* |
|---|
| 199 |
* A log appender is responsible for writing the log data. |
|---|
| 200 |
* |
|---|
| 201 |
* @param stubLogAppender $logAppender |
|---|
| 202 |
*/ |
|---|
| 203 |
public function addLogAppender(stubLogAppender $logAppender) |
|---|
| 204 |
{ |
|---|
| 205 |
$this->logAppender[] = $logAppender; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
* returns a list of log appenders appended to the logger |
|---|
| 210 |
* |
|---|
| 211 |
* @return array<stubLogAppender> |
|---|
| 212 |
*/ |
|---|
| 213 |
public function getLogAppenders() |
|---|
| 214 |
{ |
|---|
| 215 |
return $this->logAppender; |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
* log data to all loggers created |
|---|
| 220 |
* |
|---|
| 221 |
* @param stubLogData $logData contains the data to log |
|---|
| 222 |
*/ |
|---|
| 223 |
public static function logToAll(stubLogData $logData) |
|---|
| 224 |
{ |
|---|
| 225 |
foreach (self::$instances as $logger) { |
|---|
| 226 |
if (null === $logger || $logger->isApplicable($logData->getLevel()) == false) { |
|---|
| 227 |
continue; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
$logger->log($logData); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
* sends log data to all registered log appenders |
|---|
| 236 |
* |
|---|
| 237 |
* @param stubLogData $logData contains the data to log |
|---|
| 238 |
*/ |
|---|
| 239 |
public function log(stubLogData $logData) |
|---|
| 240 |
{ |
|---|
| 241 |
foreach ($this->logAppender as $logAppender) { |
|---|
| 242 |
$logAppender->append($logData); |
|---|
| 243 |
} |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
?> |
|---|