Browse files

Zend coding standards

  • Loading branch information...
1 parent 98e1a8c commit 772fa951a403739932ce07f0a51ed34ea6b70a49 Brian Fenton committed Oct 7, 2010
Showing with 141 additions and 151 deletions.
  1. +141 −151 src/KLogger.php
View
292 src/KLogger.php
@@ -1,185 +1,175 @@
<?php
+
/**
-* Finally, a light, permissions-checking logging class.
-*
-* Author : Kenny Katzgrau <katzgrau@gmail.com>
-* Date : July 26, 2008
-* Comments : Originally written for use with wpSearch
-* Website : http://codefury.net
-* Version : 1.0
-*
-* Usage:
-* $log = new KLogger ( "log.txt" , 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 priority setting
-*/
+ * Finally, a light, permissions-checking logging class.
+ *
+ * Author : Kenny Katzgrau <katzgrau@gmail.com>
+ * Date : July 26, 2008
+ * Comments : Originally written for use with wpSearch
+ * Website : http://codefury.net
+ * Version : 1.0
+ *
+ * Usage:
+ * $log = new KLogger ( "log.txt" , 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 priority setting
+ */
/**
-* Class documentation
-*/
- class KLogger
+ * Class documentation
+ */
+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)
{
-
- 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 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];
+ if ($priority === false) {
+ $priority = self::$_defaultPriority;
+ }
+
+ if ($logDirectory === false) {
+ if (count(self::$instances) > 0) {
+ return self::$instances[0];
+ } else {
+ $logDirectory = dirname(__FILE__);
}
+ }
- self::$instances[$logDirectory] = new self($logDirectory, $priority);
-
+ if (in_array($logDirectory, self::$instances)) {
return self::$instances[$logDirectory];
}
- public function __construct($logDirectory, $priority)
- {
- $logDirectory = rtrim($logDirectory, '/');
+ self::$instances[$logDirectory] = new self($logDirectory, $priority);
- if($priority == self::OFF) return;
+ return self::$instances[$logDirectory];
+ }
- $this->_logFile = $logDirectory
- . DIRECTORY_SEPARATOR
- . 'log_'
- . date('Y-m-d')
- . '.txt';
+ public function __construct($logDirectory, $priority)
+ {
+ $logDirectory = rtrim($logDirectory, '/');
- $this->_priority = $priority;
- if(!file_exists($logDirectory))
- {
- mkdir($logDirectory, self::$_defaultPermissions, TRUE);
- }
+ if ($priority == self::OFF) {
+ return;
+ }
- if(file_exists($this->_logFile))
- {
- if (!is_writable($this->_logFile))
- {
- $this->_logStatus = self::OPEN_FAILED;
- $this->_messageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
- return;
- }
- }
+ $this->_logFile = $logDirectory
+ . DIRECTORY_SEPARATOR
+ . 'log_'
+ . date('Y-m-d')
+ . '.txt';
- if(($this->_fileHandle = fopen($this->_logFile, "a" )))
- {
- $this->_logStatus = self::LOG_OPEN;
- $this->_messageQueue[] = "The log file was opened successfully.";
- }
- else
- {
+ $this->_priority = $priority;
+ if (!file_exists($logDirectory)) {
+ mkdir($logDirectory, self::$_defaultPermissions, true);
+ }
+
+ if (file_exists($this->_logFile)) {
+ if (!is_writable($this->_logFile)) {
$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)
- fclose($this->_fileHandle);
+ if (($this->_fileHandle = fopen($this->_logFile, "a"))) {
+ $this->_logStatus = self::LOG_OPEN;
+ $this->_messageQueue[] = "The log file was opened successfully.";
+ } else {
+ $this->_logStatus = self::OPEN_FAILED;
+ $this->_messageQueue[] = "The file could not be opened. Check permissions.";
}
+ }
- public function logInfo($line)
- {
- $this->log($line, self::INFO);
+ public function __destruct()
+ {
+ if ($this->_fileHandle) {
+ fclose($this->_fileHandle);
}
+ }
- public function logDebug($line)
- {
- $this->log($line, self::DEBUG);
- }
+ public function logInfo($line)
+ {
+ $this->log($line, self::INFO);
+ }
- public function logWarn($line)
- {
- $this->log($line, self::WARN);
- }
+ public function logDebug($line)
+ {
+ $this->log($line, self::DEBUG);
+ }
- public function logError($line)
- {
- $this->log($line, self::ERROR);
- }
+ public function logWarn($line)
+ {
+ $this->log($line, self::WARN);
+ }
- public function logFatal($line)
- {
- $this->log($line, KLogger::FATAL);
- }
+ public function logError($line)
+ {
+ $this->log($line, self::ERROR);
+ }
- public function log($line, $priority)
- {
- if($this->_priority <= $priority)
- {
- $status = $this->_getTimeLine($priority);
- $this->writeFreeFormLine ("$status $line \n");
- }
- }
+ public function logFatal($line)
+ {
+ $this->log($line, KLogger::FATAL);
+ }
- public function writeFreeFormLine($line)
- {
- if ( $this->_logStatus == self::LOG_OPEN
- && $this->_priority != self::OFF)
- {
- if (fwrite($this->_fileHandle, $line) === FALSE)
- {
- $this->_messageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
- }
- }
+ public function log($line, $priority)
+ {
+ if ($this->_priority <= $priority) {
+ $status = $this->_getTimeLine($priority);
+ $this->writeFreeFormLine("$status $line \n");
}
+ }
- 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 -->";
+ public function writeFreeFormLine($line)
+ {
+ if ($this->_logStatus == self::LOG_OPEN
+ && $this->_priority != self::OFF) {
+ 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)
+ {
+ $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 -->";
+ }
+ }
+}

0 comments on commit 772fa95

Please sign in to comment.