diff --git a/compLib/LogstashLogging.py b/compLib/LogstashLogging.py index 61418c4..e8f3957 100644 --- a/compLib/LogstashLogging.py +++ b/compLib/LogstashLogging.py @@ -31,27 +31,23 @@ class StreamToLogger(object): """ Fake file-like stream object that redirects writes to a logger instance. """ - def __init__(self, log_level=logging.INFO): + def __init__(self, textio, log_level=logging.INFO): self.logger = logging.getLogger('logstash') - self.console = sys.stdout + self.console = textio self.log_level = log_level self.linebuf = '' def write(self, buf): self.console.write(buf) temp_linebuf = self.linebuf + buf - self.linebuf = '' - for line in temp_linebuf.splitlines(True): - if line[-1] == '\n': - self.logger.log(self.log_level, line.rstrip()) - else: - self.linebuf += line + for line in buf.splitlines(True): + self.linebuf += line def flush(self): - self.console.flush() if self.linebuf != '': self.logger.log(self.log_level, self.linebuf.rstrip()) self.linebuf = '' + self.console.flush() asynchronousLogstashHandler.flush() @@ -60,8 +56,11 @@ try: if r.status_code == 400: logstash_logger.addHandler(asynchronousLogstashHandler) - sl = StreamToLogger(logging.INFO) - sys.stdout = sl + so = StreamToLogger(sys.stdout, logging.INFO) + sys.stdout = so + + se = StreamToLogger(sys.stderr, logging.ERROR) + sys.stderr = se else: print(f"Could not connect to {host} -> {r.status_code}!") diff --git a/test.py b/test.py index a5de56b..56a223e 100644 --- a/test.py +++ b/test.py @@ -17,9 +17,9 @@ except ImportError: from multiprocessing import Process +from compLib import LogstashLogging from compLib import Api - class MyTestCase(unittest.TestCase): def test_get_park(self): ret = Api.Seeding.get_park()