From: Vagisha Gupta Date: Mon, 5 Aug 2019 09:05:55 +0000 (+0530) Subject: Separate log messages to stderr and stdout X-Git-Tag: 1.1.0rc1~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e20542feddca24ec04b8382b9c8c03f62413717e;p=thirdparty%2Fsuricata-update.git Separate log messages to stderr and stdout 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. --- diff --git a/suricata/update/loghandler.py b/suricata/update/loghandler.py index d985777..ed63569 100644 --- a/suricata/update/loghandler.py +++ b/suricata/update/loghandler.py @@ -15,18 +15,21 @@ # 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: """ 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) diff --git a/suricata/update/main.py b/suricata/update/main.py index cd4afc0..ebce0b6 100644 --- a/suricata/update/main.py +++ b/suricata/update/main.py @@ -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,