From d8a5926a1d2df9441688e1584128fe1113a29f6d Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Mon, 13 Jan 2025 09:59:44 +0100 Subject: [PATCH] Add error log panel in admin --- .../admin/controllers/logs.classic.php | 53 ++++++++++++++++++- .../locales/en_US/admin.UTF-8.properties | 18 ++++--- lizmap/modules/admin/templates/logs_view.tpl | 32 +++++++++-- .../js/admin/scroll_to_bottom_textarea.js | 9 ++++ 4 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 lizmap/www/assets/js/admin/scroll_to_bottom_textarea.js diff --git a/lizmap/modules/admin/controllers/logs.classic.php b/lizmap/modules/admin/controllers/logs.classic.php index 4b26c9d6cc..aae29454c6 100644 --- a/lizmap/modules/admin/controllers/logs.classic.php +++ b/lizmap/modules/admin/controllers/logs.classic.php @@ -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')); @@ -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. * diff --git a/lizmap/modules/admin/locales/en_US/admin.UTF-8.properties b/lizmap/modules/admin/locales/en_US/admin.UTF-8.properties index 9a31a100aa..36efc8ef5e 100644 --- a/lizmap/modules/admin/locales/en_US/admin.UTF-8.properties +++ b/lizmap/modules/admin/locales/en_US/admin.UTF-8.properties @@ -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 diff --git a/lizmap/modules/admin/templates/logs_view.tpl b/lizmap/modules/admin/templates/logs_view.tpl index 996bf24d5f..dcd6183285 100644 --- a/lizmap/modules/admin/templates/logs_view.tpl +++ b/lizmap/modules/admin/templates/logs_view.tpl @@ -1,3 +1,5 @@ +{meta_html js $j_basepath.'assets/js/admin/scroll_to_bottom_textarea.js', ['defer' => '']} + {jmessage_bootstrap}

{@admin~admin.menu.lizmap.logs.label@}

@@ -39,14 +41,36 @@
-

{@admin~admin.logs.error.title@}

+

{@admin~admin.logs.title@}

+ +

{@admin~admin.logs.admin.title@}

-

{@admin~admin.logs.error.sentence@}

+

{@admin~admin.logs.admin.sentence@} : {$lizmapLogBaseName}

- + {ifacl2 'lizmap.admin.lizmap.log.delete'} {/ifacl2} + + {if $errorLogDisplay} +

{@admin~admin.logs.error.title@}

+ +

{@admin~admin.logs.error.sentence@} : {$errorLogBaseName}

+ + + {/if}
diff --git a/lizmap/www/assets/js/admin/scroll_to_bottom_textarea.js b/lizmap/www/assets/js/admin/scroll_to_bottom_textarea.js new file mode 100644 index 0000000000..4458e76b3c --- /dev/null +++ b/lizmap/www/assets/js/admin/scroll_to_bottom_textarea.js @@ -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; + } +};