diff --git a/README.mdown b/README.mdown index dd87e66b..8105e6a1 100644 --- a/README.mdown +++ b/README.mdown @@ -47,22 +47,25 @@ will be created if you don't set one. The formatters normalize and format incoming records so that they can be used by the handlers to output useful information. -Custom severity levels are not available. Only six levels (debug, info, -warning, error, critical, alert) are present for basic filtering purposes, but -for sorting and other use cases that would require flexibility, you should add -Processors to the Logger that can add extra information (tags, user ip, ..) to -the records before they are handled. +Custom severity levels are not available. Only the eight RFC 5424 levels (debug, +info, notice, warning, error, critical, alert, emergency) are present for basic +filtering purposes, but for sorting and other use cases that would require +flexibility, you should add Processors to the Logger that can add extra +information (tags, user ip, ..) to the records before they are handled. Log Levels ---------- -Monolog exposes 6 log levels. Although it is possible to add more by extending -the classes you need, these are generally enough. +Monolog supports all 8 logging levels defined in RFC5424, but unless you +specifically need syslog compatibility, it is advised to only use DEBUG, INFO, +WARNING, ERROR, CRITICAL, ALERT. - **DEBUG** (100): Detailed debug information. - **INFO** (200): Interesting events. Examples: User logs in, SQL logs. +- **NOTICE** (250): Normal but significant events. + - **WARNING** (300): Exceptional occurrences that are not errors. Examples: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. @@ -77,6 +80,8 @@ the classes you need, these are generally enough. down, database unavailable, etc. This should trigger the SMS alerts and wake you up. +- **EMERGENCY** (600): Emergency: system is unusable. + Docs ==== diff --git a/src/Monolog/Handler/SyslogHandler.php b/src/Monolog/Handler/SyslogHandler.php index 2ebb264f..ed2fe441 100644 --- a/src/Monolog/Handler/SyslogHandler.php +++ b/src/Monolog/Handler/SyslogHandler.php @@ -32,12 +32,14 @@ class SyslogHandler extends AbstractProcessingHandler * Translates Monolog log levels to syslog log priorities. */ private $logLevels = array( - Logger::DEBUG => LOG_DEBUG, - Logger::INFO => LOG_INFO, - Logger::WARNING => LOG_WARNING, - Logger::ERROR => LOG_ERR, - Logger::CRITICAL => LOG_CRIT, - Logger::ALERT => LOG_ALERT, + Logger::DEBUG => LOG_DEBUG, + Logger::INFO => LOG_INFO, + Logger::NOTICE => LOG_NOTICE, + Logger::WARNING => LOG_WARNING, + Logger::ERROR => LOG_ERR, + Logger::CRITICAL => LOG_CRIT, + Logger::ALERT => LOG_ALERT, + Logger::EMERGENCY => LOG_EMERG, ); /** diff --git a/src/Monolog/Handler/TestHandler.php b/src/Monolog/Handler/TestHandler.php index 434c30dc..085d9e17 100644 --- a/src/Monolog/Handler/TestHandler.php +++ b/src/Monolog/Handler/TestHandler.php @@ -30,6 +30,11 @@ class TestHandler extends AbstractProcessingHandler return $this->records; } + public function hasEmergency($record) + { + return $this->hasRecord($record, Logger::EMERGENCY); + } + public function hasAlert($record) { return $this->hasRecord($record, Logger::ALERT); @@ -50,6 +55,11 @@ class TestHandler extends AbstractProcessingHandler return $this->hasRecord($record, Logger::WARNING); } + public function hasNotice($record) + { + return $this->hasRecord($record, Logger::NOTICE); + } + public function hasInfo($record) { return $this->hasRecord($record, Logger::INFO); @@ -60,6 +70,11 @@ class TestHandler extends AbstractProcessingHandler return $this->hasRecord($record, Logger::DEBUG); } + public function hasEmergencyRecords() + { + return isset($this->recordsByLevel[Logger::EMERGENCY]); + } + public function hasAlertRecords() { return isset($this->recordsByLevel[Logger::ALERT]); @@ -80,6 +95,11 @@ class TestHandler extends AbstractProcessingHandler return isset($this->recordsByLevel[Logger::WARNING]); } + public function hasNoticeRecords() + { + return isset($this->recordsByLevel[Logger::NOTICE]); + } + public function hasInfoRecords() { return isset($this->recordsByLevel[Logger::INFO]); diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 14cff644..96518493 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -36,6 +36,11 @@ class Logger */ const INFO = 200; + /** + * Uncommon events + */ + const NOTICE = 250; + /** * Exceptional occurrences that are not errors * @@ -64,13 +69,20 @@ class Logger */ const ALERT = 550; + /** + * Urgent alert. + */ + const EMERGENCY = 600; + protected static $levels = array( 100 => 'DEBUG', 200 => 'INFO', + 250 => 'NOTICE', 300 => 'WARNING', 400 => 'ERROR', 500 => 'CRITICAL', 550 => 'ALERT', + 600 => 'EMERGENCY', ); protected $name; @@ -221,6 +233,18 @@ class Logger return $this->addRecord(self::INFO, $message, $context); } + /** + * Adds a log record at the NOTICE level. + * + * @param string $message The log message + * @param array $context The log context + * @return Boolean Whether the record has been processed + */ + public function addNotice($message, array $context = array()) + { + return $this->addRecord(self::NOTICE, $message, $context); + } + /** * Adds a log record at the WARNING level. * @@ -269,6 +293,19 @@ class Logger return $this->addRecord(self::ALERT, $message, $context); } + /** + * Adds a log record at the EMERGENCY level. + * + * @param string $message The log message + * @param array $context The log context + * @return Boolean Whether the record has been processed + */ + public function addEmergency($message, array $context = array()) + { + return $this->addRecord(self::EMERGENCY, $message, $context); + } + + /** * Gets the name of the logging level. * @@ -312,7 +349,7 @@ class Logger /** * Adds a log record at the DEBUG level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -326,7 +363,7 @@ class Logger /** * Adds a log record at the INFO level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -340,7 +377,7 @@ class Logger /** * Adds a log record at the INFO level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -348,13 +385,13 @@ class Logger */ public function notice($message, array $context = array()) { - return $this->addRecord(self::INFO, $message, $context); + return $this->addRecord(self::NOTICE, $message, $context); } /** * Adds a log record at the WARNING level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -368,7 +405,7 @@ class Logger /** * Adds a log record at the ERROR level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -382,7 +419,7 @@ class Logger /** * Adds a log record at the CRITICAL level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -396,7 +433,7 @@ class Logger /** * Adds a log record at the ALERT level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -408,9 +445,9 @@ class Logger } /** - * Adds a log record at the ALERT level. + * Adds a log record at the EMERGENCY level. * - * This method allows to have an easy ZF compatibility. + * This method allows to have an easy ZF/Symfony 2 compatibility. * * @param string $message The log message * @param array $context The log context @@ -418,6 +455,6 @@ class Logger */ public function emerg($message, array $context = array()) { - return $this->addRecord(self::ALERT, $message, $context); + return $this->addRecord(self::EMERGENCY, $message, $context); } } diff --git a/tests/Monolog/Handler/TestHandlerTest.php b/tests/Monolog/Handler/TestHandlerTest.php index 1addf7e6..801d80a9 100644 --- a/tests/Monolog/Handler/TestHandlerTest.php +++ b/tests/Monolog/Handler/TestHandlerTest.php @@ -43,12 +43,14 @@ class TestHandlerTest extends TestCase public function methodProvider() { return array( - array('Alert' , Logger::ALERT), - array('Critical', Logger::CRITICAL), - array('Error' , Logger::ERROR), - array('Warning' , Logger::WARNING), - array('Info' , Logger::INFO), - array('Debug' , Logger::DEBUG), + array('Emergency', Logger::EMERGENCY), + array('Alert' , Logger::ALERT), + array('Critical' , Logger::CRITICAL), + array('Error' , Logger::ERROR), + array('Warning' , Logger::WARNING), + array('Info' , Logger::INFO), + array('Notice' , Logger::NOTICE), + array('Debug' , Logger::DEBUG), ); } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index 6a089918..ece04bd5 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -320,10 +320,12 @@ class LoggerTest extends \PHPUnit_Framework_TestCase * @dataProvider logMethodProvider * @covers Monolog\Logger::addDebug * @covers Monolog\Logger::addInfo + * @covers Monolog\Logger::addNotice * @covers Monolog\Logger::addWarning * @covers Monolog\Logger::addError * @covers Monolog\Logger::addCritical * @covers Monolog\Logger::addAlert + * @covers Monolog\Logger::addEmergency * @covers Monolog\Logger::debug * @covers Monolog\Logger::info * @covers Monolog\Logger::notice @@ -347,22 +349,24 @@ class LoggerTest extends \PHPUnit_Framework_TestCase { return array( // monolog methods - array('addDebug', Logger::DEBUG), - array('addInfo', Logger::INFO), - array('addWarning', Logger::WARNING), - array('addError', Logger::ERROR), - array('addCritical', Logger::CRITICAL), - array('addAlert', Logger::ALERT), + array('addDebug', Logger::DEBUG), + array('addInfo', Logger::INFO), + array('addNotice', Logger::NOTICE), + array('addWarning', Logger::WARNING), + array('addError', Logger::ERROR), + array('addCritical', Logger::CRITICAL), + array('addAlert', Logger::ALERT), + array('addEmergency', Logger::EMERGENCY), // ZF/Sf2 compat methods array('debug', Logger::DEBUG), array('info', Logger::INFO), - array('notice', Logger::INFO), + array('notice', Logger::NOTICE), array('warn', Logger::WARNING), array('err', Logger::ERROR), array('crit', Logger::CRITICAL), array('alert', Logger::ALERT), - array('emerg', Logger::ALERT), + array('emerg', Logger::EMERGENCY), ); } }