logs errors to kibana

This commit is contained in:
Konstantin Lampalzer 2021-01-24 20:44:37 +01:00
parent dfb66d5383
commit 4558269795
No known key found for this signature in database
GPG key ID: 9A60A522835A2AD9
2 changed files with 11 additions and 12 deletions

View file

@ -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}!")

View file

@ -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()