Commit b82e28d5 authored by alexpott's avatar alexpott
Browse files

Issue #2267545 by ParisLiakos: Standardize to RFC 5424 log levels.

parent 49caf9de
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\DrupalKernel; use Drupal\Core\DrupalKernel;
use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Symfony\Component\ClassLoader\ApcClassLoader; use Symfony\Component\ClassLoader\ApcClassLoader;
...@@ -59,69 +60,6 @@ ...@@ -59,69 +60,6 @@
*/ */
const ERROR_REPORTING_DISPLAY_VERBOSE = 'verbose'; const ERROR_REPORTING_DISPLAY_VERBOSE = 'verbose';
/**
* @defgroup logging_severity_levels Logging severity levels
* @{
* Logging severity levels as defined in RFC 3164.
*
* The WATCHDOG_* constant definitions correspond to the logging severity levels
* defined in RFC 3164, section 4.1.1. PHP supplies predefined LOG_* constants
* for use in the syslog() function, but their values on Windows builds do not
* correspond to RFC 3164. The associated PHP bug report was closed with the
* comment, "And it's also not a bug, as Windows just have less log levels,"
* and "So the behavior you're seeing is perfectly normal."
*
* @see http://www.faqs.org/rfcs/rfc3164.html
* @see http://bugs.php.net/bug.php?id=18090
* @see http://php.net/manual/function.syslog.php
* @see http://php.net/manual/network.constants.php
* @see watchdog_severity_levels()
*/
/**
* Log message severity -- Emergency: system is unusable.
*/
const WATCHDOG_EMERGENCY = 0;
/**
* Log message severity -- Alert: action must be taken immediately.
*/
const WATCHDOG_ALERT = 1;
/**
* Log message severity -- Critical conditions.
*/
const WATCHDOG_CRITICAL = 2;
/**
* Log message severity -- Error conditions.
*/
const WATCHDOG_ERROR = 3;
/**
* Log message severity -- Warning conditions.
*/
const WATCHDOG_WARNING = 4;
/**
* Log message severity -- Normal but significant conditions.
*/
const WATCHDOG_NOTICE = 5;
/**
* Log message severity -- Informational messages.
*/
const WATCHDOG_INFO = 6;
/**
* Log message severity -- Debug-level messages.
*/
const WATCHDOG_DEBUG = 7;
/**
* @} End of "defgroup logging_severity_levels".
*/
/** /**
* First bootstrap phase: initialize configuration. * First bootstrap phase: initialize configuration.
* *
...@@ -783,7 +721,7 @@ function request_uri($omit_query_string = FALSE) { ...@@ -783,7 +721,7 @@ function request_uri($omit_query_string = FALSE) {
* *
* @see \Drupal\Core\Utility\Error::decodeException() * @see \Drupal\Core\Utility\Error::decodeException()
*/ */
function watchdog_exception($type, Exception $exception, $message = NULL, $variables = array(), $severity = WATCHDOG_ERROR, $link = NULL) { function watchdog_exception($type, Exception $exception, $message = NULL, $variables = array(), $severity = RfcLogLevel::ERROR, $link = NULL) {
// Use a default value if $message is not set. // Use a default value if $message is not set.
if (empty($message)) { if (empty($message)) {
......
...@@ -3340,30 +3340,6 @@ function element_set_attributes(array &$element, array $map) { ...@@ -3340,30 +3340,6 @@ function element_set_attributes(array &$element, array $map) {
Element::setAttributes($element, $map); Element::setAttributes($element, $map);
} }
/**
* Returns a list of severity levels, as defined in RFC 3164.
*
* @return
* Array of the possible severity levels for log messages.
*
* @see http://www.ietf.org/rfc/rfc3164.txt
* @ingroup logging_severity_levels
*
* @todo Move this in https://www.drupal.org/node/2267545 in \Drupal\Core\Log.
*/
function watchdog_severity_levels() {
return array(
WATCHDOG_EMERGENCY => t('Emergency'),
WATCHDOG_ALERT => t('Alert'),
WATCHDOG_CRITICAL => t('Critical'),
WATCHDOG_ERROR => t('Error'),
WATCHDOG_WARNING => t('Warning'),
WATCHDOG_NOTICE => t('Notice'),
WATCHDOG_INFO => t('Info'),
WATCHDOG_DEBUG => t('Debug'),
);
}
/** /**
* Flushes all persistent caches, resets all variables, and rebuilds all data structures. * Flushes all persistent caches, resets all variables, and rebuilds all data structures.
* *
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Page\DefaultHtmlPageRenderer; use Drupal\Core\Page\DefaultHtmlPageRenderer;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -21,21 +22,21 @@ ...@@ -21,21 +22,21 @@
*/ */
function drupal_error_levels() { function drupal_error_levels() {
$types = array( $types = array(
E_ERROR => array('Error', WATCHDOG_ERROR), E_ERROR => array('Error', RfcLogLevel::ERROR),
E_WARNING => array('Warning', WATCHDOG_WARNING), E_WARNING => array('Warning', RfcLogLevel::WARNING),
E_PARSE => array('Parse error', WATCHDOG_ERROR), E_PARSE => array('Parse error', RfcLogLevel::ERROR),
E_NOTICE => array('Notice', WATCHDOG_NOTICE), E_NOTICE => array('Notice', RfcLogLevel::NOTICE),
E_CORE_ERROR => array('Core error', WATCHDOG_ERROR), E_CORE_ERROR => array('Core error', RfcLogLevel::ERROR),
E_CORE_WARNING => array('Core warning', WATCHDOG_WARNING), E_CORE_WARNING => array('Core warning', RfcLogLevel::WARNING),
E_COMPILE_ERROR => array('Compile error', WATCHDOG_ERROR), E_COMPILE_ERROR => array('Compile error', RfcLogLevel::ERROR),
E_COMPILE_WARNING => array('Compile warning', WATCHDOG_WARNING), E_COMPILE_WARNING => array('Compile warning', RfcLogLevel::WARNING),
E_USER_ERROR => array('User error', WATCHDOG_ERROR), E_USER_ERROR => array('User error', RfcLogLevel::ERROR),
E_USER_WARNING => array('User warning', WATCHDOG_WARNING), E_USER_WARNING => array('User warning', RfcLogLevel::WARNING),
E_USER_NOTICE => array('User notice', WATCHDOG_NOTICE), E_USER_NOTICE => array('User notice', RfcLogLevel::NOTICE),
E_STRICT => array('Strict warning', WATCHDOG_DEBUG), E_STRICT => array('Strict warning', RfcLogLevel::DEBUG),
E_RECOVERABLE_ERROR => array('Recoverable fatal error', WATCHDOG_ERROR), E_RECOVERABLE_ERROR => array('Recoverable fatal error', RfcLogLevel::ERROR),
E_DEPRECATED => array('Deprecated function', WATCHDOG_DEBUG), E_DEPRECATED => array('Deprecated function', RfcLogLevel::DEBUG),
E_USER_DEPRECATED => array('User deprecated function', WATCHDOG_DEBUG), E_USER_DEPRECATED => array('User deprecated function', RfcLogLevel::DEBUG),
); );
return $types; return $types;
......
...@@ -27,21 +27,19 @@ class LoggerChannel implements LoggerChannelInterface { ...@@ -27,21 +27,19 @@ class LoggerChannel implements LoggerChannelInterface {
protected $channel; protected $channel;
/** /**
* Map of PSR Log constants to Watchdog log constants. * Map of PSR3 log constants to RFC 5424 log constants.
*
* @todo Move watchdog constants here in https://www.drupal.org/node/2267545.
* *
* @var array * @var array
*/ */
protected $levelTranslation = array( protected $levelTranslation = array(
LogLevel::EMERGENCY => WATCHDOG_EMERGENCY, LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY,
LogLevel::ALERT => WATCHDOG_ALERT, LogLevel::ALERT => RfcLogLevel::ALERT,
LogLevel::CRITICAL => WATCHDOG_CRITICAL, LogLevel::CRITICAL => RfcLogLevel::CRITICAL,
LogLevel::ERROR => WATCHDOG_ERROR, LogLevel::ERROR => RfcLogLevel::ERROR,
LogLevel::WARNING => WATCHDOG_WARNING, LogLevel::WARNING => RfcLogLevel::WARNING,
LogLevel::NOTICE => WATCHDOG_NOTICE, LogLevel::NOTICE => RfcLogLevel::NOTICE,
LogLevel::INFO => WATCHDOG_INFO, LogLevel::INFO => RfcLogLevel::INFO,
LogLevel::DEBUG => WATCHDOG_DEBUG, LogLevel::DEBUG => RfcLogLevel::DEBUG,
); );
/** /**
...@@ -102,7 +100,7 @@ public function log($level, $message, array $context = array()) { ...@@ -102,7 +100,7 @@ public function log($level, $message, array $context = array()) {
} }
if (is_string($level)) { if (is_string($level)) {
// Convert to integer equivalent for consistency with RFC 3164. // Convert to integer equivalent for consistency with RFC 5424.
$level = $this->levelTranslation[$level]; $level = $this->levelTranslation[$level];
} }
// Call all available loggers. // Call all available loggers.
......
<?php
/**
* @file
* Contains \Drupal\Core\Logger\RfcLogLevel.
*/
namespace Drupal\Core\Logger;
use Drupal\Core\StringTranslation\TranslationWrapper;
/**
* @defgroup logging_severity_levels Logging severity levels
* @{
* Logging severity levels as defined in RFC 5424.
*
* The constant definitions of this class correspond to the logging severity
* levels defined in RFC 5424, section 4.1.1. PHP supplies predefined LOG_*
* constants for use in the syslog() function, but their values on Windows
* builds do not correspond to RFC 5424. The associated PHP bug report was
* closed with the comment, "And it's also not a bug, as Windows just have less
* log levels," and "So the behavior you're seeing is perfectly normal."
*
* @see http://tools.ietf.org/html/rfc5424
* @see http://bugs.php.net/bug.php?id=18090
* @see http://php.net/manual/function.syslog.php
* @see http://php.net/manual/network.constants.php
* @see self::getLevels()
*
* @} End of "defgroup logging_severity_levels".
*/
class RfcLogLevel {
/**
* Log message severity -- Emergency: system is unusable.
*/
const EMERGENCY = 0;
/**
* Log message severity -- Alert: action must be taken immediately.
*/
const ALERT = 1;
/**
* Log message severity -- Critical conditions.
*/
const CRITICAL = 2;
/**
* Log message severity -- Error conditions.
*/
const ERROR = 3;
/**
* Log message severity -- Warning conditions.
*/
const WARNING = 4;
/**
* Log message severity -- Normal but significant conditions.
*/
const NOTICE = 5;
/**
* Log message severity -- Informational messages.
*/
const INFO = 6;
/**
* Log message severity -- Debug-level messages.
*/
const DEBUG = 7;
/**
* An array with the severity levels as keys and labels as values.
*
* @var array
*/
protected static $levels;
/**
* Returns a list of severity levels, as defined in RFC 5424.
*
* @return array
* Array of the possible severity levels for log messages.
*
* @see http://tools.ietf.org/html/rfc5424
* @ingroup logging_severity_levels
*/
public static function getLevels() {
if (!static::$levels) {
static::$levels = [
static::EMERGENCY => new TranslationWrapper('Emergency'),
static::ALERT => new TranslationWrapper('Alert'),
static::CRITICAL => new TranslationWrapper('Critical'),
static::ERROR => new TranslationWrapper('Error'),
static::WARNING => new TranslationWrapper('Warning'),
static::NOTICE => new TranslationWrapper('Notice'),
static::INFO => new TranslationWrapper('Info'),
static::DEBUG => new TranslationWrapper('Debug'),
];
}
return static::$levels;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Logger\RfcLoggerTrait.
*/
namespace Drupal\Core\Logger;
/**
* A copy of \Psr\Log\LoggerTrait that uses RFC 5424 compliant log levels.
*
* Internal Drupal logger implementations should use this trait instead of
* \Psr\Log\LoggerTrait. Callers of those implementations are responsible for
* translating any other log level format to RFC 5424 compliant integers.
*
* @see https://groups.google.com/forum/#!topic/php-fig/Rc5YDhNdGz4
* @see https://www.drupal.org/node/2267545
*/
trait RfcLoggerTrait {
/**
* Implements \Psr\Log\LoggerInterface::emergency()
*/
public function emergency($message, array $context = array()) {
$this->log(RfcLogLevel::EMERGENCY, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::alert()
*/
public function alert($message, array $context = array()) {
$this->log(RfcLogLevel::ALERT, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::critical()
*/
public function critical($message, array $context = array()) {
$this->log(RfcLogLevel::CRITICAL, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::error()
*/
public function error($message, array $context = array()) {
$this->log(RfcLogLevel::ERROR, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::warning()
*/
public function warning($message, array $context = array()) {
$this->log(RfcLogLevel::WARNING, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::notice()
*/
public function notice($message, array $context = array()) {
$this->log(RfcLogLevel::NOTICE, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::info()
*/
public function info($message, array $context = array()) {
$this->log(RfcLogLevel::INFO, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::debug()
*/
public function debug($message, array $context = array()) {
$this->log(RfcLogLevel::DEBUG, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::log()
*/
abstract public function log($level, $message, array $context = array());
}
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
* Administrative page callbacks for the Database Logging module. * Administrative page callbacks for the Database Logging module.
*/ */
use Drupal\Core\Logger\RfcLogLevel;
/** /**
* Creates a list of database log administration filters that can be applied. * Creates a list of database log administration filters that can be applied.
* *
...@@ -34,7 +36,7 @@ function dblog_filters() { ...@@ -34,7 +36,7 @@ function dblog_filters() {
$filters['severity'] = array( $filters['severity'] = array(
'title' => t('Severity'), 'title' => t('Severity'),
'where' => 'w.severity = ?', 'where' => 'w.severity = ?',
'options' => watchdog_severity_levels(), 'options' => RfcLogLevel::getLevels(),
); );
return $filters; return $filters;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url; use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -90,14 +91,14 @@ public function __construct(Connection $database, ModuleHandlerInterface $module ...@@ -90,14 +91,14 @@ public function __construct(Connection $database, ModuleHandlerInterface $module
*/ */
public static function getLogLevelClassMap() { public static function getLogLevelClassMap() {
return array( return array(
WATCHDOG_DEBUG => 'dblog-debug', RfcLogLevel::DEBUG => 'dblog-debug',
WATCHDOG_INFO => 'dblog-info', RfcLogLevel::INFO => 'dblog-info',
WATCHDOG_NOTICE => 'dblog-notice', RfcLogLevel::NOTICE => 'dblog-notice',
WATCHDOG_WARNING => 'dblog-warning', RfcLogLevel::WARNING => 'dblog-warning',
WATCHDOG_ERROR => 'dblog-error', RfcLogLevel::ERROR => 'dblog-error',
WATCHDOG_CRITICAL => 'dblog-critical', RfcLogLevel::CRITICAL => 'dblog-critical',
WATCHDOG_ALERT => 'dblog-alert', RfcLogLevel::ALERT => 'dblog-alert',
WATCHDOG_EMERGENCY => 'dblog-emergency', RfcLogLevel::EMERGENCY => 'dblog-emergency',
); );
} }
...@@ -232,7 +233,7 @@ public function overview() { ...@@ -232,7 +233,7 @@ public function overview() {
public function eventDetails($event_id) { public function eventDetails($event_id) {
$build = array(); $build = array();
if ($dblog = $this->database->query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users_field_data} u ON w.uid = u.uid WHERE w.wid = :id AND u.default_langcode = 1', array(':id' => $event_id))->fetchObject()) { if ($dblog = $this->database->query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users_field_data} u ON w.uid = u.uid WHERE w.wid = :id AND u.default_langcode = 1', array(':id' => $event_id))->fetchObject()) {
$severity = watchdog_severity_levels(); $severity = RfcLogLevel::getLevels();
$message = $this->formatMessage($dblog); $message = $this->formatMessage($dblog);
$username = array( $username = array(
'#theme' => 'username', '#theme' => 'username',
......
...@@ -9,14 +9,14 @@ ...@@ -9,14 +9,14 @@
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LogMessageParserInterface; use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
/** /**
* Logs events in the watchdog database table. * Logs events in the watchdog database table.
*/ */
class DbLog implements LoggerInterface { class DbLog implements LoggerInterface {
use LoggerTrait; use RfcLoggerTrait;
/** /**
* The database connection object. * The database connection object.
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\dblog\Tests; namespace Drupal\dblog\Tests;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\dblog\Controller\DbLogController; use Drupal\dblog\Controller\DbLogController;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
...@@ -126,9 +127,10 @@ private function verifyCron($row_limit) { ...@@ -126,9 +127,10 @@ private function verifyCron($row_limit) {
* @param string $type * @param string $type
* (optional) The type of watchdog entry. Defaults to 'custom'. * (optional) The type of watchdog entry. Defaults to 'custom'.
* @param int $severity * @param int $severity
* (optional) The severity of the watchdog entry. Defaults to WATCHDOG_NOTICE. * (optional) The severity of the watchdog entry. Defaults to
* \Drupal\Core\Logger\RfcLogLevel::NOTICE.
*/ */
private function generateLogEntries($count, $type = 'custom', $severity = WATCHDOG_NOTICE) { private function generateLogEntries($count, $type = 'custom', $severity = RfcLogLevel::NOTICE) {
global $base_root; global $base_root;
// Prepare the fields to be logged // Prepare the fields to be logged
...@@ -445,7 +447,7 @@ protected function testDBLogAddAndClear() { ...@@ -445,7 +447,7 @@ protected function testDBLogAddAndClear() {
'channel' => 'system', 'channel' => 'system',
'message' => 'Log entry added to test the doClearTest clear down.', 'message' => 'Log entry added to test the doClearTest clear down.',
'variables' => array(), 'variables' => array(),
'severity' => WATCHDOG_NOTICE, 'severity' => RfcLogLevel::NOTICE,
'link' => NULL, 'link' => NULL,
'user' => $this->big_user, 'user' => $this->big_user,
'uid' => $this->big_user->id(), 'uid' => $this->big_user->id(),
...@@ -483,7 +485,7 @@ protected function testFilter() { ...@@ -483,7 +485,7 @@ protected function testFilter() {
$types = array(); $types = array();
for ($i = 0; $i < 3; $i++) { for ($i = 0; $i < 3; $i++) {
$type_names[] = $type_name = $this->randomMachineName(); $type_names[] = $type_name = $this->randomMachineName();
$severity = WATCHDOG_EMERGENCY; $severity = RfcLogLevel::EMERGENCY;
for ($j = 0; $j < 3; $j++) { for ($j = 0; $j < 3; $j++) {
$types[] = $type = array( $types[] = $type = array(
'count' => $j + 1, 'count' => $j + 1,
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\views\Views; use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;