Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Fenton
committed
Oct 7, 2010
1 parent
98e1a8c
commit 772fa95
Showing
1 changed file
with
141 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,185 +1,175 @@ | |||
<?php | <?php | ||
|
|||
/** | /** | ||
* Finally, a light, permissions-checking logging class. | * Finally, a light, permissions-checking logging class. | ||
* | * | ||
* Author : Kenny Katzgrau <katzgrau@gmail.com> | * Author : Kenny Katzgrau <katzgrau@gmail.com> | ||
* Date : July 26, 2008 | * Date : July 26, 2008 | ||
* Comments : Originally written for use with wpSearch | * Comments : Originally written for use with wpSearch | ||
* Website : http://codefury.net | * Website : http://codefury.net | ||
* Version : 1.0 | * Version : 1.0 | ||
* | * | ||
* Usage: | * Usage: | ||
* $log = new KLogger ( "log.txt" , KLogger::INFO ); | * $log = new KLogger ( "log.txt" , KLogger::INFO ); | ||
* $log->_logInfo("Returned a million search results"); //Prints to the log file | * $log->_logInfo("Returned a million search results"); //Prints to the log file | ||
* $log->_logFatal("Oh dear."); //Prints to the log file | * $log->_logFatal("Oh dear."); //Prints to the log file | ||
* $log->_logDebug("x = 5"); //Prints nothing due to priority setting | * $log->_logDebug("x = 5"); //Prints nothing due to priority setting | ||
*/ | */ | ||
|
|
||
/** | /** | ||
* Class documentation | * Class documentation | ||
*/ | */ | ||
class KLogger | class KLogger | ||
{ | |||
const DEBUG = 1; // Most Verbose | |||
const INFO = 2; // ... | |||
const WARN = 3; // ... | |||
const ERROR = 4; // ... | |||
const FATAL = 5; // Least Verbose | |||
const OFF = 6; // Nothing at all. | |||
|
|||
const LOG_OPEN = 1; | |||
const OPEN_FAILED = 2; | |||
const LOG_CLOSED = 3; | |||
|
|||
/* Public members: Not so much of an example of encapsulation, but that's okay. */ | |||
|
|||
private $_logStatus = self::LOG_CLOSED; | |||
private $_messageQueue = array(); | |||
private $_logFile = null; | |||
private $_priority = self::INFO; | |||
private $_fileHandle = null; | |||
private static $_defaultPriority = self::DEBUG; | |||
private static $_dateFormat = "Y-m-d G:i:s"; | |||
private static $_defaultPermissions = 0777; | |||
private static $instances = array(); | |||
|
|||
public static function instance($logDirectory = false, $priority = false) | |||
{ | { | ||
|
if ($priority === false) { | ||
const DEBUG = 1; // Most Verbose | $priority = self::$_defaultPriority; | ||
const INFO = 2; // ... | } | ||
const WARN = 3; // ... |
|
||
const ERROR = 4; // ... | if ($logDirectory === false) { | ||
const FATAL = 5; // Least Verbose | if (count(self::$instances) > 0) { | ||
const OFF = 6; // Nothing at all. | return self::$instances[0]; | ||
|
} else { | ||
const LOG_OPEN = 1; | $logDirectory = dirname(__FILE__); | ||
const OPEN_FAILED = 2; | |||
const LOG_CLOSED = 3; | |||
|
|||
/* Public members: Not so much of an example of encapsulation, but that's okay. */ | |||
private $_logStatus = self::LOG_CLOSED; | |||
private static $_defaultPriority = self::DEBUG; | |||
private static $_dateFormat = "Y-m-d G:i:s"; | |||
private static $_defaultPermissions= 0777; | |||
private $_messageQueue = array(); | |||
private $_logFile = NULL; | |||
private $_priority = self::INFO; | |||
private $_fileHandle = NULL; | |||
|
|||
private static $instances = array(); | |||
|
|||
public static function instance($logDirectory = FALSE, $priority = FALSE) | |||
{ | |||
if($priority === FALSE) $priority = self::$_defaultPriority; | |||
|
|||
if($logDirectory === FALSE) | |||
{ | |||
if(count(self::$instances) > 0) | |||
return self::$instances[0]; | |||
else | |||
$logDirectory = dirname(__FILE__); | |||
} | |||
|
|||
if(in_array($logDirectory, self::$instances)) | |||
{ | |||
return self::$instances[$logDirectory]; | |||
} | } | ||
} | |||
|
|
||
self::$instances[$logDirectory] = new self($logDirectory, $priority); | if (in_array($logDirectory, self::$instances)) { | ||
|
|||
return self::$instances[$logDirectory]; | return self::$instances[$logDirectory]; | ||
} | } | ||
|
|
||
public function __construct($logDirectory, $priority) | self::$instances[$logDirectory] = new self($logDirectory, $priority); | ||
{ | |||
$logDirectory = rtrim($logDirectory, '/'); | |||
|
|
||
if($priority == self::OFF) return; | return self::$instances[$logDirectory]; | ||
} | |||
|
|
||
$this->_logFile = $logDirectory | public function __construct($logDirectory, $priority) | ||
. DIRECTORY_SEPARATOR | { | ||
. 'log_' | $logDirectory = rtrim($logDirectory, '/'); | ||
. date('Y-m-d') | |||
. '.txt'; | |||
|
|
||
$this->_priority = $priority; | if ($priority == self::OFF) { | ||
if(!file_exists($logDirectory)) | return; | ||
{ | } | ||
mkdir($logDirectory, self::$_defaultPermissions, TRUE); | |||
} | |||
|
|
||
if(file_exists($this->_logFile)) | $this->_logFile = $logDirectory | ||
{ | . DIRECTORY_SEPARATOR | ||
if (!is_writable($this->_logFile)) | . 'log_' | ||
{ | . date('Y-m-d') | ||
$this->_logStatus = self::OPEN_FAILED; | . '.txt'; | ||
$this->_messageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set."; | |||
return; | |||
} | |||
} | |||
|
|
||
if(($this->_fileHandle = fopen($this->_logFile, "a" ))) | $this->_priority = $priority; | ||
{ | if (!file_exists($logDirectory)) { | ||
$this->_logStatus = self::LOG_OPEN; | mkdir($logDirectory, self::$_defaultPermissions, true); | ||
$this->_messageQueue[] = "The log file was opened successfully."; | } | ||
} |
|
||
else | if (file_exists($this->_logFile)) { | ||
{ | if (!is_writable($this->_logFile)) { | ||
$this->_logStatus = self::OPEN_FAILED; | $this->_logStatus = self::OPEN_FAILED; | ||
$this->_messageQueue[] = "The file could not be opened. Check permissions."; | $this->_messageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set."; | ||
return; | |||
} | } | ||
} | } | ||
|
|
||
public function __destruct() | if (($this->_fileHandle = fopen($this->_logFile, "a"))) { | ||
{ | $this->_logStatus = self::LOG_OPEN; | ||
if ($this->_fileHandle) | $this->_messageQueue[] = "The log file was opened successfully."; | ||
fclose($this->_fileHandle); | } else { | ||
$this->_logStatus = self::OPEN_FAILED; | |||
$this->_messageQueue[] = "The file could not be opened. Check permissions."; | |||
} | } | ||
} | |||
|
|
||
public function logInfo($line) | public function __destruct() | ||
{ | { | ||
$this->log($line, self::INFO); | if ($this->_fileHandle) { | ||
fclose($this->_fileHandle); | |||
} | } | ||
} | |||
|
|
||
public function logDebug($line) | public function logInfo($line) | ||
{ | { | ||
$this->log($line, self::DEBUG); | $this->log($line, self::INFO); | ||
} | } | ||
|
|
||
public function logWarn($line) | public function logDebug($line) | ||
{ | { | ||
$this->log($line, self::WARN); | $this->log($line, self::DEBUG); | ||
} | } | ||
|
|
||
public function logError($line) | public function logWarn($line) | ||
{ | { | ||
$this->log($line, self::ERROR); | $this->log($line, self::WARN); | ||
} | } | ||
|
|
||
public function logFatal($line) | public function logError($line) | ||
{ | { | ||
$this->log($line, KLogger::FATAL); | $this->log($line, self::ERROR); | ||
} | } | ||
|
|
||
public function log($line, $priority) | public function logFatal($line) | ||
{ | { | ||
if($this->_priority <= $priority) | $this->log($line, KLogger::FATAL); | ||
{ | } | ||
$status = $this->_getTimeLine($priority); | |||
$this->writeFreeFormLine ("$status $line \n"); | |||
} | |||
} | |||
|
|
||
public function writeFreeFormLine($line) | public function log($line, $priority) | ||
{ | { | ||
if ( $this->_logStatus == self::LOG_OPEN | if ($this->_priority <= $priority) { | ||
&& $this->_priority != self::OFF) | $status = $this->_getTimeLine($priority); | ||
{ | $this->writeFreeFormLine("$status $line \n"); | ||
if (fwrite($this->_fileHandle, $line) === FALSE) | |||
{ | |||
$this->_messageQueue[] = "The file could not be written to. Check that appropriate permissions have been set."; | |||
} | |||
} | |||
} | } | ||
} | |||
|
|
||
private function _getTimeLine($level) | public function writeFreeFormLine($line) | ||
{ | { | ||
$time = date(self::$_dateFormat); | if ($this->_logStatus == self::LOG_OPEN | ||
|
&& $this->_priority != self::OFF) { | ||
switch($level) | if (fwrite($this->_fileHandle, $line) === false) { | ||
{ | $this->_messageQueue[] = "The file could not be written to. Check that appropriate permissions have been set."; | ||
case self::INFO: | |||
return "$time - INFO -->"; | |||
case self::WARN: | |||
return "$time - WARN -->"; | |||
case self::DEBUG: | |||
return "$time - DEBUG -->"; | |||
case self::ERROR: | |||
return "$time - ERROR -->"; | |||
case self::FATAL: | |||
return "$time - FATAL -->"; | |||
default: | |||
return "$time - LOG -->"; | |||
} | } | ||
} | } | ||
|
|||
} | } | ||
|
|
||
|
private function _getTimeLine($level) | ||
?> | { | ||
$time = date(self::$_dateFormat); | |||
|
|||
switch ($level) { | |||
case self::INFO: | |||
return "$time - INFO -->"; | |||
case self::WARN: | |||
return "$time - WARN -->"; | |||
case self::DEBUG: | |||
return "$time - DEBUG -->"; | |||
case self::ERROR: | |||
return "$time - ERROR -->"; | |||
case self::FATAL: | |||
return "$time - FATAL -->"; | |||
default: | |||
return "$time - LOG -->"; | |||
} | |||
} | |||
} |