]> git.ipfire.org Git - thirdparty/suricata-update.git/commitdiff
Separate log messages to stderr and stdout
authorVagisha Gupta <vagishagupta23@gmail.com>
Mon, 5 Aug 2019 09:05:55 +0000 (14:35 +0530)
committerJason Ish <jason.ish@oisf.net>
Wed, 28 Aug 2019 22:10:47 +0000 (16:10 -0600)
Currently, all the messages in suricata-update are logged to stderr.
Changes are made to split between log messages where regular
output (INFO, DEBUG) goes to stdout whereas ERRORS, WARNINGS and
CRITICAL messages goes to stderr.

suricata/update/loghandler.py
suricata/update/main.py

index d98577767478f94dde538ef28767b8da106a259a..ed6356959fe30d752c4acb805da9f9bf19b09648 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
+import sys
 import logging
 import time
 
 # A list of secrets that will be replaced in the log output.
 secrets = {}
 
+
 def add_secret(secret, replacement):
     """Register a secret to be masked. The secret will be replaced with:
            <replacement>
     """
     secrets[str(secret)] = str(replacement)
 
+
 class SuriColourLogHandler(logging.StreamHandler):
     """An alternative stream log handler that logs with Suricata inspired
     log colours."""
@@ -77,3 +80,29 @@ class SuriColourLogHandler(logging.StreamHandler):
         for secret in secrets:
             msg = msg.replace(secret, "<%s>" % secrets[secret])
         return msg
+
+
+class LessThanFilter(logging.Filter):
+    def __init__(self, exclusive_maximum, name=""):
+        super(LessThanFilter, self).__init__(name)
+        self.max_level = exclusive_maximum
+
+    def filter(self, record):
+        return 1 if record.levelno < self.max_level else 0
+
+
+def configure_logging():
+    if os.fstat(sys.stdout.fileno()) == os.fstat(sys.stderr.fileno()):
+        filter_stdout = True
+    else:
+        filter_stdout = False
+    logger = logging.getLogger()
+    logger.setLevel(logging.NOTSET)
+    logging_handler_out = SuriColourLogHandler(sys.stdout)
+    logging_handler_out.setLevel(logging.DEBUG)
+    if filter_stdout:
+        logging_handler_out.addFilter(LessThanFilter(logging.WARNING))
+    logger.addHandler(logging_handler_out)
+    logging_handler_err = SuriColourLogHandler(sys.stderr)
+    logging_handler_err.setLevel(logging.WARNING)
+    logger.addHandler(logging_handler_err)
index cd4afc087407602b86e72f6fda54702683aa9253..ebce0b6d5fd1e8a61a2a7d2275005466c5e5f505 100644 (file)
@@ -73,8 +73,8 @@ except:
 # Initialize logging, use colour if on a tty.
 if len(logging.root.handlers) == 0 and os.isatty(sys.stderr.fileno()):
     logger = logging.getLogger()
+    suricata.update.loghandler.configure_logging()
     logger.setLevel(level=logging.INFO)
-    logger.addHandler(suricata.update.loghandler.SuriColourLogHandler())
 else:
     logging.basicConfig(
         level=logging.INFO,