From dec51953403df1c9182923487e1ff50015e9e691 Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Jun 07 2018 14:24:43 +0000
Subject: Ticket 49745 - UI add filter options for error log severity levels


Description:  Add a filtering option for error logging that allows
              for filtering log content by severity levels.  This includes
              a generic option for all "errors" and all "info messages"

https://pagure.io/389-ds-base/issue/49745

Reviewed by: spichugi(Thanks!)

---

diff --git a/src/cockpit/389-console/js/monitor.js b/src/cockpit/389-console/js/monitor.js
index 16621b7..ed9d26f 100644
--- a/src/cockpit/389-console/js/monitor.js
+++ b/src/cockpit/389-console/js/monitor.js
@@ -1,7 +1,29 @@
+
 var accesslog_cont_refresh;
 var auditlog_cont_refresh;
 var auditfaillog_cont_refresh;
-var erorrslog_cont_refresh;
+var errorslog_cont_refresh;
+
+var sev_emerg =  " - EMERG - ";
+var sev_crit =   " - CRIT - ";
+var sev_alert =  " - ALERT - ";
+var sev_err =    " - ERR - ";
+var sev_warn =   " - WARN - ";
+var sev_notice = " - NOTICE - ";
+var sev_info =   " - INFO - ";
+var sev_debug =  " - DEBUG - ";
+var sev_levels = {"Emergency": sev_emerg,
+                  "Critical": sev_crit,
+                  "Alert": sev_alert,
+                  "Error": sev_err,
+                  "Warning": sev_warn,
+                  "Notice": sev_notice,
+                  "Info": sev_info,
+                  "Debug": sev_debug
+                 };
+var sev_all_errs = [sev_emerg, sev_crit, sev_alert, sev_err];
+var sev_all_info = [sev_warn, sev_notice, sev_info, sev_debug];
+
 
 function gen_ratio_chart(ratio, chart ) {
   var c3ChartDefaults = patternfly.c3ChartDefaults();
@@ -68,18 +90,18 @@ function gen_util_chart(used, maxsize, hitratio, chart ) {
 function refresh_access_log () {
   var access_log = "/var/log/dirsrv/" + server_id + "/access";  // TODO - get actual log location from config
   var lines = $("#accesslog-lines").val();
-  var logging = cockpit.spawn(["tail", "-" + lines, access_log], 
+  var logging = cockpit.spawn(["tail", "-" + lines, access_log],
                               { "superuser": "try" }).done(function(data) {
-    $("#accesslog-area").text(data); 
+    $("#accesslog-area").text(data);
   });
 }
 
 function refresh_audit_log () {
   var audit_log = "/var/log/dirsrv/" + server_id + "/audit";  // TODO - get actual log location from config
   var lines = $("#auditlog-lines").val();
-  var logging = cockpit.spawn(["tail", "-" + lines, audit_log], 
+  var logging = cockpit.spawn(["tail", "-" + lines, audit_log],
                               { "superuser": "try" }).done(function(data) {
-    $("#auditlog-area").text(data); 
+    $("#auditlog-area").text(data);
   });
 }
 
@@ -95,10 +117,36 @@ function refresh_auditfail_log () {
 function refresh_errors_log () {
   var errors_log = "/var/log/dirsrv/" + server_id + "/errors";  // TODO - get actual log location from config
   var lines = $("#errorslog-lines").val();
-  var logging = cockpit.spawn(["tail", "-" + lines, errors_log], 
+  var sev_level = $("#errorslog-sev-level").val();
+  var logging = cockpit.spawn(["tail", "-" + lines, errors_log],
                               { "superuser": "try" }).done(function(data) {
+    if (sev_level != "Everything"){
+      // Filter Data
+      var lines = data.split('\n');
+      var new_data = "";
+      for (var i = 0; i < lines.length; i++){
+        var line = "";
+        if (sev_level == "Error Messages"){
+          for (var lev = 0; lev < sev_all_errs.length; lev++) {
+            if (lines[i].indexOf(sev_all_errs[lev]) != -1){
+              line = lines[i] + "\n";
+            }
+          }
+        } else if (sev_level == "Info Messages"){
+          for (var lev = 0; lev < sev_all_info.length; lev++) {
+            if (lines[i].indexOf(sev_all_info[lev]) != -1){
+              line = lines[i] + "\n";
+            }
+          }
+        } else if (lines[i].indexOf(sev_levels[sev_level]) != -1){
+          line = lines[i] + "\n";
+        }
+        // Add the filtered line to new data
+        new_data += line;
+      }
+      data = new_data;
+    }
     $("#errorslog-area").text(data);
-    
   });
 }
 
@@ -171,7 +219,6 @@ $(document).ready( function() {
       $("#monitor-log-errors-page").show();
     });
 
-
     $("#accesslog-refresh-btn").on('click', function () {
       refresh_access_log();
     });
@@ -306,6 +353,11 @@ $(document).ready( function() {
       }
     });
 
+    // Refresh page after changing severity level
+    $("#errorslog-sev-level").on("change", function() {
+       refresh_errors_log();
+    });
+
     $(document).on('click', '.repl-detail-btn', function(e) {
       e.preventDefault();
       var data = monitor_repl_table.row( $(this).parents('tr') ).data();
diff --git a/src/cockpit/389-console/monitor.html b/src/cockpit/389-console/monitor.html
index 440f97e..0be0929 100644
--- a/src/cockpit/389-console/monitor.html
+++ b/src/cockpit/389-console/monitor.html
@@ -589,7 +589,7 @@
     <h3>Errors Log</h3>
     <hr>
     <label for="errorslog-lines"> Log Lines To Show</label><select
-      class="btn btn-default dropdown ds-log-dropdown" id="errorslog-lines">
+      class="btn btn-default dropdown ds-left-margin" id="errorslog-lines">
       <option>50</option>
       <option>100</option>
       <option>200</option>
@@ -601,7 +601,24 @@
       <option>5000</option>
       <option>10000</option>
     </select><button id="errorslog-refresh-btn" class="ds-adj-btn">Refresh</button><label
-      class="ds-left-margin">Continuously Refresh<input type="checkbox" class="ds-sm-left-margin" id="errorslog-cont-refresh"></label>
+      class="ds-left-margin">Continuously Refresh<input type="checkbox" class="ds-sm-left-margin" id="errorslog-cont-refresh"></label><div 
+      class="dropdown ds-float-right"><label 
+      for="errorslog-sev-level">Filter Logging By Severity Level</label><select
+        class="btn btn-default dropdown ds-left-margin" id="errorslog-sev-level">
+          <option>Everything</option>
+          <option>Error Messages</option>
+          <option>Info Messages</option>
+          <option disabled>──────────</option>
+          <option>Emergency</option>
+          <option>Alert</option>
+          <option>Critical</option>
+          <option>Error</option>
+          <option>Warning</option>
+          <option>Notice</option>
+          <option>Info</option>
+          <option>Debug</option>
+        </select>
+      </div>
     <textarea id="errorslog-area" class="ds-logarea"></textarea>
     <p></p>
   </div>

