Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle when log_type isn't file and give explanation #866

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/logreader-main.js.map

Large diffs are not rendered by default.

43 changes: 35 additions & 8 deletions lib/Controller/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use OCA\LogReader\Log\SearchFilter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IRequest;

Expand Down Expand Up @@ -53,11 +52,33 @@ public function __construct($appName,
* @param int $count
* @param int $offset
* @param string $levels
* @return TemplateResponse
* @return JSONResponse
*/
public function get($count = 50, $offset = 0, $levels = '11111') {
$iterator = $this->logIteratorFactory->getLogIterator($levels);
return $this->responseFromIterator($iterator, $count, $offset);
public function get($count = 50, $offset = 0, $levels = '11111'): JSONResponse {
$logType = $this->config->getSystemValue('log_type', 'file');
if ($logType === 'file') { // we only support web access when `log_type` is set to `file` (the default)
$iterator = $this->logIteratorFactory->getLogIterator($levels);
return $this->responseFromIterator($iterator, $count, $offset);
} else { // A log_type other than `file` seems to be configured so:
// * Generate a dummy entry so we don't error out
// * Use the dummy entry to inform the admin to look elsewhere and/or correct their configuration
$dummyLine["id"] = uniqid();
$dummyLine["reqid"] = "00000000000000000000"; // irrelevant
$dummyLine["level"] = 1; // INFO
$dummyLine["time"] = date(DATE_ATOM, time());
$dummyLine["remoteAddr"] = "0.0.0.0";
$dummyLine["user"] = "---";
$dummyLine["app"] = "Logreader";
$dummyLine["method"] = "---";
$dummyLine["url"] = "---";
$dummyLine["message"] =
'File-based logging must be enabled to access logs from the Web UI. Your `log_type` is currently '
. 'set to: [' . $logType . ']. If you feel this is an error, please verify `log_type` in your '
. 'config.php and check the Nextcloud Administration Manual. This is not an actual log entry.';
$dummyLine["userAgent"] = "---";
$dummyLine["version"] = "---";
return new JSONResponse(['data' => $dummyLine, 'remain' => false]);
}
}


Expand Down Expand Up @@ -91,6 +112,12 @@ public function poll(string $lastReqId, string $levels = '11111'): JSONResponse
$cycles = 0;
$maxCycles = 20;

$logType = $this->config->getSystemValue('log_type', 'file');
if ($logType !== 'file') { // we only support access when `log_type` is set to `file` (the default)
// TODO: Don't even attempt polling in the front-end
sleep(20);
return new JSONResponse([]);
}
$lastItem = $this->getLastItem($levels);
while ($lastItem === null || $lastItem['reqId'] === $lastReqId) {
sleep(1);
Expand Down Expand Up @@ -128,11 +155,11 @@ public function poll(string $lastReqId, string $levels = '11111'): JSONResponse
* @param int $count
* @param int $offset
* @param string $levels
* @return TemplateResponse
* @return JSONResponse
*
* @NoCSRFRequired
*/
public function search($query = '', $count = 50, $offset = 0, $levels = '11111') {
public function search($query = '', $count = 50, $offset = 0, $levels = '11111'): JSONResponse {
$iterator = $this->logIteratorFactory->getLogIterator($levels);
$iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times
$iterator->rewind();
Expand Down Expand Up @@ -193,7 +220,7 @@ public function setLevels(string $levels): int {
return $minLevel;
}

protected function responseFromIterator(\Iterator $iterator, $count, $offset) {
protected function responseFromIterator(\Iterator $iterator, $count, $offset): JSONResponse {
$iterator->rewind();
for ($i = 0; $i < $offset; $i++) {
$iterator->next();
Expand Down