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