Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A logging handler that sends log messages to **(Grafana) Loki** in JSON format.
* timeout (int, optional): Timeout interval in seconds to wait before flushing the buffer. Defaults to 10 seconds.
* 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.
* default_formatter (loki_logger_handler.formatters.LogFormatter, 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
### Loki 3.0
* enable_structured_loki_metadata (bool, optional): Whether to include structured loki_metadata in the logs. Defaults to False. Only supported for Loki 3.0 and above
Expand Down
18 changes: 18 additions & 0 deletions loki_logger_handler/formatters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging
from abc import ABC, abstractmethod


class LogFormatter(ABC):
@abstractmethod
def format(self, record):
# type: (logging.LogRecord) -> dict | dict
"""
Format a log record into a structured dictionary.

Args:
record (logging.LogRecord): The log record to format.

Returns:
(tuple): A tuple of dictionary representation of the log record and the extracted loki metadata
"""
raise NotImplementedError
13 changes: 6 additions & 7 deletions loki_logger_handler/formatters/logger_formatter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import traceback
import time

class LoggerFormatter:
from loki_logger_handler.formatters import LogFormatter


class LoggerFormatter(LogFormatter):
"""
A custom formatter for log records that formats the log record into a structured dictionary.
A custom formatter for log records generated by the standard library logger, formatting the record into a structured dictionary.
"""
LOG_RECORD_FIELDS = {
"msg",
Expand All @@ -28,9 +30,6 @@ class LoggerFormatter:
"exc_text",
}

def __init__(self):
pass

def format(self, record):
"""
Format a log record into a structured dictionary.
Expand All @@ -39,7 +38,7 @@ def format(self, record):
record (logging.LogRecord): The log record to format.

Returns:
dict: A dictionary representation of the log record.
(tuple): A tuple of dictionary representation of the log record and the extracted loki metadata.
"""

formatted = {
Expand Down
12 changes: 6 additions & 6 deletions loki_logger_handler/formatters/loguru_formatter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import traceback
import sys

from loki_logger_handler.formatters import LogFormatter

class LoguruFormatter:

class LoguruFormatter(LogFormatter):
"""
A custom formatter for log records generated by Loguru, formatting the record into a structured dictionary.
"""
def __init__(self):
pass

def format(self, record):
"""
Format a Loguru log record into a structured dictionary.
Expand All @@ -17,7 +16,7 @@ def format(self, record):
record (dict): The Loguru log record to format.

Returns:
dict: A dictionary representation of the log record.
(tuple): A tuple of dictionary representation of the log record and the extracted loki metadata
"""
# Convert timestamp to a standard format across Python versions
timestamp = record.get("time")
Expand Down Expand Up @@ -65,7 +64,8 @@ def format(self, record):

return formatted, loki_metadata

def add_exception_details(self, record, formatted):
@staticmethod
def add_exception_details(record, formatted):
"""
Adds exception details to the formatted log record.

Expand Down
2 changes: 1 addition & 1 deletion loki_logger_handler/loki_logger_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
message_in_json_format (bool): Whether to format log values as JSON.
timeout (int, optional): Timeout interval for flushing logs. Defaults to 10 seconds.
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.
default_formatter (loki_logger_handler.formatters.LogFormatter, 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
enable_structured_loki_metadata (bool, optional): Whether to include structured loki_metadata in the logs. Defaults to False. Only supported for Loki 3.0 and above
loki_metadata (dict, optional): Default loki_metadata values. Defaults to None. Only supported for Loki 3.0 and above
Expand Down