diff options
author | Kenny Katzgrau <katzgrau@gmail.com> | 2012-07-01 16:44:29 -0400 |
---|---|---|
committer | Kenny Katzgrau <katzgrau@gmail.com> | 2012-07-01 16:44:29 -0400 |
commit | 8281586471d08960bf693b5715b5c3a30f446a07 (patch) | |
tree | 0bee82bde7f1bc4d6e9d7eddebb2219e99e9e034 | |
parent | b6590e4a6840982e239900da7cc2ae71110998b5 (diff) | |
download | KLogger-8281586471d08960bf693b5715b5c3a30f446a07.zip KLogger-8281586471d08960bf693b5715b5c3a30f446a07.tar.gz KLogger-8281586471d08960bf693b5715b5c3a30f446a07.tar.bz2 |
Added the ability to pass objects as a second log argument. Special thanks to Cameron Will for the initial pull request0.2.0
-rw-r--r-- | README.markdown | 4 | ||||
-rwxr-xr-x | example/example.php | 7 | ||||
-rwxr-xr-x | src/KLogger.php | 59 |
3 files changed, 46 insertions, 24 deletions
diff --git a/README.markdown b/README.markdown index 4b61183..b068247 100644 --- a/README.markdown +++ b/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 diff --git a/example/example.php b/example/example.php index efbc317..6c27ef1 100755 --- a/example/example.php +++ b/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->logFatal('Fatal Test'); $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); diff --git a/src/KLogger.php b/src/KLogger.php index 00ce4e5..6766df3 100755 --- a/src/KLogger.php +++ b/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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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 @@ class KLogger * @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);
}
}
|