Skip to content

Commit

Permalink
Added enable_self_errors attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
xente committed Dec 23, 2024
1 parent a16b7f3 commit 4289638
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ A logging handler that sends log messages to **(Grafana) Loki** in JSON format.
* compressed (bool, optional): Whether to compress the log messages before sending them to Loki. Defaults to True.
* loguru (bool, optional): Whether to use `LoguruFormatter`. Defaults to False.
* default_formatter (logging.Formatter, optional): Formatter for the log records. If not provided,`LoggerFormatter` or `LoguruFormatter` will be used.
* enable_self_errors (bool, optional): Set to True to show Hanlder errors on console. Defaults to False

## Formatters
* **LoggerFormatter**: Formatter for default python logging implementation
Expand Down
41 changes: 29 additions & 12 deletions loki_logger_handler/loki_logger_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

# Compatibility for Python 2 and 3 queue module
try:
import queue # Python 3.x
Expand All @@ -9,8 +7,6 @@
import atexit
import logging
import threading
import time

import requests

from loki_logger_handler.formatters.logger_formatter import LoggerFormatter
Expand Down Expand Up @@ -43,7 +39,8 @@ def __init__(
message_in_json_format=True,
timeout=10,
compressed=True,
default_formatter=LoggerFormatter()
default_formatter=LoggerFormatter(),
enable_self_errors=False
):
"""
Initialize the LokiLoggerHandler object.
Expand All @@ -58,13 +55,25 @@ def __init__(
compressed (bool, optional): Whether to compress the logs using gzip. Defaults to True.
default_formatter (logging.Formatter, optional): Formatter for the log records. If not provided,
LoggerFormatter or LoguruFormatter will be used.
enable_self_errors (bool, optional): Set to
True to show Hanlder errors on console. Default False
"""
super(LokiLoggerHandler, self).__init__()

self.labels = labels
self.label_keys = label_keys if label_keys is not None else {}
self.timeout = timeout
self.formatter = default_formatter

self.enable_self_errors = enable_self_errors

# Create a logger for self-errors if enabled
if self.enable_self_errors:
self.debug_logger = logging.getLogger("LokiHandlerDebug")
self.debug_logger.setLevel(logging.ERROR)
console_handler = logging.StreamHandler()
self.debug_logger.addHandler(console_handler)

self.request = LokiRequest(
url=url, compressed=compressed, additional_headers=additional_headers or {}
)
Expand All @@ -79,7 +88,10 @@ def __init__(

self.message_in_json_format = message_in_json_format

self.send_error = None
#Halndler working with errors
self.error = False



def emit(self, record):
"""
Expand All @@ -91,8 +103,10 @@ def emit(self, record):
try:
formatted_record = self.formatter.format(record)
self._put(formatted_record)
except Exception:
pass # Silently ignore any exceptions
except Exception as e:
if self.enable_self_errors:
self.debug_logger.error("Unexpected error: %s",e, exc_info=True)
self.error = True

def _flush(self):
"""
Expand All @@ -112,9 +126,10 @@ def _flush(self):
if not self.buffer.empty():
try:
self._send()
except Exception:
pass # Silently ignore any exceptions

except Exception as e:
if self.enable_self_errors:
self.debug_logger.error("Unexpected error: %s",e, exc_info=True)
self.error = True

def _send(self):
"""
Expand All @@ -135,7 +150,9 @@ def _send(self):
try:
self.request.send(streams.serialize())
except requests.RequestException as e:
self.send_error = e
if self.enable_self_errors:
self.debug_logger.error("Unexpected error: %s",e, exc_info=True)
self.error = True

def write(self, message):
"""
Expand Down
14 changes: 5 additions & 9 deletions loki_logger_handler/loki_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ def send(self, data):
response.raise_for_status()

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: {}, "
"response text: {}, "
"post request URL: {}\n".format(
response.status_code, response.text, response.request.url
)
)
raise e
response_message= f"Response status code: {response.status_code}, response text: {response.text}, post request URL: {response.request.url}"
raise requests.RequestException(f"Error while sending logs: {str(e)}\nCaptured error details:\n{response_message}") from e

raise requests.RequestException(f"Error while sending logs: {str(e)}") from e


finally:
if response:
Expand Down

0 comments on commit 4289638

Please sign in to comment.