Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
4d699181c3
2 changed files with 11 additions and 12 deletions
|
@ -31,27 +31,23 @@ class StreamToLogger(object):
|
||||||
"""
|
"""
|
||||||
Fake file-like stream object that redirects writes to a logger instance.
|
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.logger = logging.getLogger('logstash')
|
||||||
self.console = sys.stdout
|
self.console = textio
|
||||||
self.log_level = log_level
|
self.log_level = log_level
|
||||||
self.linebuf = ''
|
self.linebuf = ''
|
||||||
|
|
||||||
def write(self, buf):
|
def write(self, buf):
|
||||||
self.console.write(buf)
|
self.console.write(buf)
|
||||||
temp_linebuf = self.linebuf + buf
|
temp_linebuf = self.linebuf + buf
|
||||||
self.linebuf = ''
|
for line in buf.splitlines(True):
|
||||||
for line in temp_linebuf.splitlines(True):
|
self.linebuf += line
|
||||||
if line[-1] == '\n':
|
|
||||||
self.logger.log(self.log_level, line.rstrip())
|
|
||||||
else:
|
|
||||||
self.linebuf += line
|
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.console.flush()
|
|
||||||
if self.linebuf != '':
|
if self.linebuf != '':
|
||||||
self.logger.log(self.log_level, self.linebuf.rstrip())
|
self.logger.log(self.log_level, self.linebuf.rstrip())
|
||||||
self.linebuf = ''
|
self.linebuf = ''
|
||||||
|
self.console.flush()
|
||||||
asynchronousLogstashHandler.flush()
|
asynchronousLogstashHandler.flush()
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,8 +56,11 @@ try:
|
||||||
if r.status_code == 400:
|
if r.status_code == 400:
|
||||||
logstash_logger.addHandler(asynchronousLogstashHandler)
|
logstash_logger.addHandler(asynchronousLogstashHandler)
|
||||||
|
|
||||||
sl = StreamToLogger(logging.INFO)
|
so = StreamToLogger(sys.stdout, logging.INFO)
|
||||||
sys.stdout = sl
|
sys.stdout = so
|
||||||
|
|
||||||
|
se = StreamToLogger(sys.stderr, logging.ERROR)
|
||||||
|
sys.stderr = se
|
||||||
else:
|
else:
|
||||||
print(f"Could not connect to {host} -> {r.status_code}!")
|
print(f"Could not connect to {host} -> {r.status_code}!")
|
||||||
|
|
||||||
|
|
2
test.py
2
test.py
|
@ -17,9 +17,9 @@ except ImportError:
|
||||||
|
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
|
|
||||||
|
from compLib import LogstashLogging
|
||||||
from compLib import Api
|
from compLib import Api
|
||||||
|
|
||||||
|
|
||||||
class MyTestCase(unittest.TestCase):
|
class MyTestCase(unittest.TestCase):
|
||||||
def test_get_park(self):
|
def test_get_park(self):
|
||||||
ret = Api.Seeding.get_park()
|
ret = Api.Seeding.get_park()
|
||||||
|
|
Reference in a new issue