From 4eac2d8bc28ad58948ea4c46ac8e8487b326e662 Mon Sep 17 00:00:00 2001 From: fenn-cs Date: Fri, 14 Apr 2023 16:20:56 +0100 Subject: [PATCH] Avoid making db connection when logging about database connection For various reasons, sometimes a database would not be possible. Our file logger before this commit has a function call that relies on a db connection that is ironic and creates some kind of chicken and egg scenario. This commit, resolves that. Signed-off-by: fenn-cs --- index.php | 1 + lib/base.php | 1 + lib/private/Log/File.php | 10 +++++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 534b1c3f993c6..949207002bde9 100644 --- a/index.php +++ b/index.php @@ -30,6 +30,7 @@ */ require_once __DIR__ . '/lib/versioncheck.php'; + try { require_once __DIR__ . '/lib/base.php'; diff --git a/lib/base.php b/lib/base.php index 119e89f6ae0bf..909398dc6ca46 100644 --- a/lib/base.php +++ b/lib/base.php @@ -1165,6 +1165,7 @@ protected static function handleAuthHeaders(): void { } } } + } OC::init(); diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index a33667c9b68f7..f2362978ba7b2 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -80,7 +80,15 @@ public function __construct(string $path, string $fallbackPath, SystemConfig $co * @param int $level */ public function write(string $app, $message, int $level) { - $entry = $this->logDetailsAsJSON($app, $message, $level); + try { + $entry = $this->logDetailsAsJSON($app, $message, $level); + } catch (\Doctrine\DBAL\Exception $e) { + // The existing logger system deeply depends on the database to write any logs + // This means if there is no connection to the database nothing can be logged + // Including the database connection error + // We catch the database connection here and avoid the part (\OC\Log\LogDetails) that relies on the the database. + $entry = json_encode(['app' => $app, 'message' => $message, 'level' => 1]); + } $handle = @fopen($this->logFile, 'a'); if ($this->logFileMode > 0 && is_file($this->logFile) && (fileperms($this->logFile) & 0777) != $this->logFileMode) { @chmod($this->logFile, $this->logFileMode);