Skip to content

Commit

Permalink
Avoid making db connection when logging about database connection
Browse files Browse the repository at this point in the history
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 <fenn25.fn@gmail.com>
  • Loading branch information
nfebe committed May 9, 2023
1 parent 72a0c97 commit 4eac2d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
require_once __DIR__ . '/lib/versioncheck.php';


try {
require_once __DIR__ . '/lib/base.php';

Expand Down
1 change: 1 addition & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ protected static function handleAuthHeaders(): void {
}
}
}

}

OC::init();
10 changes: 9 additions & 1 deletion lib/private/Log/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4eac2d8

Please sign in to comment.