diff --git a/core/authorize.php b/core/authorize.php index 8715fb0..6db7d1c 100644 --- a/core/authorize.php +++ b/core/authorize.php @@ -148,7 +148,7 @@ function authorize_access_allowed() { } else { drupal_add_http_header('Status', '403 Forbidden'); - watchdog('access denied', 'authorize.php', NULL, WATCHDOG_WARNING); + watchdog('access denied', 'authorize.php', array(), WATCHDOG_WARNING); $page_title = t('Access denied'); $output = t('You are not allowed to access this page.'); } diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index d61a75b..f4f26bd 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1182,11 +1182,14 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * @param $link * A link to associate with the message. * + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. + * Use \Drupal::logger($channel)->log($severity, $message, $context), or any + * of the shortcut methods of \Psr\Log\LoggerTrait. + * * @see watchdog_severity_levels() * @see hook_watchdog() */ -function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { - $variables = $variables ?: array(); +function watchdog($type, $message, array $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) { if ($link) { $variables['link'] = $link; } diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index e1ae91a..e955343 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -612,4 +612,14 @@ public static function formBuilder() { return static::$container->get('form_builder'); } + /** + * Returns a channel logger object. + * + * @return \Drupal\Core\Logger\LoggerChannelInterface + * The logger for this channel. + */ + public static function logger($channel) { + return static::$container->get('logger.factory')->get($channel); + } + } diff --git a/core/lib/Drupal/Core/Controller/ExceptionController.php b/core/lib/Drupal/Core/Controller/ExceptionController.php index d79567c..926d65f 100644 --- a/core/lib/Drupal/Core/Controller/ExceptionController.php +++ b/core/lib/Drupal/Core/Controller/ExceptionController.php @@ -136,7 +136,7 @@ public function on405Html(FlattenException $exception, Request $request) { */ public function on403Html(FlattenException $exception, Request $request) { $system_path = $request->attributes->get('_system_path'); - watchdog('access denied', $system_path, NULL, WATCHDOG_WARNING); + watchdog('access denied', $system_path, array(), WATCHDOG_WARNING); $system_config = $this->container->get('config.factory')->get('system.site'); $path = $this->container->get('path.alias_manager')->getSystemPath($system_config->get('page.403')); @@ -200,7 +200,7 @@ public function on403Html(FlattenException $exception, Request $request) { * A response object. */ public function on404Html(FlattenException $exception, Request $request) { - watchdog('page not found', String::checkPlain($request->attributes->get('_system_path')), NULL, WATCHDOG_WARNING); + watchdog('page not found', String::checkPlain($request->attributes->get('_system_path')), array(), WATCHDOG_WARNING); // Check for and return a fast 404 page if configured. $config = \Drupal::config('system.performance'); diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 1231689..c4c1fef 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -1735,7 +1735,7 @@ protected function drupalInstallationAttempted() { /** * Wraps watchdog(). */ - protected function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { + protected function watchdog($type, $message, array $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) { watchdog($type, $message, $variables, $severity, $link); } diff --git a/core/lib/Drupal/Core/Logger/LogMessageParserTrait.php b/core/lib/Drupal/Core/Logger/LogMessageParserTrait.php index 808c7a8..309f1e1 100644 --- a/core/lib/Drupal/Core/Logger/LogMessageParserTrait.php +++ b/core/lib/Drupal/Core/Logger/LogMessageParserTrait.php @@ -34,16 +34,16 @@ */ protected function parseMessagePlaceholders(&$message, array &$context) { $variables = array(); - $has_ps3 = FALSE; + $has_psr3 = FALSE; if (($start = strpos($message, '{')) !== FALSE && strpos($message, '}') > $start) { - $has_ps3 = TRUE; + $has_psr3 = TRUE; // Transform PSR3 style messages containing placeholders to // \Drupal\Component\Utility\String::format() style. $message = preg_replace('/\{(.*)\}/U', '@$1', $message); } foreach ($context as $key => $variable) { // PSR3 style placeholders. - if ($has_ps3) { + if ($has_psr3) { // Keys are not prefixed with anything according to PSR3 specs. // If the message is "User {username} created" the variable key will be // just "username". diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 6ba39d9..e09adac 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -28,9 +28,21 @@ class LoggerChannel implements LoggerChannelInterface { /** * Map of PSR Log constants to Watchdog log constants. * + * This only exists for BC. + * @todo Remove once watchdog() is removed. + * * @var array */ - protected $levelTranslation; + 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, + ); /** * An array of arrays of \Psr\Log\LoggerInterface keyed by priority. @@ -90,8 +102,8 @@ public function log($level, $message, array $context = array()) { } if (is_string($level)) { - // Convert to integer equivalent for BC and PHP syslog() compatibility. - $level = $this->mapLevel($level); + // Convert to integer equivalent for BC. + $level = $this->levelTranslation[$level]; } // Call all available loggers. foreach ($this->sortLoggers() as $logger) { @@ -117,13 +129,6 @@ public function setCurrentUser(AccountInterface $current_user = NULL) { * {@inheritdoc} */ public function setLoggers(array $loggers) { - foreach ($loggers as $priority => $group) { - foreach ($group as $logger) { - if (!$logger instanceof LoggerInterface) { - throw new InvalidArgumentException('All loggers must implement \Psr\Log\LoggerInterface.'); - } - } - } $this->loggers = $loggers; } @@ -135,35 +140,6 @@ public function addLogger(LoggerInterface $logger, $priority = 0) { } /** - * Converts a PSR Log level to a Watchdog log level. - * - * This only exists for BC. - * @todo Remove once watchdog() is removed. - * - * @param string $level - * A PSR-3 logging level string. - * - * @return int - * The corresponding Watchdog constant. - */ - protected function mapLevel($level) { - if (empty($this->levelTranslation)) { - $this->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, - ); - } - - return $this->levelTranslation[$level]; - } - - /** * Sorts loggers according to priority. * * @return array diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php index 986af54..58c01ba 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php @@ -14,7 +14,7 @@ /** * Defines a factory for logging channels. */ -class LoggerChannelFactory extends ContainerAware { +class LoggerChannelFactory extends ContainerAware implements LoggerChannelFactoryInterface { /** * Array of all instantiated logger channels keyed by channel name. diff --git a/core/modules/dblog/lib/Drupal/dblog/Logger/DbLog.php b/core/modules/dblog/lib/Drupal/dblog/Logger/DbLog.php index 034c6ba..47c5178 100644 --- a/core/modules/dblog/lib/Drupal/dblog/Logger/DbLog.php +++ b/core/modules/dblog/lib/Drupal/dblog/Logger/DbLog.php @@ -18,6 +18,7 @@ class DbLog implements LoggerInterface { use LoggerTrait; + use LogMessageParserTrait; /** diff --git a/core/modules/syslog/lib/Drupal/syslog/Logger/SysLog.php b/core/modules/syslog/lib/Drupal/syslog/Logger/SysLog.php index b0e31f8..70dae66 100644 --- a/core/modules/syslog/lib/Drupal/syslog/Logger/SysLog.php +++ b/core/modules/syslog/lib/Drupal/syslog/Logger/SysLog.php @@ -28,6 +28,13 @@ class SysLog implements LoggerInterface { protected $config; /** + * Stores whether there is a system logger connection opened or not. + * + * @var bool + */ + protected $connectionOpened = FALSE; + + /** * Constructs a SysLog object. * * @param \Drupal\Core\Config\ConfigFactory $config_factory @@ -35,11 +42,19 @@ class SysLog implements LoggerInterface { */ public function __construct(ConfigFactory $config_factory) { $this->config = $config_factory->get('syslog.settings'); - $facility = $this->config->get('facility'); - if ($facility === '') { - $facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER; + } + + /** + * Opens a connection to the system logger. + */ + protected function openConnection() { + if (!$this->connectionOpened) { + $facility = $this->config->get('facility'); + if ($facility === '') { + $facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER; + } + $this->connectionOpened = openlog($this->config->get('identity'), LOG_NDELAY, $facility); } - openlog($this->config->get('identity'), LOG_NDELAY, $facility); } /** @@ -48,6 +63,9 @@ public function __construct(ConfigFactory $config_factory) { public function log($level, $message, array $context = array()) { global $base_url; + // Ensure we have a connection available. + $this->openConnection(); + // Populate the $context['variables'] with the message placeholders. $this->parseMessagePlaceholders($message, $context); diff --git a/core/modules/system/lib/Drupal/system/Tests/Pager/PagerTest.php b/core/modules/system/lib/Drupal/system/Tests/Pager/PagerTest.php index dc8ead4..845aa28 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Pager/PagerTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Pager/PagerTest.php @@ -36,7 +36,7 @@ function setUp() { // Insert 300 log messages. for ($i = 0; $i < 300; $i++) { - watchdog('pager_test', $this->randomString(), NULL, WATCHDOG_DEBUG); + watchdog('pager_test', $this->randomString(), array(), WATCHDOG_DEBUG); } $this->admin_user = $this->drupalCreateUser(array( diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php index 0bb5006..65ba07d 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php @@ -118,7 +118,7 @@ public function testOnRouteBuildingInvalidVariables(Route $route) { namespace { if (!function_exists('watchdog')) { - function watchdog($type, $message, array $args = NULL) { + function watchdog($type, $message, array $args = array()) { } } if (!function_exists('drupal_set_message')) { diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index a1d2ff9..d070209 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -752,6 +752,7 @@ public static function create(ContainerInterface $container) { namespace { function test_form_id_custom_submit(array &$form, array &$form_state) { } + // @todo Remove once watchdog() is removed. if (!defined('WATCHDOG_ERROR')) { define('WATCHDOG_ERROR', 3); } diff --git a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php index 44059c4..7cf98b7 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php +++ b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php @@ -297,7 +297,7 @@ protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = /** * {@inheritdoc} */ - protected function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { + protected function watchdog($type, $message, array $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) { } /** diff --git a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelFactoryTest.php b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelFactoryTest.php index 5028334..23f2ef6 100644 --- a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelFactoryTest.php +++ b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelFactoryTest.php @@ -13,16 +13,19 @@ use Symfony\Component\HttpFoundation\Request; // @todo Remove once watchdog() is removed. -if (!defined('WATCHDOG_ERROR')) { +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); } +// WATCHDOG_ERROR is also defined in FormBuilderTest, so check independently. +if (!defined('WATCHDOG_ERROR')) { + define('WATCHDOG_ERROR', 3); +} /** * Tests the logger channel factory. diff --git a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php index cb24bcb..817f935 100644 --- a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php +++ b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php @@ -13,16 +13,19 @@ use Symfony\Component\HttpFoundation\Request; // @todo Remove once watchdog() is removed. -if (!defined('WATCHDOG_ERROR')) { +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); } +// WATCHDOG_ERROR is also defined in FormBuilderTest, so check independently. +if (!defined('WATCHDOG_ERROR')) { + define('WATCHDOG_ERROR', 3); +} /** * Tests the logger channel. diff --git a/core/update.php b/core/update.php index a2de6fc..42eb5bc 100644 --- a/core/update.php +++ b/core/update.php @@ -230,7 +230,7 @@ function update_info_page() { function update_access_denied_page() { drupal_add_http_header('Status', '403 Forbidden'); header(\Drupal::request()->server->get('SERVER_PROTOCOL') . ' 403 Forbidden'); - watchdog('access denied', 'update.php', NULL, WATCHDOG_WARNING); + watchdog('access denied', 'update.php', array(), WATCHDOG_WARNING); $output = '

Access denied. You are not authorized to access this page. Log in using either an account with the administer software updates permission or the site maintenance account (the account you created during installation). If you cannot log in, you will have to edit settings.php to bypass this access check. To do this:

  1. With a text editor find the settings.php file on your system. From the main Drupal directory that you installed all the files into, go to sites/your_site_name if such directory exists, or else to sites/default which applies otherwise.