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