Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge

  • Loading branch information...
commit d4ba0ab2b2fa335d8d7cd3a31946b650aea20fc3 2 parents 7e28011 + 6cbee1a
Brian Fenton authored
Showing with 70 additions and 42 deletions.
  1. +70 −42 src/KLogger.php
View
112 src/KLogger.php
@@ -2,13 +2,14 @@
/**
* Finally, a light, permissions-checking logging class.
- * Comments : Originally written for use with wpSearch
- *
+ *
+ * Originally written for use with wpSearch
+ *
* Usage:
* $log = new KLogger('/var/log/', KLogger::INFO );
- * $log->logInfo("Returned a million search results"); //Prints to the log file
- * $log->logFatal("Oh dear."); //Prints to the log file
- * $log->logDebug("x = 5"); //Prints nothing due to current severity threshhold
+ * $log->logInfo('Returned a million search results'); //Prints to the log file
+ * $log->logFatal('Oh dear.'); //Prints to the log file
+ * $log->logDebug('x = 5'); //Prints nothing due to current severity threshhold
*
* @author Kenny Katzgrau <katzgrau@gmail.com>
* @since July 26, 2008
@@ -34,15 +35,15 @@ class KLogger
/**
* Internal status codes
*/
- const LOG_OPEN = 1;
- const OPEN_FAILED = 2;
- const LOG_CLOSED = 3;
+ const STATUS_LOG_OPEN = 1;
+ const STATUS_OPEN_FAILED = 2;
+ const STATUS_LOG_CLOSED = 3;
/**
* Current status of the log file
* @var integer
*/
- private $_logStatus = self::LOG_CLOSED;
+ private $_logStatus = self::STATUS_LOG_CLOSED;
/**
* Holds messages generated by the class
* @var array
@@ -52,12 +53,12 @@ class KLogger
* Path to the log file
* @var string
*/
- private $_logFile = null;
+ private $_logFilePath = null;
/**
* Current minimum logging threshold
* @var integer
*/
- private $_priority = self::INFO;
+ private $_severityThreshold = self::INFO;
/**
* This holds the file handle for this instance's log file
* @var resource
@@ -76,15 +77,15 @@ class KLogger
);
/**
- * Default priority of log messages, if not specified
+ * Default severity of log messages, if not specified
* @var integer
*/
- private static $_defaultPriority = self::DEBUG;
+ private static $_defaultSeverity = self::DEBUG;
/**
* Valid PHP date() format string for log timestamps
* @var string
*/
- private static $_dateFormat = "Y-m-d G:i:s";
+ private static $_dateFormat = 'Y-m-d G:i:s';
/**
* Octal notation for default permissions of the log file
* @var integer
@@ -101,13 +102,13 @@ class KLogger
* instance.
*
* @param string $logDirectory File path to the logging directory
- * @param integer $priority One of the pre-defined priority constants
+ * @param integer $severity One of the pre-defined severity constants
* @return KLogger
*/
- public static function instance($logDirectory = false, $priority = false)
+ public static function instance($logDirectory = false, $severity = false)
{
- if ($priority === false) {
- $priority = self::$_defaultPriority;
+ if ($severity === false) {
+ $severity = self::$_defaultSeverity;
}
if ($logDirectory === false) {
@@ -122,7 +123,7 @@ public static function instance($logDirectory = false, $priority = false)
return self::$instances[$logDirectory];
}
- self::$instances[$logDirectory] = new self($logDirectory, $priority);
+ self::$instances[$logDirectory] = new self($logDirectory, $severity);
return self::$instances[$logDirectory];
}
@@ -131,39 +132,39 @@ public static function instance($logDirectory = false, $priority = false)
* Class constructor
*
* @param string $logDirectory File path to the logging directory
- * @param integer $priority One of the pre-defined priority constants
+ * @param integer $severity One of the pre-defined severity constants
* @return void
*/
- public function __construct($logDirectory, $priority)
+ public function __construct($logDirectory, $severity)
{
- $logDirectory = rtrim($logDirectory, '/');
+ $logDirectory = rtrim($logDirectory, '\\/');
- if ($priority == self::OFF) {
+ if ($severity == self::OFF) {
return;
}
- $this->_logFile = $logDirectory
+ $this->_logFilePath = $logDirectory
. DIRECTORY_SEPARATOR
. 'log_'
. date('Y-m-d')
. '.txt';
- $this->_priority = $priority;
+ $this->_severityThreshold = $severity;
if (!file_exists($logDirectory)) {
mkdir($logDirectory, self::$_defaultPermissions, true);
}
- if (file_exists($this->_logFile) && !is_writable($this->_logFile)) {
- $this->_logStatus = self::OPEN_FAILED;
+ if (file_exists($this->_logFilePath) && !is_writable($this->_logFilePath)) {
+ $this->_logStatus = self::STATUS_OPEN_FAILED;
$this->_messageQueue[] = $this->_messages['writefail'];
return;
}
- if (($this->_fileHandle = fopen($this->_logFile, "a"))) {
- $this->_logStatus = self::LOG_OPEN;
+ if (($this->_fileHandle = fopen($this->_logFilePath, 'a'))) {
+ $this->_logStatus = self::STATUS_LOG_OPEN;
$this->_messageQueue[] = $this->_messages['opensuccess'];
} else {
- $this->_logStatus = self::OPEN_FAILED;
+ $this->_logStatus = self::STATUS_OPEN_FAILED;
$this->_messageQueue[] = $this->_messages['openfail'];
}
}
@@ -179,7 +180,34 @@ public function __destruct()
}
/**
- * Writes a $line to the log with a priority level of INFO
+ * Returns (and removes) the last message from the queue.
+ * @return string
+ */
+ public function getMessage()
+ {
+ return array_pop($this->_messageQueue);
+ }
+
+ /**
+ * Returns the entire message queue (leaving it intact)
+ * @return array
+ */
+ public function getMessages()
+ {
+ return $this->_messageQueue;
+ }
+
+ /**
+ * Empties the message queue
+ * @return void
+ */
+ public function clearMessages()
+ {
+ $this->_messageQueue = array();
+ }
+
+ /**
+ * Writes a $line to the log with a severity level of INFO
*
* @param string $line Information to log
* @return void
@@ -190,7 +218,7 @@ public function logInfo($line)
}
/**
- * Writes a $line to the log with a priority level of DEBUG
+ * Writes a $line to the log with a severity level of DEBUG
*
* @param string $line Information to log
* @return void
@@ -201,7 +229,7 @@ public function logDebug($line)
}
/**
- * Writes a $line to the log with a priority level of WARN
+ * Writes a $line to the log with a severity level of WARN
*
* @param string $line Information to log
* @return void
@@ -212,7 +240,7 @@ public function logWarn($line)
}
/**
- * Writes a $line to the log with a priority level of ERROR
+ * Writes a $line to the log with a severity level of ERROR
*
* @param string $line Information to log
* @return void
@@ -223,7 +251,7 @@ public function logError($line)
}
/**
- * Writes a $line to the log with a priority level of FATAL
+ * Writes a $line to the log with a severity level of FATAL
*
* @param string $line Information to log
* @return void
@@ -234,15 +262,15 @@ public function logFatal($line)
}
/**
- * Writes a $line to the log with the given priority
+ * Writes a $line to the log with the given severity
*
* @param string $line Text to add to the log
- * @param integer $priority Priority level of log message (use constants)
+ * @param integer $severity Severity level of log message (use constants)
*/
- public function log($line, $priority)
+ public function log($line, $severity)
{
- if ($this->_priority <= $priority) {
- $status = $this->_getTimeLine($priority);
+ if ($this->_severityThreshold <= $severity) {
+ $status = $this->_getTimeLine($severity);
$this->writeFreeFormLine("$status $line \n");
}
}
@@ -255,8 +283,8 @@ public function log($line, $priority)
*/
public function writeFreeFormLine($line)
{
- if ($this->_logStatus == self::LOG_OPEN
- && $this->_priority != self::OFF) {
+ if ($this->_logStatus == self::STATUS_LOG_OPEN
+ && $this->_severityThreshold != self::OFF) {
if (fwrite($this->_fileHandle, $line) === false) {
$this->_messageQueue[] = $this->_messages['writefail'];
}
Please sign in to comment.
Something went wrong with that request. Please try again.