Skip to content

Commit

Permalink
Merge pull request #5182 from Gustry/admin-log
Browse files Browse the repository at this point in the history
Add error log panel in admin
  • Loading branch information
Gustry authored Jan 15, 2025
2 parents a308f29 + d8a5926 commit a4d162e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 13 deletions.
53 changes: 51 additions & 2 deletions lizmap/modules/admin/controllers/logs.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,36 @@ public function index()
$conditions = jDao::createConditions();
$detailNumber = $dao->countBy($conditions);

// Get last error log
$errorLog = \Lizmap\App\FileTools::tail(jApp::logPath('lizmap-admin.log'), 50);
// Number of lines for logs
$maxLinesToFetch = 200;

// Get last admin log
$lizmapLogPath = jApp::logPath('lizmap-admin.log');
$lizmapLog = \Lizmap\App\FileTools::tail($lizmapLogPath, $maxLinesToFetch);
$lizmapLogTextArea = $this->logLinesDisplayTextArea($lizmapLog);

$errorLogDisplay = !\lizmap::getServices()->hideSensitiveProperties();
$errorLogPath = jApp::logPath('errors.log');
$errorLog = '';
$errorLogTextArea = '';
if ($errorLogDisplay) {
// Get last error log
$errorLog = \Lizmap\App\FileTools::tail($errorLogPath, $maxLinesToFetch);
$errorLogTextArea = $this->logLinesDisplayTextArea($errorLog);
}

// Display content via templates
$tpl = new jTpl();
$assign = array(
'counterNumber' => $counterNumber,
'detailNumber' => $detailNumber,
'lizmapLog' => $lizmapLog,
'lizmapLogBaseName' => basename($lizmapLogPath),
'lizmapLogTextArea' => $lizmapLogTextArea,
'errorLogDisplay' => $errorLogDisplay,
'errorLog' => $errorLog,
'errorLogBaseName' => basename($errorLogPath),
'errorLogTextArea' => $errorLogTextArea,
);
$tpl->assign($assign);
$rep->body->assign('MAIN', $tpl->fetch('logs_view'));
Expand All @@ -62,6 +83,34 @@ public function index()
return $rep;
}

/**
* Compute the height of the text area to use by default.
*
* @param string $log the log content
*
* @return int the number of lines for the text area
*/
private function logLinesDisplayTextArea($log)
{
$maxLinesTextArea = 30;
$minLinesTextArea = 10;

$numberLines = substr_count($log, "\n");

if ($numberLines < $minLinesTextArea) {
// Log file < 10
return $minLinesTextArea;
}

if ($numberLines < $maxLinesTextArea) {
// 10 <= log file < 30
return $numberLines;
}

// log file >= 30
return $maxLinesTextArea;
}

/**
* Display the logs counter.
*
Expand Down
18 changes: 11 additions & 7 deletions lizmap/modules/admin/locales/en_US/admin.UTF-8.properties
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,18 @@ logs.email.popup.body=A user has opened a feature information popup.
logs.email.editionSaveFeature.body=A user has created or modified a feature.
logs.email.editionDeleteFeature.body=A user has deleted a feature.
logs.error.title=Error log
logs.error.sentence=Last lines of the Lizmap Web Client application error log file
logs.error.file.too.big=The error log file is too big. Consider using log rotation for Lizmap Web Client logs.
logs.error.file.erase=Erase the error log file
logs.error.file.erase.confirm=Are you sure you want to erase the error log file?
logs.error.file.erase.ok=The error log file has successfully been erased.
logs.error.no.delete.right=You have no right to delete the logs.
logs.title=Logs
logs.file.too.big=The log file is too big. Consider using log rotation for this log.
logs.error.title=Error log
logs.error.sentence=Last lines of the error log file
logs.admin.title=Admin log
logs.admin.sentence=Last lines of the admin log file
logs.admin.file.erase=Erase the admin log file
logs.admin.file.erase.confirm=Are you sure you want to erase the admin log file?
logs.admin.file.erase.ok=The admin log file has successfully been erased.
logs.admin.no.delete.right=You have no right to delete the logs.
upload.image.error.file.missing=File is missing
upload.image.error.file.bigger=The uploaded file exceeds the maximum file size allowed
Expand Down
32 changes: 28 additions & 4 deletions lizmap/modules/admin/templates/logs_view.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{meta_html js $j_basepath.'assets/js/admin/scroll_to_bottom_textarea.js', ['defer' => '']}

{jmessage_bootstrap}

<h1>{@admin~admin.menu.lizmap.logs.label@}</h1>
Expand Down Expand Up @@ -39,14 +41,36 @@


<div>
<h2>{@admin~admin.logs.error.title@}</h2>
<h2>{@admin~admin.logs.title@}</h2>

<h3>{@admin~admin.logs.admin.title@}</h3>

<p>{@admin~admin.logs.error.sentence@}</p>
<p>{@admin~admin.logs.admin.sentence@} : <code>{$lizmapLogBaseName}</code></p>

<textarea rows="10" style="width:90%;">{if $errorLog != 'toobig'}{$errorLog}{else}{@admin~admin.logs.error.file.too.big@}{/if}</textarea>
<textarea id="lizmap-admin-log" rows="{$lizmapLogTextArea}" style="width:90%;">
{if $lizmapLog != 'toobig'}
{$lizmapLog}
{else}
{@admin~admin.logs.file.too.big@}
{/if}
</textarea>
{ifacl2 'lizmap.admin.lizmap.log.delete'}
<div class="form-actions">
<a class="btn" href="{jurl 'admin~logs:eraseError'}" onclick="return confirm(`{@admin~admin.logs.error.file.erase.confirm@}`);">{@admin~admin.logs.error.file.erase@}</a>
<a class="btn" href="{jurl 'admin~logs:eraseError'}" onclick="return confirm(`{@admin~admin.logs.admin.file.erase.confirm@}`);">{@admin~admin.logs.admin.file.erase@}</a>
</div>
{/ifacl2}

{if $errorLogDisplay}
<h3>{@admin~admin.logs.error.title@}</h3>

<p>{@admin~admin.logs.error.sentence@} : <code>{$errorLogBaseName}</code></p>

<textarea id="lizmap-error-log" rows="{$errorLogTextArea}"" style="width:90%;">
{if $errorLog != 'toobig'}
{$errorLog}
{else}
{@admin~admin.logs.file.too.big@}
{/if}
</textarea>
{/if}
</div>
9 changes: 9 additions & 0 deletions lizmap/www/assets/js/admin/scroll_to_bottom_textarea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
window.onload = () => {
let textarea = document.getElementById('lizmap-admin-log');
textarea.scrollTop = textarea.scrollHeight;

textarea = document.getElementById('lizmap-error-log');
if (textarea) {
textarea.scrollTop = textarea.scrollHeight;
}
};

0 comments on commit a4d162e

Please sign in to comment.