xente / loki-logger-handler

A logging handler that sends log messages to Loki in JSON format
MIT License
17 stars 10 forks source link

Handler should use self.formatter instead of self.logger_formatter #14

Open Liquidmasl opened 1 day ago

Liquidmasl commented 1 day ago

logging.Handler already has self.formatter and a setter method for it, but the setter method does nothing because the lokiLoggingHander does use its own formatter variable.

This just lead to a lot of confusion


class LokiLoggerHandler(logging.Handler):
    def __init__(
        self,
        url,
        labels,
        labelKeys=None,
        timeout=10,
        compressed=True,
        defaultFormatter=LoggerFormatter(),
        additional_headers=dict()
    ):
        super().__init__()

        if labelKeys is None:
            labelKeys = {}

        self.labels = labels
        self.labelKeys = labelKeys
        self.timeout = timeout
        self.logger_formatter = defaultFormatter  # This should be just self.formatter
        self.request = LokiRequest(url=url, compressed=compressed, additional_headers=additional_headers)
        self.buffer = queue.Queue()
        self.flush_thread = threading.Thread(target=self._flush, daemon=True)
        self.flush_thread.start()

just changing that one variable decleration makes it easier for the logger to be a drop in replacement for another handler

Liquidmasl commented 1 day ago

ah well, and in emit the format call should change as well of course

Liquidmasl commented 1 day ago

ah well, and in emit the format call should change as well of course