diff --git a/loki_logger_handler/loki_logger_handler.py b/loki_logger_handler/loki_logger_handler.py index 9585f34..0d230c9 100644 --- a/loki_logger_handler/loki_logger_handler.py +++ b/loki_logger_handler/loki_logger_handler.py @@ -6,14 +6,17 @@ except ImportError: import Queue as queue # Python 2.7 +import atexit +import logging import threading import time -import logging -import atexit + +import requests + from loki_logger_handler.formatters.logger_formatter import LoggerFormatter -from loki_logger_handler.streams import Streams from loki_logger_handler.loki_request import LokiRequest from loki_logger_handler.stream import Stream +from loki_logger_handler.streams import Streams class LokiLoggerHandler(logging.Handler): @@ -62,16 +65,20 @@ def __init__( self.label_keys = label_keys if label_keys is not None else {} self.timeout = timeout self.formatter = default_formatter - self.request = LokiRequest(url=url, compressed=compressed, additional_headers=additional_headers or {}) + self.request = LokiRequest( + url=url, compressed=compressed, additional_headers=additional_headers or {} + ) self.buffer = queue.Queue() self.flush_thread = threading.Thread(target=self._flush) - + # Set daemon for Python 2 and 3 compatibility self.flush_thread.daemon = True self.flush_thread.start() - + self.message_in_json_format = message_in_json_format + self.send_error = None + def emit(self, record): """ Emit a log record. @@ -117,7 +124,10 @@ def _send(self): if temp_streams: streams = Streams(list(temp_streams.values())) - self.request.send(streams.serialize()) + try: + self.request.send(streams.serialize()) + except requests.RequestException as e: + self.send_error = e def write(self, message): """ diff --git a/loki_logger_handler/loki_request.py b/loki_logger_handler/loki_request.py index eb178a9..6eba0fb 100644 --- a/loki_logger_handler/loki_request.py +++ b/loki_logger_handler/loki_request.py @@ -18,6 +18,7 @@ class LokiRequest: headers (dict): Additional headers to include in the request. session (requests.Session): The session used for making HTTP requests. """ + def __init__(self, url, compressed=False, additional_headers=None): """ Initialize the LokiRequest object with the server URL, compression option, and additional headers. @@ -49,7 +50,7 @@ def send(self, data): if self.compressed: self.headers["Content-Encoding"] = "gzip" buf = IO() - with gzip.GzipFile(fileobj=buf, mode='wb') as f: + with gzip.GzipFile(fileobj=buf, mode="wb") as f: f.write(data.encode("utf-8")) data = buf.getvalue() @@ -58,6 +59,7 @@ def send(self, data): except requests.RequestException as e: sys.stderr.write("Error while sending logs: {}\n".format(e)) + if response is not None: sys.stderr.write( "Response status code: {}, " @@ -66,6 +68,7 @@ def send(self, data): response.status_code, response.text, response.request.url ) ) + raise e finally: if response: