3 * $Id: Logger.class.php 10 2007-05-27 10:55:12Z spocke $
\r
5 * @package MCFileManager.filesystems
\r
7 * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
\r
10 // File type contstants
\r
11 define('MC_LOGGER_DEBUG', 0);
\r
12 define('MC_LOGGER_INFO', 10);
\r
13 define('MC_LOGGER_WARN', 20);
\r
14 define('MC_LOGGER_ERROR', 30);
\r
15 define('MC_LOGGER_FATAL', 40);
\r
18 * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
\r
19 * designed to be compact but still powerful and flexible.
\r
21 class Moxiecode_Logger {
\r
32 * Constructs a new logger instance.
\r
34 function Moxiecode_Logger() {
\r
36 $this->_filename = "{level}.log";
\r
37 $this->setMaxSize("100k");
\r
38 $this->_maxFiles = 10;
\r
39 $this->_level = MC_LOGGER_DEBUG;
\r
40 $this->_format = "[{time}] [{level}] {message}";
\r
44 * Sets the current log level, use the MC_LOGGER constants.
\r
46 * @param int $level Log level instance for example MC_LOGGER_DEBUG.
\r
48 function setLevel($level) {
\r
49 if (is_string($level)) {
\r
50 switch (strtolower($level)) {
\r
52 $level = MC_LOGGER_DEBUG;
\r
56 $level = MC_LOGGER_INFO;
\r
61 $level = MC_LOGGER_WARN;
\r
65 $level = MC_LOGGER_ERROR;
\r
69 $level = MC_LOGGER_FATAL;
\r
73 $level = MC_LOGGER_FATAL;
\r
77 $this->_level = $level;
\r
81 * Returns the current log level for example MC_LOGGER_DEBUG.
\r
83 * @return int Current log level for example MC_LOGGER_DEBUG.
\r
85 function getLevel() {
\r
86 return $this->_level;
\r
89 function setPath($path) {
\r
90 $this->_path = $path;
\r
93 function getPath() {
\r
94 return $this->_path;
\r
97 function setFileName($file_name) {
\r
98 $this->_filename = $file_name;
\r
101 function getFileName() {
\r
102 return $this->_filename;
\r
105 function setFormat($format) {
\r
106 $this->_format = $format;
\r
109 function getFormat() {
\r
110 return $this->_format;
\r
113 function setMaxSize($size) {
\r
114 // Fix log max size
\r
115 $logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
\r
118 if (strpos((strtolower($size)), "k") > 0)
\r
119 $logMaxSizeBytes *= 1024;
\r
122 if (strpos((strtolower($size)), "m") > 0)
\r
123 $logMaxSizeBytes *= (1024 * 1024);
\r
125 $this->_maxSizeBytes = $logMaxSizeBytes;
\r
126 $this->_maxSize = $size;
\r
129 function getMaxSize() {
\r
130 return $this->_maxSize;
\r
133 function setMaxFiles($max_files) {
\r
134 $this->_maxFiles = $max_files;
\r
137 function getMaxFiles() {
\r
138 return $this->_maxFiles;
\r
141 function debug($msg) {
\r
142 $args = func_get_args();
\r
143 $this->_logMsg(MC_LOGGER_DEBUG, implode(', ', $args));
\r
146 function info($msg) {
\r
147 $args = func_get_args();
\r
148 $this->_logMsg(MC_LOGGER_INFO, implode(', ', $args));
\r
151 function warn($msg) {
\r
152 $args = func_get_args();
\r
153 $this->_logMsg(MC_LOGGER_WARN, implode(', ', $args));
\r
156 function error($msg) {
\r
157 $args = func_get_args();
\r
158 $this->_logMsg(MC_LOGGER_ERROR, implode(', ', $args));
\r
161 function fatal($msg) {
\r
162 $args = func_get_args();
\r
163 $this->_logMsg(MC_LOGGER_FATAL, implode(', ', $args));
\r
166 function isDebugEnabled() {
\r
167 return $this->_level >= MC_LOGGER_DEBUG;
\r
170 function isInfoEnabled() {
\r
171 return $this->_level >= MC_LOGGER_INFO;
\r
174 function isWarnEnabled() {
\r
175 return $this->_level >= MC_LOGGER_WARN;
\r
178 function isErrorEnabled() {
\r
179 return $this->_level >= MC_LOGGER_ERROR;
\r
182 function isFatalEnabled() {
\r
183 return $this->_level >= MC_LOGGER_FATAL;
\r
186 function _logMsg($level, $message) {
\r
189 if ($level < $this->_level)
\r
192 $logFile = $this->toOSPath($this->_path . "/" . $this->_filename);
\r
195 case MC_LOGGER_DEBUG:
\r
196 $levelName = "DEBUG";
\r
199 case MC_LOGGER_INFO:
\r
200 $levelName = "INFO";
\r
203 case MC_LOGGER_WARN:
\r
204 $levelName = "WARN";
\r
207 case MC_LOGGER_ERROR:
\r
208 $levelName = "ERROR";
\r
211 case MC_LOGGER_FATAL:
\r
212 $levelName = "FATAL";
\r
216 $logFile = str_replace('{level}', strtolower($levelName), $logFile);
\r
218 $text = $this->_format;
\r
219 $text = str_replace('{time}', date("Y-m-d H:i:s"), $text);
\r
220 $text = str_replace('{level}', strtolower($levelName), $text);
\r
221 $text = str_replace('{message}', $message, $text);
\r
222 $message = $text . "\r\n";
\r
225 if (file_exists($logFile)) {
\r
226 $size = @filesize($logFile);
\r
228 if ($size + strlen($message) > $this->_maxSizeBytes)
\r
232 // Roll if the size is right
\r
234 for ($i=$this->_maxFiles-1; $i>=1; $i--) {
\r
235 $rfile = $this->toOSPath($logFile . "." . $i);
\r
236 $nfile = $this->toOSPath($logFile . "." . ($i+1));
\r
238 if (@file_exists($rfile))
\r
239 @rename($rfile, $nfile);
\r
242 @rename($logFile, $this->toOSPath($logFile . ".1"));
\r
244 // Delete last logfile
\r
245 $delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1));
\r
246 if (@file_exists($delfile))
\r
251 if (($fp = @fopen($logFile, "a")) != null) {
\r
252 @fputs($fp, $message);
\r
259 * Converts a Unix path to OS specific path.
\r
261 * @param String $path Unix path to convert.
\r
263 function toOSPath($path) {
\r
264 return str_replace("/", DIRECTORY_SEPARATOR, $path);
\r