Permalink
Browse files

Added the ability to pass objects as a second log argument. Special t…

…hanks to Cameron Will for the initial pull request
1 parent b6590e4 commit 8281586471d08960bf693b5715b5c3a30f446a07 @katzgrau committed Jul 1, 2012
Showing with 46 additions and 24 deletions.
  1. +3 −1 README.markdown
  2. +6 −1 example/example.php
  3. +37 −22 src/KLogger.php
View
4 README.markdown
@@ -23,6 +23,7 @@ download [here](http://github.com/katzgrau/KLogger/downloads).
$log = new KLogger('/var/log/'); # Specify the log directory
$log->logInfo('Returned a million search results'); //Prints to the log file
$log->logFatal('Oh dear.'); //Prints to the log file
+ $log->logInfo('Here is an object', $obj); //Prints to the log file with a dump of the object
## Goals
@@ -48,10 +49,11 @@ Additionally, it's been used in numerous projects, both commercial and personal.
## Special Thanks
-Special thanks to all contributors, which right now includes two people:
+Special thanks to all contributors, which right now includes three people:
[Tim Kinnane](http://twitter.com/etherealtim)
[Brian Fenton](http://github.com/fentie)
+[Cameron Will](https://github.com/cwill747)
## License
View
7 example/example.php
@@ -3,7 +3,9 @@
# Should log to the same directory as this file
require dirname(__FILE__) . '/../src/KLogger.php';
-$log = KLogger::instance(dirname(__FILE__), KLogger::DEBUG);
+$log = KLogger::instance(dirname(__FILE__), KLogger::DEBUG);
+$args1 = array('a' => array('b' => 'c'), 'd');
+$args2 = NULL;
$log->logInfo('Info Test');
$log->logNotice('Notice Test');
@@ -13,3 +15,6 @@
$log->logAlert('Alert Test');
$log->logCrit('Crit test');
$log->logEmerg('Emerg Test');
+
+$log->logInfo('Testing passing an array or object', $args1);
+$log->logWarn('Testing passing a NULL value', $args2);
View
59 src/KLogger.php
@@ -6,15 +6,15 @@
* Originally written for use with wpSearch
*
* Usage:
- * $log = new KLogger('/var/log/', KLogger::INFO );
+ * $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
*
* @author Kenny Katzgrau <katzgrau@gmail.com>
- * @since July 26, 2008
+ * @since July 26, 2008 — Last update July 1, 2012
* @link http://codefury.net
- * @version 0.1
+ * @version 0.2.0
*/
/**
@@ -54,6 +54,13 @@ class KLogger
const STATUS_LOG_CLOSED = 3;
/**
+ * We need a default argument value in order to add the ability to easily
+ * print out objects etc. But we can't use NULL, 0, FALSE, etc, because those
+ * are often the values the developers will test for. So we'll make one up.
+ */
+ const NO_ARGUMENTS = 'KLogger::NO_ARGUMENTS';
+
+ /**
* Current status of the log file
* @var integer
*/
@@ -198,7 +205,7 @@ public function __destruct()
* @param string $line Information to log
* @return void
*/
- public function logDebug($line)
+ public function logDebug($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::DEBUG);
}
@@ -247,9 +254,9 @@ public static function setDateFormat($dateFormat)
* @param string $line Information to log
* @return void
*/
- public function logInfo($line)
+ public function logInfo($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::INFO);
+ $this->log($line, self::INFO, $args);
}
/**
@@ -259,9 +266,9 @@ public function logInfo($line)
* @param string $line Information to log
* @return void
*/
- public function logNotice($line)
+ public function logNotice($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::NOTICE);
+ $this->log($line, self::NOTICE, $args);
}
/**
@@ -272,9 +279,9 @@ public function logNotice($line)
* @param string $line Information to log
* @return void
*/
- public function logWarn($line)
+ public function logWarn($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::WARN);
+ $this->log($line, self::WARN, $args);
}
/**
@@ -284,9 +291,9 @@ public function logWarn($line)
* @param string $line Information to log
* @return void
*/
- public function logError($line)
+ public function logError($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::ERR);
+ $this->log($line, self::ERR, $args);
}
/**
@@ -297,9 +304,9 @@ public function logError($line)
* @return void
* @deprecated Use logCrit
*/
- public function logFatal($line)
+ public function logFatal($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::FATAL);
+ $this->log($line, self::FATAL, $args);
}
/**
@@ -308,9 +315,9 @@ public function logFatal($line)
* @param string $line Information to log
* @return void
*/
- public function logAlert($line)
+ public function logAlert($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::ALERT);
+ $this->log($line, self::ALERT, $args);
}
/**
@@ -319,9 +326,9 @@ public function logAlert($line)
* @param string $line Information to log
* @return void
*/
- public function logCrit($line)
+ public function logCrit($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::CRIT);
+ $this->log($line, self::CRIT, $args);
}
/**
@@ -330,9 +337,9 @@ public function logCrit($line)
* @param string $line Information to log
* @return void
*/
- public function logEmerg($line)
+ public function logEmerg($line, $args = self::NO_ARGUMENTS)
{
- $this->log($line, self::EMERG);
+ $this->log($line, self::EMERG, $args);
}
/**
@@ -341,11 +348,19 @@ public function logEmerg($line)
* @param string $line Text to add to the log
* @param integer $severity Severity level of log message (use constants)
*/
- public function log($line, $severity)
+ public function log($line, $severity, $args = self::NO_ARGUMENTS)
{
if ($this->_severityThreshold >= $severity) {
$status = $this->_getTimeLine($severity);
- $this->writeFreeFormLine("$status $line \n");
+
+ $line = "$status $line";
+
+ if($args !== self::NO_ARGUMENTS) {
+ /* Print the passed object value */
+ $line = $line . '; ' . var_export($args, true);
+ }
+
+ $this->writeFreeFormLine($line . PHP_EOL);
}
}

0 comments on commit 8281586

Please sign in to comment.