Commit b82e28d5 authored by alexpott's avatar alexpott

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

parent 49caf9de
......@@ -12,6 +12,7 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Error;
use Symfony\Component\ClassLoader\ApcClassLoader;
......@@ -59,69 +60,6 @@
*/
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.
*
......@@ -783,7 +721,7 @@ function request_uri($omit_query_string = FALSE) {
*
* @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.
if (empty($message)) {
......
......@@ -3340,30 +3340,6 @@ function element_set_attributes(array &$element, array $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.
*
......
......@@ -7,6 +7,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Page\DefaultHtmlPageRenderer;
use Drupal\Core\Utility\Error;
use Symfony\Component\HttpFoundation\Response;
......@@ -21,21 +22,21 @@
*/
function drupal_error_levels() {
$types = array(
E_ERROR => array('Error', WATCHDOG_ERROR),
E_WARNING => array('Warning', WATCHDOG_WARNING),
E_PARSE => array('Parse error', WATCHDOG_ERROR),
E_NOTICE => array('Notice', WATCHDOG_NOTICE),
E_CORE_ERROR => array('Core error', WATCHDOG_ERROR),
E_CORE_WARNING => array('Core warning', WATCHDOG_WARNING),
E_COMPILE_ERROR => array('Compile error', WATCHDOG_ERROR),
E_COMPILE_WARNING => array('Compile warning', WATCHDOG_WARNING),
E_USER_ERROR => array('User error', WATCHDOG_ERROR),
E_USER_WARNING => array('User warning', WATCHDOG_WARNING),
E_USER_NOTICE => array('User notice', WATCHDOG_NOTICE),
E_STRICT => array('Strict warning', WATCHDOG_DEBUG),
E_RECOVERABLE_ERROR => array('Recoverable fatal error', WATCHDOG_ERROR),
E_DEPRECATED => array('Deprecated function', WATCHDOG_DEBUG),
E_USER_DEPRECATED => array('User deprecated function', WATCHDOG_DEBUG),
E_ERROR => array('Error', RfcLogLevel::ERROR),
E_WARNING => array('Warning', RfcLogLevel::WARNING),
E_PARSE => array('Parse error', RfcLogLevel::ERROR),
E_NOTICE => array('Notice', RfcLogLevel::NOTICE),
E_CORE_ERROR => array('Core error', RfcLogLevel::ERROR),
E_CORE_WARNING => array('Core warning', RfcLogLevel::WARNING),
E_COMPILE_ERROR => array('Compile error', RfcLogLevel::ERROR),
E_COMPILE_WARNING => array('Compile warning', RfcLogLevel::WARNING),
E_USER_ERROR => array('User error', RfcLogLevel::ERROR),
E_USER_WARNING => array('User warning', RfcLogLevel::WARNING),
E_USER_NOTICE => array('User notice', RfcLogLevel::NOTICE),
E_STRICT => array('Strict warning', RfcLogLevel::DEBUG),
E_RECOVERABLE_ERROR => array('Recoverable fatal error', RfcLogLevel::ERROR),
E_DEPRECATED => array('Deprecated function', RfcLogLevel::DEBUG),
E_USER_DEPRECATED => array('User deprecated function', RfcLogLevel::DEBUG),
);
return $types;
......
......@@ -27,21 +27,19 @@ class LoggerChannel implements LoggerChannelInterface {
protected $channel;
/**
* Map of PSR Log constants to Watchdog log constants.
*
* @todo Move watchdog constants here in https://www.drupal.org/node/2267545.
* Map of PSR3 log constants to RFC 5424 log constants.
*
* @var array
*/
protected $levelTranslation = array(
LogLevel::EMERGENCY => WATCHDOG_EMERGENCY,
LogLevel::ALERT => WATCHDOG_ALERT,
LogLevel::CRITICAL => WATCHDOG_CRITICAL,
LogLevel::ERROR => WATCHDOG_ERROR,
LogLevel::WARNING => WATCHDOG_WARNING,
LogLevel::NOTICE => WATCHDOG_NOTICE,
LogLevel::INFO => WATCHDOG_INFO,
LogLevel::DEBUG => WATCHDOG_DEBUG,
LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY,
LogLevel::ALERT => RfcLogLevel::ALERT,
LogLevel::CRITICAL => RfcLogLevel::CRITICAL,
LogLevel::ERROR => RfcLogLevel::ERROR,
LogLevel::WARNING => RfcLogLevel::WARNING,
LogLevel::NOTICE => RfcLogLevel::NOTICE,
LogLevel::INFO => RfcLogLevel::INFO,
LogLevel::DEBUG => RfcLogLevel::DEBUG,
);
/**
......@@ -102,7 +100,7 @@ public function log($level, $message, array $context = array()) {
}
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];
}
// 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 @@
* 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.
*
......@@ -34,7 +36,7 @@ function dblog_filters() {
$filters['severity'] = array(
'title' => t('Severity'),
'where' => 'w.severity = ?',
'options' => watchdog_severity_levels(),
'options' => RfcLogLevel::getLevels(),
);
return $filters;
......
......@@ -15,6 +15,7 @@
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -90,14 +91,14 @@ public function __construct(Connection $database, ModuleHandlerInterface $module
*/
public static function getLogLevelClassMap() {
return array(
WATCHDOG_DEBUG => 'dblog-debug',
WATCHDOG_INFO => 'dblog-info',
WATCHDOG_NOTICE => 'dblog-notice',
WATCHDOG_WARNING => 'dblog-warning',
WATCHDOG_ERROR => 'dblog-error',
WATCHDOG_CRITICAL => 'dblog-critical',
WATCHDOG_ALERT => 'dblog-alert',
WATCHDOG_EMERGENCY => 'dblog-emergency',
RfcLogLevel::DEBUG => 'dblog-debug',
RfcLogLevel::INFO => 'dblog-info',
RfcLogLevel::NOTICE => 'dblog-notice',
RfcLogLevel::WARNING => 'dblog-warning',
RfcLogLevel::ERROR => 'dblog-error',
RfcLogLevel::CRITICAL => 'dblog-critical',
RfcLogLevel::ALERT => 'dblog-alert',
RfcLogLevel::EMERGENCY => 'dblog-emergency',
);
}
......@@ -232,7 +233,7 @@ public function overview() {
public function eventDetails($event_id) {
$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()) {
$severity = watchdog_severity_levels();
$severity = RfcLogLevel::getLevels();
$message = $this->formatMessage($dblog);
$username = array(
'#theme' => 'username',
......
......@@ -9,14 +9,14 @@
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
/**
* Logs events in the watchdog database table.
*/
class DbLog implements LoggerInterface {
use LoggerTrait;
use RfcLoggerTrait;
/**
* The database connection object.
......
......@@ -8,6 +8,7 @@
namespace Drupal\dblog\Tests;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\dblog\Controller\DbLogController;
use Drupal\simpletest\WebTestBase;
......@@ -126,9 +127,10 @@ private function verifyCron($row_limit) {
* @param string $type
* (optional) The type of watchdog entry. Defaults to 'custom'.
* @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;
// Prepare the fields to be logged
......@@ -445,7 +447,7 @@ protected function testDBLogAddAndClear() {
'channel' => 'system',
'message' => 'Log entry added to test the doClearTest clear down.',
'variables' => array(),
'severity' => WATCHDOG_NOTICE,
'severity' => RfcLogLevel::NOTICE,
'link' => NULL,
'user' => $this->big_user,
'uid' => $this->big_user->id(),
......@@ -483,7 +485,7 @@ protected function testFilter() {
$types = array();
for ($i = 0; $i < 3; $i++) {
$type_names[] = $type_name = $this->randomMachineName();
$severity = WATCHDOG_EMERGENCY;
$severity = RfcLogLevel::EMERGENCY;
for ($j = 0; $j < 3; $j++) {
$types[] = $type = array(
'count' => $j + 1,
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
......@@ -83,7 +84,7 @@ public function testIntegration() {
foreach ($entries as $entry) {
$entry += array(
'type' => 'test-views',
'severity' => WATCHDOG_NOTICE,
'severity' => RfcLogLevel::NOTICE,
);
$logger_factory->get($entry['type'])->log($entry['severity'], $entry['message'], $entry['variables']);
}
......
......@@ -7,6 +7,8 @@
namespace Drupal\migrate;
use Drupal\Core\Logger\RfcLogLevel;
/**
* Defines a migrate message class.
*/
......@@ -18,15 +20,15 @@ class MigrateMessage implements MigrateMessageInterface {
* @var array
*/
protected $map = array(
'status' => WATCHDOG_INFO,
'error' => WATCHDOG_ERROR,
'status' => RfcLogLevel::INFO,
'error' => RfcLogLevel::ERROR,
);
/**
* {@inheritdoc}
*/
public function display($message, $type = 'status') {
$type = isset($this->map[$type]) ? $this->map[$type] : WATCHDOG_NOTICE;
$type = isset($this->map[$type]) ? $this->map[$type] : RfcLogLevel::NOTICE;
\Drupal::logger('migrate')->log($type, $message);
}
......
......@@ -9,14 +9,14 @@
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
/**
* Redirects logging messages to syslog.
*/
class SysLog implements LoggerInterface {
use LoggerTrait;
use RfcLoggerTrait;
/**
* A configuration object containin syslog settings.
......
......@@ -7,6 +7,8 @@
namespace Drupal\system\Tests\Module;
use Drupal\Core\Logger\RfcLogLevel;
/**
* Install/uninstall core module and confirm table creation/deletion.
*
......@@ -93,7 +95,7 @@ public function testInstallUninstall() {
$this->assertModules(array($module_to_install), TRUE);
$this->assertModuleTablesExist($module_to_install);
$this->assertModuleConfig($module_to_install);
$this->assertLogMessage('system', "%module module installed.", array('%module' => $module_to_install), WATCHDOG_INFO);
$this->assertLogMessage('system', "%module module installed.", array('%module' => $module_to_install), RfcLogLevel::INFO);
}
// Uninstall the original module, and check appropriate
......@@ -162,7 +164,7 @@ protected function assertSuccessfullUninstall($module, $package = 'Core') {
// module was just uninstalled, since the {watchdog} table won't be there
// anymore.)
$this->assertText(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
$this->assertLogMessage('system', "%module module uninstalled.", array('%module' => $module), WATCHDOG_INFO);
$this->assertLogMessage('system', "%module module uninstalled.", array('%module' => $module), RfcLogLevel::INFO);
// Check that the module's database tables no longer exist.
$this->assertModuleTablesDoNotExist($module);
......
......@@ -10,6 +10,7 @@
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\simpletest\WebTestBase;
/**
......@@ -183,7 +184,7 @@ function assertModules(array $modules, $enabled) {
* @param $link
* A link to associate with the message.
*/
function assertLogMessage($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = '') {
function assertLogMessage($type, $message, $variables = array(), $severity = RfcLogLevel::NOTICE, $link = '') {
$count = db_select('watchdog', 'w')
->condition('type', $type)
->condition('message', $message)
......
......@@ -12,18 +12,6 @@
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
// @todo Remove in https://www.drupal.org/node/2267545.
if (!defined('WATCHDOG_EMERGENCY')) {
define('WATCHDOG_EMERGENCY', 0);
define('WATCHDOG_ALERT', 1);
define('WATCHDOG_CRITICAL', 2);
define('WATCHDOG_ERROR', 3);
define('WATCHDOG_WARNING', 4);
define('WATCHDOG_NOTICE', 5);
define('WATCHDOG_INFO', 6);
define('WATCHDOG_DEBUG', 7);
}
/**
* @coversDefaultClass \Drupal\Core\Logger\LoggerChannelFactory
* @group Logger
......
......@@ -13,18 +13,6 @@
use Symfony\Component\HttpFoundation\Request;