Skip to content

Commit

Permalink
Replace ILogger method calls index.php
Browse files Browse the repository at this point in the history
This commit replaces more ILogger method calls with
 `Psr\Log\LoggerInterface` as we gradually move away from the
 custom ILogger.

This commit aslo, sets the customPsrLogger to not depend on the
 database to `logfile` config value.

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
nfebe committed Apr 13, 2023
1 parent 8ee52d3 commit f3f988c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
21 changes: 16 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

OC::handleRequest();
} catch (\OC\ServiceUnavailableException $ex) {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->getPsrLogger()->error($ex->getMessage(), [
'exception' => $ex,
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 503);
Expand All @@ -44,8 +46,12 @@
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
} catch (Exception $ex2) {
try {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->getLogger()->logException($ex2, ['app' => 'index']);
\OC::$server->getPsrLogger()->error($ex->getMessage(), [
'exception' => $ex,
]);
\OC::$server->getPsrLogger()->error($ex2->getMessage(), [
'exception' => $ex2,
]);
} catch (Throwable $e) {
// no way to log it properly - but to avoid a white page of death we try harder and ignore this one here
}
Expand All @@ -68,13 +74,17 @@
}
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
} catch (Exception $ex) {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->getPsrLogger()->error($ex->getMessage(), [
'exception' => $ex,
]);

//show the user a detailed error page
OC_Template::printExceptionErrorPage($ex, 500);
} catch (Error $ex) {
try {
\OC::$server->getLogger()->logException($ex, ['app' => 'index']);
\OC::$server->getPsrLogger()->error($ex->getMessage(), [
'exception' => $ex,
]);
} catch (Error $e) {
http_response_code(500);
header('Content-Type: text/plain; charset=utf-8');
Expand All @@ -83,6 +93,7 @@
print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
print("More details can be found in the webserver log.\n");


throw $ex;
}
OC_Template::printExceptionErrorPage($ex, 500);
Expand Down
7 changes: 7 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,13 @@ public function getLogger() {
return $this->get(ILogger::class);
}

public function getPsrLogger(): LoggerInterface {
$logFactory = $this->get(LogFactory::class);
$logFile = \OC::$server->get(SystemConfig::class)->getValue('logfile', false);
$logFile = $logFile ? $logFile : \OC::$SERVERROOT . '/data/nextcloud.log';
return $logFactory->getCustomPsrLogger($logFile);
}

/**
* @return ILogFactory
* @throws \OCP\AppFramework\QueryException
Expand Down

0 comments on commit f3f988c

Please sign in to comment.