Skip to content

Commit b3ec06d

Browse files
authored
Merge pull request #866 from joshtrichards/jr-better-handle-non-file-log-types
Better handle when log_type isn't `file` and give explanation
2 parents 5a5afef + d4fbd43 commit b3ec06d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

js/logreader-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Controller/LogController.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use OCA\LogReader\Log\SearchFilter;
2626
use OCP\AppFramework\Controller;
2727
use OCP\AppFramework\Http\JSONResponse;
28-
use OCP\AppFramework\Http\TemplateResponse;
2928
use OCP\IConfig;
3029
use OCP\IRequest;
3130

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

6384

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

115+
$logType = $this->config->getSystemValue('log_type', 'file');
116+
if ($logType !== 'file') { // we only support access when `log_type` is set to `file` (the default)
117+
// TODO: Don't even attempt polling in the front-end
118+
sleep(20);
119+
return new JSONResponse([]);
120+
}
94121
$lastItem = $this->getLastItem($levels);
95122
while ($lastItem === null || $lastItem['reqId'] === $lastReqId) {
96123
sleep(1);
@@ -128,11 +155,11 @@ public function poll(string $lastReqId, string $levels = '11111'): JSONResponse
128155
* @param int $count
129156
* @param int $offset
130157
* @param string $levels
131-
* @return TemplateResponse
158+
* @return JSONResponse
132159
*
133160
* @NoCSRFRequired
134161
*/
135-
public function search($query = '', $count = 50, $offset = 0, $levels = '11111') {
162+
public function search($query = '', $count = 50, $offset = 0, $levels = '11111'): JSONResponse {
136163
$iterator = $this->logIteratorFactory->getLogIterator($levels);
137164
$iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times
138165
$iterator->rewind();
@@ -193,7 +220,7 @@ public function setLevels(string $levels): int {
193220
return $minLevel;
194221
}
195222

196-
protected function responseFromIterator(\Iterator $iterator, $count, $offset) {
223+
protected function responseFromIterator(\Iterator $iterator, $count, $offset): JSONResponse {
197224
$iterator->rewind();
198225
for ($i = 0; $i < $offset; $i++) {
199226
$iterator->next();

0 commit comments

Comments
 (0)