Skip to content

Commit

Permalink
converted to using new severities as per RFC, updated shortcut method…
Browse files Browse the repository at this point in the history
…s accordingly
  • Loading branch information
Brian Fenton committed Oct 13, 2010
1 parent d4ba0ab commit 9ab47fa
Showing 1 changed file with 92 additions and 20 deletions.
112 changes: 92 additions & 20 deletions src/KLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@
class KLogger
{
/**
* Error logging thresholds, from low to high
* Error severity, from low to high. From BSD syslog RFC, secion 4.1.1
* @link http://www.faqs.org/rfcs/rfc3164.html
*/
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 EMERG = 0; // Emergency: system is unusable
const ALERT = 1; // Alert: action must be taken immediately
const CRIT = 2; // Critical: critical conditions
const ERR = 3; // Error: error conditions
const WARN = 4; // Warning: warning conditions
const NOTICE = 5; // Notice: normal but significant condition
const INFO = 6; // Informational: informational messages
const DEBUG = 7; // Debug: debug messages

//custom logging level
/**
* Log nothing at all
*/
const OFF = 8;
/**
* Alias for CRIT
* @deprecated
*/
const FATAL = 2;

/**
* Internal status codes
Expand All @@ -48,12 +62,12 @@ class KLogger
* Holds messages generated by the class
* @var array
*/
private $_messageQueue = array();
private $_messageQueue = array();
/**
* Path to the log file
* @var string
*/
private $_logFilePath = null;
private $_logFilePath = null;
/**
* Current minimum logging threshold
* @var integer
Expand All @@ -63,7 +77,7 @@ class KLogger
* This holds the file handle for this instance's log file
* @var resource
*/
private $_fileHandle = null;
private $_fileHandle = null;

/**
* Standard messages produced by the class. Can be modified for il8n
Expand Down Expand Up @@ -139,7 +153,7 @@ public function __construct($logDirectory, $severity)
{
$logDirectory = rtrim($logDirectory, '\\/');

if ($severity == self::OFF) {
if ($severity === self::OFF) {
return;
}

Expand Down Expand Up @@ -178,6 +192,16 @@ public function __destruct()
fclose($this->_fileHandle);
}
}
/**
* Writes a $line to the log with a severity level of DEBUG
*
* @param string $line Information to log
* @return void
*/
public function logDebug($line)
{
$this->log($line, self::DEBUG);
}

/**
* Returns (and removes) the last message from the queue.
Expand Down Expand Up @@ -207,7 +231,8 @@ public function clearMessages()
}

/**
* Writes a $line to the log with a severity level of INFO
* Writes a $line to the log with a severity level of INFO. Any information
* can be used here, or it could be used with E_STRICT errors
*
* @param string $line Information to log
* @return void
Expand All @@ -218,18 +243,21 @@ public function logInfo($line)
}

/**
* Writes a $line to the log with a severity level of DEBUG
* Writes a $line to the log with a severity level of NOTICE. Generally
* corresponds to E_STRICT, E_NOTICE, or E_USER_NOTICE errors
*
* @param string $line Information to log
* @return void
*/
public function logDebug($line)
public function logNotice($line)
{
$this->log($line, self::DEBUG);
$this->log($line, self::NOTICE);
}

/**
* Writes a $line to the log with a severity level of WARN
* Writes a $line to the log with a severity level of WARN. Generally
* corresponds to E_WARNING, E_USER_WARNING, E_CORE_WARNING, or
* E_COMPILE_WARNING
*
* @param string $line Information to log
* @return void
Expand All @@ -240,27 +268,63 @@ public function logWarn($line)
}

/**
* Writes a $line to the log with a severity level of ERROR
* Writes a $line to the log with a severity level of ERR. Most likely used
* with E_RECOVERABLE_ERROR
*
* @param string $line Information to log
* @return void
*/
public function logError($line)
{
$this->log($line, self::ERROR);
$this->log($line, self::ERR);
}

/**
* Writes a $line to the log with a severity level of FATAL
* Writes a $line to the log with a severity level of FATAL. Generally
* corresponds to E_ERROR, E_USER_ERROR, E_CORE_ERROR, or E_COMPILE_ERROR
*
* @param string $line Information to log
* @return void
* @deprecated Use logCrit
*/
public function logFatal($line)
{
$this->log($line, self::FATAL);
}

/**
* Writes a $line to the log with a severity level of ALERT.
*
* @param string $line Information to log
* @return void
*/
public function logAlert($line)
{
$this->log($line, self::ALERT);
}

/**
* Writes a $line to the log with a severity level of CRIT.
*
* @param string $line Information to log
* @return void
*/
public function logCrit($line)
{
$this->log($line, self::FATAL);
}

/**
* Writes a $line to the log with a severity level of EMERG.
*
* @param string $line Information to log
* @return void
*/
public function logEmerg($line)
{
$this->log($line, self::EMERG);
}

/**
* Writes a $line to the log with the given severity
*
Expand Down Expand Up @@ -296,13 +360,21 @@ private function _getTimeLine($level)
$time = date(self::$_dateFormat);

switch ($level) {
case self::EMERG:
return "$time - EMERG -->";
case self::ALERT:
return "$time - ALERT -->";
case self::CRIT:
return "$time - CRIT -->";
case self::NOTICE:
return "$time - NOTICE -->";
case self::INFO:
return "$time - INFO -->";
case self::WARN:
return "$time - WARN -->";
case self::DEBUG:
return "$time - DEBUG -->";
case self::ERROR:
case self::ERR:
return "$time - ERROR -->";
case self::FATAL:
return "$time - FATAL -->";
Expand Down

0 comments on commit 9ab47fa

Please sign in to comment.