diff --git a/.idea/compLIB.iml b/.idea/compLIB.iml index e2d46e8..4a206a1 100644 --- a/.idea/compLIB.iml +++ b/.idea/compLIB.iml @@ -2,10 +2,9 @@ - - + diff --git a/.idea/misc.xml b/.idea/misc.xml index a2e120d..1dae23d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/compLib/LogstashLogging.py b/compLib/LogstashLogging.py new file mode 100644 index 0000000..5e417b3 --- /dev/null +++ b/compLib/LogstashLogging.py @@ -0,0 +1,72 @@ +import logging +import sys +import requests + +from logstash_async.transport import HttpTransport +from logstash_async.handler import AsynchronousLogstashHandler + +host = 'logs.robo4you.at' +port = 443 + +logstash_logger = logging.getLogger('logstash') +logstash_logger.setLevel(logging.INFO) + +transport = HttpTransport( + host, + port, + username="robo", + password="competition", + timeout=10.0, +) + +asynchronousLogstashHandler = AsynchronousLogstashHandler( + host, + port, + transport=transport, + database_path='logstash_test.db' +) + +class StreamToLogger(object): + """ + Fake file-like stream object that redirects writes to a logger instance. + """ + def __init__(self, log_level=logging.INFO): + self.logger = logging.getLogger('logstash') + self.console = sys.stdout + 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 + + def flush(self): + self.console.flush() + if self.linebuf != '': + self.logger.log(self.log_level, self.linebuf.rstrip()) + self.linebuf = '' + asynchronousLogstashHandler.flush() + +try: + requests.head(f"http://{host}:{port}") + logstash_logger.addHandler(asynchronousLogstashHandler) + + sl = StreamToLogger(logging.INFO) + sys.stdout = sl + +except requests.exceptions.ConnectionError as identifier: + print(f"Could not connect to {host}!") + pass + + + + + + + diff --git a/compLib/__init__.py b/compLib/__init__.py index a0235ce..8afaf7d 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -1 +1,6 @@ -__version__ = "0.0.2" \ No newline at end of file +__version__ = "0.0.2" + +import compLib.LogstashLogging +import logging + +print("Starting compLib...") \ No newline at end of file