raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * $Id: Logger.class.php 10 2007-05-27 10:55:12Z spocke $
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package MCFileManager.filesystems
mp-wp_genesis 6 * @author Moxiecode
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 // File type contstants
mp-wp_genesis 10 define('MC_LOGGER_DEBUG', 0);
mp-wp_genesis 11 define('MC_LOGGER_INFO', 10);
mp-wp_genesis 12 define('MC_LOGGER_WARN', 20);
mp-wp_genesis 13 define('MC_LOGGER_ERROR', 30);
mp-wp_genesis 14 define('MC_LOGGER_FATAL', 40);
mp-wp_genesis 15
mp-wp_genesis 16 /**
mp-wp_genesis 17 * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
mp-wp_genesis 18 * designed to be compact but still powerful and flexible.
mp-wp_genesis 19 */
mp-wp_genesis 20 class Moxiecode_Logger {
mp-wp_genesis 21 // Private fields
mp-wp_genesis 22 var $_path;
mp-wp_genesis 23 var $_filename;
mp-wp_genesis 24 var $_maxSize;
mp-wp_genesis 25 var $_maxFiles;
mp-wp_genesis 26 var $_maxSizeBytes;
mp-wp_genesis 27 var $_level;
mp-wp_genesis 28 var $_format;
mp-wp_genesis 29
mp-wp_genesis 30 /**
mp-wp_genesis 31 * Constructs a new logger instance.
mp-wp_genesis 32 */
mp-wp_genesis 33 function Moxiecode_Logger() {
mp-wp_genesis 34 $this->_path = "";
mp-wp_genesis 35 $this->_filename = "{level}.log";
mp-wp_genesis 36 $this->setMaxSize("100k");
mp-wp_genesis 37 $this->_maxFiles = 10;
mp-wp_genesis 38 $this->_level = MC_LOGGER_DEBUG;
mp-wp_genesis 39 $this->_format = "[{time}] [{level}] {message}";
mp-wp_genesis 40 }
mp-wp_genesis 41
mp-wp_genesis 42 /**
mp-wp_genesis 43 * Sets the current log level, use the MC_LOGGER constants.
mp-wp_genesis 44 *
mp-wp_genesis 45 * @param int $level Log level instance for example MC_LOGGER_DEBUG.
mp-wp_genesis 46 */
mp-wp_genesis 47 function setLevel($level) {
mp-wp_genesis 48 if (is_string($level)) {
mp-wp_genesis 49 switch (strtolower($level)) {
mp-wp_genesis 50 case "debug":
mp-wp_genesis 51 $level = MC_LOGGER_DEBUG;
mp-wp_genesis 52 break;
mp-wp_genesis 53
mp-wp_genesis 54 case "info":
mp-wp_genesis 55 $level = MC_LOGGER_INFO;
mp-wp_genesis 56 break;
mp-wp_genesis 57
mp-wp_genesis 58 case "warn":
mp-wp_genesis 59 case "warning":
mp-wp_genesis 60 $level = MC_LOGGER_WARN;
mp-wp_genesis 61 break;
mp-wp_genesis 62
mp-wp_genesis 63 case "error":
mp-wp_genesis 64 $level = MC_LOGGER_ERROR;
mp-wp_genesis 65 break;
mp-wp_genesis 66
mp-wp_genesis 67 case "fatal":
mp-wp_genesis 68 $level = MC_LOGGER_FATAL;
mp-wp_genesis 69 break;
mp-wp_genesis 70
mp-wp_genesis 71 default:
mp-wp_genesis 72 $level = MC_LOGGER_FATAL;
mp-wp_genesis 73 }
mp-wp_genesis 74 }
mp-wp_genesis 75
mp-wp_genesis 76 $this->_level = $level;
mp-wp_genesis 77 }
mp-wp_genesis 78
mp-wp_genesis 79 /**
mp-wp_genesis 80 * Returns the current log level for example MC_LOGGER_DEBUG.
mp-wp_genesis 81 *
mp-wp_genesis 82 * @return int Current log level for example MC_LOGGER_DEBUG.
mp-wp_genesis 83 */
mp-wp_genesis 84 function getLevel() {
mp-wp_genesis 85 return $this->_level;
mp-wp_genesis 86 }
mp-wp_genesis 87
mp-wp_genesis 88 function setPath($path) {
mp-wp_genesis 89 $this->_path = $path;
mp-wp_genesis 90 }
mp-wp_genesis 91
mp-wp_genesis 92 function getPath() {
mp-wp_genesis 93 return $this->_path;
mp-wp_genesis 94 }
mp-wp_genesis 95
mp-wp_genesis 96 function setFileName($file_name) {
mp-wp_genesis 97 $this->_filename = $file_name;
mp-wp_genesis 98 }
mp-wp_genesis 99
mp-wp_genesis 100 function getFileName() {
mp-wp_genesis 101 return $this->_filename;
mp-wp_genesis 102 }
mp-wp_genesis 103
mp-wp_genesis 104 function setFormat($format) {
mp-wp_genesis 105 $this->_format = $format;
mp-wp_genesis 106 }
mp-wp_genesis 107
mp-wp_genesis 108 function getFormat() {
mp-wp_genesis 109 return $this->_format;
mp-wp_genesis 110 }
mp-wp_genesis 111
mp-wp_genesis 112 function setMaxSize($size) {
mp-wp_genesis 113 // Fix log max size
mp-wp_genesis 114 $logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
mp-wp_genesis 115
mp-wp_genesis 116 // Is KB
mp-wp_genesis 117 if (strpos((strtolower($size)), "k") > 0)
mp-wp_genesis 118 $logMaxSizeBytes *= 1024;
mp-wp_genesis 119
mp-wp_genesis 120 // Is MB
mp-wp_genesis 121 if (strpos((strtolower($size)), "m") > 0)
mp-wp_genesis 122 $logMaxSizeBytes *= (1024 * 1024);
mp-wp_genesis 123
mp-wp_genesis 124 $this->_maxSizeBytes = $logMaxSizeBytes;
mp-wp_genesis 125 $this->_maxSize = $size;
mp-wp_genesis 126 }
mp-wp_genesis 127
mp-wp_genesis 128 function getMaxSize() {
mp-wp_genesis 129 return $this->_maxSize;
mp-wp_genesis 130 }
mp-wp_genesis 131
mp-wp_genesis 132 function setMaxFiles($max_files) {
mp-wp_genesis 133 $this->_maxFiles = $max_files;
mp-wp_genesis 134 }
mp-wp_genesis 135
mp-wp_genesis 136 function getMaxFiles() {
mp-wp_genesis 137 return $this->_maxFiles;
mp-wp_genesis 138 }
mp-wp_genesis 139
mp-wp_genesis 140 function debug($msg) {
mp-wp_genesis 141 $args = func_get_args();
mp-wp_genesis 142 $this->_logMsg(MC_LOGGER_DEBUG, implode(', ', $args));
mp-wp_genesis 143 }
mp-wp_genesis 144
mp-wp_genesis 145 function info($msg) {
mp-wp_genesis 146 $args = func_get_args();
mp-wp_genesis 147 $this->_logMsg(MC_LOGGER_INFO, implode(', ', $args));
mp-wp_genesis 148 }
mp-wp_genesis 149
mp-wp_genesis 150 function warn($msg) {
mp-wp_genesis 151 $args = func_get_args();
mp-wp_genesis 152 $this->_logMsg(MC_LOGGER_WARN, implode(', ', $args));
mp-wp_genesis 153 }
mp-wp_genesis 154
mp-wp_genesis 155 function error($msg) {
mp-wp_genesis 156 $args = func_get_args();
mp-wp_genesis 157 $this->_logMsg(MC_LOGGER_ERROR, implode(', ', $args));
mp-wp_genesis 158 }
mp-wp_genesis 159
mp-wp_genesis 160 function fatal($msg) {
mp-wp_genesis 161 $args = func_get_args();
mp-wp_genesis 162 $this->_logMsg(MC_LOGGER_FATAL, implode(', ', $args));
mp-wp_genesis 163 }
mp-wp_genesis 164
mp-wp_genesis 165 function isDebugEnabled() {
mp-wp_genesis 166 return $this->_level >= MC_LOGGER_DEBUG;
mp-wp_genesis 167 }
mp-wp_genesis 168
mp-wp_genesis 169 function isInfoEnabled() {
mp-wp_genesis 170 return $this->_level >= MC_LOGGER_INFO;
mp-wp_genesis 171 }
mp-wp_genesis 172
mp-wp_genesis 173 function isWarnEnabled() {
mp-wp_genesis 174 return $this->_level >= MC_LOGGER_WARN;
mp-wp_genesis 175 }
mp-wp_genesis 176
mp-wp_genesis 177 function isErrorEnabled() {
mp-wp_genesis 178 return $this->_level >= MC_LOGGER_ERROR;
mp-wp_genesis 179 }
mp-wp_genesis 180
mp-wp_genesis 181 function isFatalEnabled() {
mp-wp_genesis 182 return $this->_level >= MC_LOGGER_FATAL;
mp-wp_genesis 183 }
mp-wp_genesis 184
mp-wp_genesis 185 function _logMsg($level, $message) {
mp-wp_genesis 186 $roll = false;
mp-wp_genesis 187
mp-wp_genesis 188 if ($level < $this->_level)
mp-wp_genesis 189 return;
mp-wp_genesis 190
mp-wp_genesis 191 $logFile = $this->toOSPath($this->_path . "/" . $this->_filename);
mp-wp_genesis 192
mp-wp_genesis 193 switch ($level) {
mp-wp_genesis 194 case MC_LOGGER_DEBUG:
mp-wp_genesis 195 $levelName = "DEBUG";
mp-wp_genesis 196 break;
mp-wp_genesis 197
mp-wp_genesis 198 case MC_LOGGER_INFO:
mp-wp_genesis 199 $levelName = "INFO";
mp-wp_genesis 200 break;
mp-wp_genesis 201
mp-wp_genesis 202 case MC_LOGGER_WARN:
mp-wp_genesis 203 $levelName = "WARN";
mp-wp_genesis 204 break;
mp-wp_genesis 205
mp-wp_genesis 206 case MC_LOGGER_ERROR:
mp-wp_genesis 207 $levelName = "ERROR";
mp-wp_genesis 208 break;
mp-wp_genesis 209
mp-wp_genesis 210 case MC_LOGGER_FATAL:
mp-wp_genesis 211 $levelName = "FATAL";
mp-wp_genesis 212 break;
mp-wp_genesis 213 }
mp-wp_genesis 214
mp-wp_genesis 215 $logFile = str_replace('{level}', strtolower($levelName), $logFile);
mp-wp_genesis 216
mp-wp_genesis 217 $text = $this->_format;
mp-wp_genesis 218 $text = str_replace('{time}', date("Y-m-d H:i:s"), $text);
mp-wp_genesis 219 $text = str_replace('{level}', strtolower($levelName), $text);
mp-wp_genesis 220 $text = str_replace('{message}', $message, $text);
mp-wp_genesis 221 $message = $text . "\r\n";
mp-wp_genesis 222
mp-wp_genesis 223 // Check filesize
mp-wp_genesis 224 if (file_exists($logFile)) {
mp-wp_genesis 225 $size = @filesize($logFile);
mp-wp_genesis 226
mp-wp_genesis 227 if ($size + strlen($message) > $this->_maxSizeBytes)
mp-wp_genesis 228 $roll = true;
mp-wp_genesis 229 }
mp-wp_genesis 230
mp-wp_genesis 231 // Roll if the size is right
mp-wp_genesis 232 if ($roll) {
mp-wp_genesis 233 for ($i=$this->_maxFiles-1; $i>=1; $i--) {
mp-wp_genesis 234 $rfile = $this->toOSPath($logFile . "." . $i);
mp-wp_genesis 235 $nfile = $this->toOSPath($logFile . "." . ($i+1));
mp-wp_genesis 236
mp-wp_genesis 237 if (@file_exists($rfile))
mp-wp_genesis 238 @rename($rfile, $nfile);
mp-wp_genesis 239 }
mp-wp_genesis 240
mp-wp_genesis 241 @rename($logFile, $this->toOSPath($logFile . ".1"));
mp-wp_genesis 242
mp-wp_genesis 243 // Delete last logfile
mp-wp_genesis 244 $delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1));
mp-wp_genesis 245 if (@file_exists($delfile))
mp-wp_genesis 246 @unlink($delfile);
mp-wp_genesis 247 }
mp-wp_genesis 248
mp-wp_genesis 249 // Append log line
mp-wp_genesis 250 if (($fp = @fopen($logFile, "a")) != null) {
mp-wp_genesis 251 @fputs($fp, $message);
mp-wp_genesis 252 @fflush($fp);
mp-wp_genesis 253 @fclose($fp);
mp-wp_genesis 254 }
mp-wp_genesis 255 }
mp-wp_genesis 256
mp-wp_genesis 257 /**
mp-wp_genesis 258 * Converts a Unix path to OS specific path.
mp-wp_genesis 259 *
mp-wp_genesis 260 * @param String $path Unix path to convert.
mp-wp_genesis 261 */
mp-wp_genesis 262 function toOSPath($path) {
mp-wp_genesis 263 return str_replace("/", DIRECTORY_SEPARATOR, $path);
mp-wp_genesis 264 }
mp-wp_genesis 265 }
mp-wp_genesis 266
mp-wp_genesis 267 ?>