Skip to content

Commit

Permalink
Show log content summary in log list; also reverse log order, so newe…
Browse files Browse the repository at this point in the history
…st are first (lonnieezell#492)

* Show log content summary in log list; reverse logs, so newest are first

* Fix style on file

* Improve mobile view of log list
  • Loading branch information
dgvirtual authored Jan 3, 2025
1 parent 26721f7 commit 2487491
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
15 changes: 14 additions & 1 deletion src/Tools/Controllers/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@ public function __construct()
public function index()
{
// Load the Log Files.
$logs = get_filenames($this->logsPath);
$logs = array_reverse(get_filenames($this->logsPath));

unset($logs[0]);

$result = $this->logsHandler->paginateLogs($logs, $this->logsLimit);

// Cycle through the $result array and attach the content property
for ($i = 0; $i < count($result['logs']); $i++) {
if ($result['logs'][$i] === 'index.html') {
unset($result['logs'][$i]);
continue;
}
$logFilePath = $this->logsPath . $result['logs'][$i];
$result['logs'][$i] = [
'filename' => $result['logs'][$i],
'content' => $this->logsHandler->countLogLevels($logFilePath),
];
}

return $this->render($this->viewPrefix . 'logs', [
'logs' => $result['logs'],
'pager' => $result['pager'],
Expand Down
38 changes: 38 additions & 0 deletions src/Tools/Libraries/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,44 @@ public function processFileLogs($file)
return $superLog;
}

public function countLogLevels($filePath): string
{
$levels = array_keys(self::$levelClasses);

// Initialize the counts array
$counts = array_fill_keys($levels, 0);

// Read the file content
$fileContent = file_get_contents($filePath);

if ($fileContent === false) {
throw new Exception("Unable to read the file: $filePath");
}

// Count occurrences of each level
foreach ($levels as $level) {
$counts[$level] = substr_count($fileContent, $level);
}

// Remove entries with value 0
$counts = array_filter($counts, function ($value) {
return $value > 0;
});

$counts = array_reverse($counts);


// Transform the array into a string with color codes
$result = [];
foreach ($counts as $level => $count) {
$class = self::$levelClasses[$level];
$result[] = '<span class="text-' . $class . '">' . $level . '</span>: ' . $count;
}

return strtolower(implode(', ', $result));
}


/**
* returns an array of the file contents
* each element in the array is a line
Expand Down
20 changes: 9 additions & 11 deletions src/Tools/Views/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,29 @@
<input class="select-all" type="checkbox" />
</th>
<?php endif ?>
<th class='date'><?= lang('Tools.date'); ?></th>
<th><?= lang('Tools.file'); ?></th>
<th class='date no-wrap'><?= lang('Tools.date'); ?></th>
<th><?= lang('Tools.content'); ?></th>
<th class="d-none d-lg-table-cell"><?= lang('Tools.file'); ?></th>
</tr>
</thead>

<tbody>
<?php
foreach ($logs as $log) :
// Skip the index.html file.
if ($log === 'index.html') {
continue;
}
?>
<tr>
<?php if (auth()->user()->can('logs.manage')) : ?>
<td class="column-check text-center">
<input type="checkbox" value="<?= esc(str_replace('.log', '', $log)); ?>" name="checked[]" />
<input type="checkbox" value="<?= esc(str_replace('.log', '', $log['filename'])); ?>" name="checked[]" />
</td>
<?php endif ?>
<td class='date'>
<a href='<?= site_url(ADMIN_AREA . "/tools/view-log/" . str_replace('.log', '', $log)); ?>'>
<?= app_date(str_replace('.log', '', str_replace('log-', '', $log))); ?>
<td class='date no-wrap'>
<a href='<?= site_url(ADMIN_AREA . "/tools/view-log/" . str_replace('.log', '', $log['filename'])); ?>'>
<?= app_date(str_replace('.log', '', str_replace('log-', '', $log['filename']))); ?>
</a>
</td>
<td><?= esc($log); ?></td>
<td><?= $log['content'] ?></td>
<td class="d-none d-lg-table-cell"><?= esc($log['filename']) ?></td>
</tr>
<?php
endforeach;
Expand Down
10 changes: 10 additions & 0 deletions themes/Admin/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,14 @@ input[type="checkbox"]:indeterminate {
*/
[x-cloak] {
display: none !important;
}

/*
* --------------------------------------------------------------------------
* LOGS MODULE
* --------------------------------------------------------------------------
*/

.no-wrap {
white-space: nowrap;
}

0 comments on commit 2487491

Please sign in to comment.