Skip to content

Commit

Permalink
WIP: Replace more ILogger method calls
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.

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
Fenn-CS committed Mar 30, 2023
1 parent 8ee52d3 commit 94953dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
21 changes: 16 additions & 5 deletions index.php
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
Expand Up @@ -1834,6 +1834,13 @@ public function getLogger() {
return $this->get(ILogger::class);
}

public function getPsrLogger(): LoggerInterface {
$logFactory = $this->get(LogFactory::class);
$default = $this->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log';
$logFile = $this->getConfig()->getAppValue('admin_audit', 'logfile', $default);
return $logFactory->getCustomPsrLogger($logFile);
}

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

0 comments on commit 94953dc

Please sign in to comment.