class DbLog

Same name in other branches
  1. 9 core/modules/dblog/src/Logger/DbLog.php \Drupal\dblog\Logger\DbLog
  2. 8.9.x core/modules/dblog/src/Logger/DbLog.php \Drupal\dblog\Logger\DbLog
  3. 10 core/modules/dblog/src/Logger/DbLog.php \Drupal\dblog\Logger\DbLog

Logs events in the watchdog database table.

Hierarchy

Expanded class hierarchy of DbLog

65 string references to 'DbLog'
CommentIntegrationTest::setUp in core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php
ConfigEntityUpdater::update in core/lib/Drupal/Core/Config/Entity/ConfigEntityUpdater.php
Updates configuration entities as part of a Drupal update.
ConfigImporterTest::testUnmetDependency in core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php
Tests dependency validation during configuration import.
ConfigInstallProfileOverrideTest::testInstallProfileConfigOverwrite in core/modules/config/tests/src/Functional/ConfigInstallProfileOverrideTest.php
Tests install profile config changes.
config_test.dynamic.completely_new.yml in core/profiles/testing_config_overrides/config/optional/config_test.dynamic.completely_new.yml
core/profiles/testing_config_overrides/config/optional/config_test.dynamic.completely_new.yml

... See full list

1 service uses DbLog
logger.dblog in core/modules/dblog/dblog.services.yml
Drupal\dblog\Logger\DbLog

File

core/modules/dblog/src/Logger/DbLog.php, line 16

Namespace

Drupal\dblog\Logger
View source
class DbLog implements LoggerInterface {
    use RfcLoggerTrait;
    use DependencySerializationTrait;
    
    /**
     * The dedicated database connection target to use for log entries.
     */
    const DEDICATED_DBLOG_CONNECTION_TARGET = 'dedicated_dblog';
    
    /**
     * The database connection object.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $connection;
    
    /**
     * The message's placeholders parser.
     *
     * @var \Drupal\Core\Logger\LogMessageParserInterface
     */
    protected $parser;
    
    /**
     * Constructs a DbLog object.
     *
     * @param \Drupal\Core\Database\Connection $connection
     *   The database connection object.
     * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
     *   The parser to use when extracting message variables.
     */
    public function __construct(Connection $connection, LogMessageParserInterface $parser) {
        $this->connection = $connection;
        $this->parser = $parser;
    }
    
    /**
     * {@inheritdoc}
     */
    public function log($level, string|\Stringable $message, array $context = []) : void {
        // Remove backtrace and exception since they may contain an unserializable variable.
        unset($context['backtrace'], $context['exception']);
        // Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
        // style, so they can be translated too in runtime.
        $message_placeholders = $this->parser
            ->parseMessagePlaceholders($message, $context);
        try {
            $this->connection
                ->insert('watchdog')
                ->fields([
                'uid' => $context['uid'],
                'type' => mb_substr($context['channel'], 0, 64),
                'message' => $message,
                'variables' => serialize($message_placeholders),
                'severity' => $level,
                'link' => $context['link'],
                'location' => $context['request_uri'],
                'referer' => $context['referer'],
                'hostname' => mb_substr($context['ip'], 0, 128),
                'timestamp' => $context['timestamp'],
            ])
                ->execute();
        } catch (\Exception $e) {
            // When running Drupal on MySQL or MariaDB you can run into several errors
            // that corrupt the database connection. Some examples for these kind of
            // errors on the database layer are "1100 - Table 'xyz' was not locked
            // with LOCK TABLES" and "1153 - Got a packet bigger than
            // 'max_allowed_packet' bytes". If such an error happens, the MySQL server
            // invalidates the connection and answers all further requests in this
            // connection with "2006 - MySQL server had gone away". In that case the
            // insert statement above results in a database exception. To ensure that
            // the causal error is written to the log we try once to open a dedicated
            // connection and write again.
            if (($e instanceof DatabaseException || $e instanceof \PDOException) && $this->connection
                ->getTarget() != self::DEDICATED_DBLOG_CONNECTION_TARGET) {
                // Open a dedicated connection for logging.
                $key = $this->connection
                    ->getKey();
                $info = Database::getConnectionInfo($key);
                Database::addConnectionInfo($key, self::DEDICATED_DBLOG_CONNECTION_TARGET, $info['default']);
                $this->connection = Database::getConnection(self::DEDICATED_DBLOG_CONNECTION_TARGET, $key);
                // Now try once to log the error again.
                $this->log($level, $message, $context);
            }
            else {
                throw $e;
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DbLog::$connection protected property The database connection object.
DbLog::$parser protected property The message's placeholders parser.
DbLog::DEDICATED_DBLOG_CONNECTION_TARGET constant The dedicated database connection target to use for log entries.
DbLog::log public function Overrides RfcLoggerTrait::log
DbLog::__construct public function Constructs a DbLog object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
RfcLoggerTrait::alert public function
RfcLoggerTrait::critical public function
RfcLoggerTrait::debug public function
RfcLoggerTrait::emergency public function
RfcLoggerTrait::error public function
RfcLoggerTrait::info public function
RfcLoggerTrait::notice public function
RfcLoggerTrait::warning public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.