1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-02 19:27:37 +02:00

Implement the 8 RFC3164 severity levels instead of just 6.

- constants defined for the 2 missing levels: NOTICE and EMERGENCY.
- add<level>() and <level>() convenience methods added.
- TestHandler and tests updated to account for the two extra levels.
- surjective mappings from the RFC3164 to only 6 levels changes to bijective.
- README updated accordingly.
This commit is contained in:
Frederic G. MARAND
2012-05-28 20:29:27 +02:00
parent 2eb0c0978d
commit 07aac12c72
6 changed files with 107 additions and 38 deletions

View File

@@ -47,22 +47,24 @@ 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 incoming records so that they can be used by the handlers to output useful
information. information.
Custom severity levels are not available. Only six levels (debug, info, Custom severity levels are not available. Only the eight RFC 3164 levels (debug,
warning, error, critical, alert) are present for basic filtering purposes, but info, notice, warning, error, critical, alert, emergency) are present for basic
for sorting and other use cases that would require flexibility, you should add filtering purposes, but for sorting and other use cases that would require
Processors to the Logger that can add extra information (tags, user ip, ..) to flexibility, you should add Processors to the Logger that can add extra
the records before they are handled. information (tags, user ip, ..) to the records before they are handled.
Log Levels Log Levels
---------- ----------
Monolog exposes 6 log levels. Although it is possible to add more by extending Monolog exposes the 8 standard RFC3164 log levels. Although it is possible to
the classes you need, these are generally enough. add more by extending the classes you need, these are generally enough.
- **DEBUG** (100): Detailed debug information. - **DEBUG** (100): Detailed debug information.
- **INFO** (200): Interesting events. Examples: User logs in, SQL logs. - **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: - **WARNING** (300): Exceptional occurrences that are not errors. Examples:
Use of deprecated APIs, poor use of an API, undesirable things that are not Use of deprecated APIs, poor use of an API, undesirable things that are not
necessarily wrong. necessarily wrong.
@@ -77,6 +79,8 @@ the classes you need, these are generally enough.
down, database unavailable, etc. This should trigger the SMS alerts and wake down, database unavailable, etc. This should trigger the SMS alerts and wake
you up. you up.
- **EMERGENCY** (600): Emergency: system is unusable.
Docs Docs
==== ====

View File

@@ -32,12 +32,14 @@ class SyslogHandler extends AbstractProcessingHandler
* Translates Monolog log levels to syslog log priorities. * Translates Monolog log levels to syslog log priorities.
*/ */
private $logLevels = array( private $logLevels = array(
Logger::DEBUG => LOG_DEBUG, Logger::DEBUG => LOG_DEBUG,
Logger::INFO => LOG_INFO, Logger::INFO => LOG_INFO,
Logger::WARNING => LOG_WARNING, Logger::NOTICE => LOG_NOTICE,
Logger::ERROR => LOG_ERR, Logger::WARNING => LOG_WARNING,
Logger::CRITICAL => LOG_CRIT, Logger::ERROR => LOG_ERR,
Logger::ALERT => LOG_ALERT, Logger::CRITICAL => LOG_CRIT,
Logger::ALERT => LOG_ALERT,
Logger::EMERGENCY => LOG_EMERG,
); );
/** /**

View File

@@ -30,6 +30,11 @@ class TestHandler extends AbstractProcessingHandler
return $this->records; return $this->records;
} }
public function hasEmergency($record)
{
return $this->hasRecord($record, Logger::EMERGENCY);
}
public function hasAlert($record) public function hasAlert($record)
{ {
return $this->hasRecord($record, Logger::ALERT); return $this->hasRecord($record, Logger::ALERT);
@@ -50,6 +55,11 @@ class TestHandler extends AbstractProcessingHandler
return $this->hasRecord($record, Logger::WARNING); return $this->hasRecord($record, Logger::WARNING);
} }
public function hasNotice($record)
{
return $this->hasRecord($record, Logger::NOTICE);
}
public function hasInfo($record) public function hasInfo($record)
{ {
return $this->hasRecord($record, Logger::INFO); return $this->hasRecord($record, Logger::INFO);
@@ -60,6 +70,11 @@ class TestHandler extends AbstractProcessingHandler
return $this->hasRecord($record, Logger::DEBUG); return $this->hasRecord($record, Logger::DEBUG);
} }
public function hasEmergencyRecords()
{
return isset($this->recordsByLevel[Logger::EMERGENCY]);
}
public function hasAlertRecords() public function hasAlertRecords()
{ {
return isset($this->recordsByLevel[Logger::ALERT]); return isset($this->recordsByLevel[Logger::ALERT]);
@@ -80,6 +95,11 @@ class TestHandler extends AbstractProcessingHandler
return isset($this->recordsByLevel[Logger::WARNING]); return isset($this->recordsByLevel[Logger::WARNING]);
} }
public function hasNoticeRecords()
{
return isset($this->recordsByLevel[Logger::NOTICE]);
}
public function hasInfoRecords() public function hasInfoRecords()
{ {
return isset($this->recordsByLevel[Logger::INFO]); return isset($this->recordsByLevel[Logger::INFO]);

View File

@@ -36,6 +36,11 @@ class Logger
*/ */
const INFO = 200; const INFO = 200;
/**
* Uncommon events
*/
const NOTICE = 250;
/** /**
* Exceptional occurrences that are not errors * Exceptional occurrences that are not errors
* *
@@ -64,13 +69,20 @@ class Logger
*/ */
const ALERT = 550; const ALERT = 550;
/**
* Urgent alert.
*/
const EMERGENCY = 600;
protected static $levels = array( protected static $levels = array(
100 => 'DEBUG', 100 => 'DEBUG',
200 => 'INFO', 200 => 'INFO',
250 => 'NOTICE',
300 => 'WARNING', 300 => 'WARNING',
400 => 'ERROR', 400 => 'ERROR',
500 => 'CRITICAL', 500 => 'CRITICAL',
550 => 'ALERT', 550 => 'ALERT',
600 => 'EMERGENCY',
); );
protected $name; protected $name;
@@ -219,6 +231,18 @@ class Logger
return $this->addRecord(self::INFO, $message, $context); 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. * Adds a log record at the WARNING level.
* *
@@ -267,6 +291,19 @@ class Logger
return $this->addRecord(self::ALERT, $message, $context); 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. * Gets the name of the logging level.
* *
@@ -310,7 +347,7 @@ class Logger
/** /**
* Adds a log record at the DEBUG level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -324,7 +361,7 @@ class Logger
/** /**
* Adds a log record at the INFO level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -338,7 +375,7 @@ class Logger
/** /**
* Adds a log record at the INFO level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -346,13 +383,13 @@ class Logger
*/ */
public function notice($message, array $context = array()) 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. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -366,7 +403,7 @@ class Logger
/** /**
* Adds a log record at the ERROR level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -380,7 +417,7 @@ class Logger
/** /**
* Adds a log record at the CRITICAL level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -394,7 +431,7 @@ class Logger
/** /**
* Adds a log record at the ALERT level. * 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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -406,9 +443,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 string $message The log message
* @param array $context The log context * @param array $context The log context
@@ -416,6 +453,6 @@ class Logger
*/ */
public function emerg($message, array $context = array()) public function emerg($message, array $context = array())
{ {
return $this->addRecord(self::ALERT, $message, $context); return $this->addRecord(self::EMERGENCY, $message, $context);
} }
} }

View File

@@ -43,12 +43,14 @@ class TestHandlerTest extends TestCase
public function methodProvider() public function methodProvider()
{ {
return array( return array(
array('Alert' , Logger::ALERT), array('Emergency', Logger::EMERGENCY),
array('Critical', Logger::CRITICAL), array('Alert' , Logger::ALERT),
array('Error' , Logger::ERROR), array('Critical' , Logger::CRITICAL),
array('Warning' , Logger::WARNING), array('Error' , Logger::ERROR),
array('Info' , Logger::INFO), array('Warning' , Logger::WARNING),
array('Debug' , Logger::DEBUG), array('Info' , Logger::INFO),
array('Notice' , Logger::NOTICE),
array('Debug' , Logger::DEBUG),
); );
} }
} }

View File

@@ -319,10 +319,12 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
* @dataProvider logMethodProvider * @dataProvider logMethodProvider
* @covers Monolog\Logger::addDebug * @covers Monolog\Logger::addDebug
* @covers Monolog\Logger::addInfo * @covers Monolog\Logger::addInfo
* @covers Monolog\Logger::addNotice
* @covers Monolog\Logger::addWarning * @covers Monolog\Logger::addWarning
* @covers Monolog\Logger::addError * @covers Monolog\Logger::addError
* @covers Monolog\Logger::addCritical * @covers Monolog\Logger::addCritical
* @covers Monolog\Logger::addAlert * @covers Monolog\Logger::addAlert
* @covers Monolog\Logger::addEmergency
* @covers Monolog\Logger::debug * @covers Monolog\Logger::debug
* @covers Monolog\Logger::info * @covers Monolog\Logger::info
* @covers Monolog\Logger::notice * @covers Monolog\Logger::notice
@@ -346,22 +348,24 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
{ {
return array( return array(
// monolog methods // monolog methods
array('addDebug', Logger::DEBUG), array('addDebug', Logger::DEBUG),
array('addInfo', Logger::INFO), array('addInfo', Logger::INFO),
array('addWarning', Logger::WARNING), array('addNotice', Logger::NOTICE),
array('addError', Logger::ERROR), array('addWarning', Logger::WARNING),
array('addCritical', Logger::CRITICAL), array('addError', Logger::ERROR),
array('addAlert', Logger::ALERT), array('addCritical', Logger::CRITICAL),
array('addAlert', Logger::ALERT),
array('addEmergency', Logger::EMERGENCY),
// ZF/Sf2 compat methods // ZF/Sf2 compat methods
array('debug', Logger::DEBUG), array('debug', Logger::DEBUG),
array('info', Logger::INFO), array('info', Logger::INFO),
array('notice', Logger::INFO), array('notice', Logger::NOTICE),
array('warn', Logger::WARNING), array('warn', Logger::WARNING),
array('err', Logger::ERROR), array('err', Logger::ERROR),
array('crit', Logger::CRITICAL), array('crit', Logger::CRITICAL),
array('alert', Logger::ALERT), array('alert', Logger::ALERT),
array('emerg', Logger::ALERT), array('emerg', Logger::EMERGENCY),
); );
} }
} }