summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny <kkatzgrau@hugeinc.com>2010-06-24 00:01:45 -0400
committerKenny <kkatzgrau@hugeinc.com>2010-06-24 00:01:45 -0400
commita74818bb8e80593dfadef06592c6c906af3cf941 (patch)
tree865b016fa9c6ce9d52adb1284c82f37c50bbfbc4
parentf6fe86a3144b41089469cdfc1c8e9a8cba4658e0 (diff)
downloadKLogger-a74818bb8e80593dfadef06592c6c906af3cf941.zip
KLogger-a74818bb8e80593dfadef06592c6c906af3cf941.tar.gz
KLogger-a74818bb8e80593dfadef06592c6c906af3cf941.tar.bz2
* Normalized casing of members and methods
* Added 'rolling' log files (just logs to a date-stamped file) * Added getInstance() with singleton-like behavior * Removed references to the class name in favor of self
-rw-r--r--.gitignore4
-rw-r--r--README.markdown30
-rwxr-xr-xsrc/KLogger.php325
3 files changed, 201 insertions, 158 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f2fdd26
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+src/test.php
+test/
+test/*
+test.php
diff --git a/README.markdown b/README.markdown
index 0e9bbec..ebe7cbb 100644
--- a/README.markdown
+++ b/README.markdown
@@ -12,7 +12,7 @@ file permissions (which is expected). It was meant to be a class that you could
quickly include into a project and have working right away.
The class was written in 2008, but I have since received a number of emails both
-say 'thanks' and asking me to add features.
+saying 'thanks' and asking me to add features.
This github project will host the development of the next version of KLogger.
The original version of KLogger is tagged as version 0.1, and is available for
@@ -23,19 +23,19 @@ download [here](http://github.com/katzgrau/KLogger/downloads).
KLogger's API will change for the better for it's 1.0 release. Expected changes
include:
- * Adherence to the Zend Coding Standards for PHP (right now, KLogger has a
- .NET-ish member and method naming convention). The names of private
- methods will be prefixed with '_' and camel-cased, for example.
- * The implementation of a singleton pattern. A logger is likely needed in
- many parts of an application — there should be no need to instantiate
- multiple instances. You'll probably see: `$log = KLogger::instance();` in
- the near future.
- * Implementation of rolling log files. I've debated whether log files should
- based on simply the date, or both the date size of the log file. I'm
- thinking just the date will be sufficient to start.
- * Put KLogger under a specific license (which will happen below)
- * Have a bit more encapsulation of class members (and erase my cheeky
- comments about not having encapsulation)
+ * Adherence to the Zend Coding Standards for PHP (right now, KLogger has a
+ .NET-ish member and method naming convention). The names of private
+ methods will be prefixed with '_' and camel-cased, for example.
+ * The implementation of a singleton pattern. A logger is likely needed in
+ many parts of an application — there should be no need to instantiate
+ multiple instances. You'll probably see: `$log = KLogger::instance();` in
+ the near future.
+ * Implementation of rolling log files. I've debated whether log files should
+ based on simply the date, or both the date size of the log file. I'm
+ thinking just the date will be sufficient to start.
+ * Put KLogger under a specific license (which will happen below)
+ * Have a bit more encapsulation of class members (and erase my cheeky
+ comments about not having encapsulation)
## Why use KLogger?
@@ -56,6 +56,8 @@ Additionally, it's been used in numerous projects, both commercial and personal.
## License
+The MIT License
+
Copyright (c) 2008-2010 Kenny Katzgrau <katzgrau@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
diff --git a/src/KLogger.php b/src/KLogger.php
index 9f53788..363da9a 100755
--- a/src/KLogger.php
+++ b/src/KLogger.php
@@ -1,148 +1,185 @@
<?php
-
- /* Finally, A light, permissions-checking logging class.
- *
- * Author : Kenneth 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 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. */
- public $Log_Status = KLogger::LOG_CLOSED;
- public $DateFormat = "Y-m-d G:i:s";
- public $MessageQueue;
-
- private $log_file;
- private $priority = KLogger::INFO;
-
- private $file_handle;
-
- public function __construct( $filepath , $priority )
- {
- if ( $priority == KLogger::OFF ) return;
-
- $this->log_file = $filepath;
- $this->MessageQueue = array();
- $this->priority = $priority;
-
- if ( file_exists( $this->log_file ) )
- {
- if ( !is_writable($this->log_file) )
- {
- $this->Log_Status = KLogger::OPEN_FAILED;
- $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
- return;
- }
- }
-
- if ( $this->file_handle = fopen( $this->log_file , "a" ) )
- {
- $this->Log_Status = KLogger::LOG_OPEN;
- $this->MessageQueue[] = "The log file was opened successfully.";
- }
- else
- {
- $this->Log_Status = KLogger::OPEN_FAILED;
- $this->MessageQueue[] = "The file could not be opened. Check permissions.";
- }
-
- return;
- }
-
- public function __destruct()
- {
- if ( $this->file_handle )
- fclose( $this->file_handle );
- }
-
- public function LogInfo($line)
- {
- $this->Log( $line , KLogger::INFO );
- }
-
- public function LogDebug($line)
- {
- $this->Log( $line , KLogger::DEBUG );
- }
-
- public function LogWarn($line)
- {
- $this->Log( $line , KLogger::WARN );
- }
-
- public function LogError($line)
- {
- $this->Log( $line , KLogger::ERROR );
- }
-
- public function LogFatal($line)
- {
- $this->Log( $line , KLogger::FATAL );
- }
-
- public function Log($line, $priority)
- {
- if ( $this->priority <= $priority )
- {
- $status = $this->getTimeLine( $priority );
- $this->WriteFreeFormLine ( "$status $line \n" );
- }
- }
-
- public function WriteFreeFormLine( $line )
- {
- if ( $this->Log_Status == KLogger::LOG_OPEN && $this->priority != KLogger::OFF )
- {
- if (fwrite( $this->file_handle , $line ) === false) {
- $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
- }
- }
- }
-
- private function getTimeLine( $level )
- {
- $time = date( $this->DateFormat );
-
- switch( $level )
- {
- case KLogger::INFO:
- return "$time - INFO -->";
- case KLogger::WARN:
- return "$time - WARN -->";
- case KLogger::DEBUG:
- return "$time - DEBUG -->";
- case KLogger::ERROR:
- return "$time - ERROR -->";
- case KLogger::FATAL:
- return "$time - FATAL -->";
- default:
- return "$time - LOG -->";
- }
- }
-
- }
+ /**
+ * 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
+ {
+
+ 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];
+ }
+
+ self::$instances[$logDirectory] = new self($logDirectory, $priority);
+
+ return self::$instances[$logDirectory];
+ }
+
+ public function __construct($logDirectory, $priority)
+ {
+ $logDirectory = rtrim($logDirectory, '/');
+
+ if($priority == self::OFF) return;
+
+ $this->_logFile = $logDirectory
+ . DIRECTORY_SEPARATOR
+ . 'log_'
+ . date('Y-m-d')
+ . '.txt';
+
+ $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 exists, but could not be opened for writing. Check that appropriate permissions have been set.";
+ return;
+ }
+ }
+
+ 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 __destruct()
+ {
+ if ($this->_fileHandle)
+ fclose($this->_fileHandle);
+ }
+
+ public function logInfo($line)
+ {
+ $this->log($line, self::INFO);
+ }
+
+ public function logDebug($line)
+ {
+ $this->log($line, self::DEBUG);
+ }
+
+ public function logWarn($line)
+ {
+ $this->log($line, self::WARN);
+ }
+
+ public function logError($line)
+ {
+ $this->log($line, self::ERROR);
+ }
+
+ public function logFatal($line)
+ {
+ $this->log($line, KLogger::FATAL);
+ }
+
+ public function log($line, $priority)
+ {
+ if($this->_priority <= $priority)
+ {
+ $status = $this->_getTimeLine($priority);
+ $this->writeFreeFormLine ("$status $line \n");
+ }
+ }
+
+ 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 -->";
+ }
+ }
+
+ }
?> \ No newline at end of file