Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions logging_loki/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def __init__(self,
props_to_labels: Optional[list[str]] = None,
level_tag: Optional[str] = const.level_tag,
logger_tag: Optional[str] = const.logger_tag,
verify: Union[bool, str] = True
verify: Union[bool, str] = True,
replace_timestamp: Optional[bool] = False,
):
"""
Create new Loki emitter.
Expand Down Expand Up @@ -75,6 +76,8 @@ def __init__(self,
self.logger_tag: str = logger_tag
# verify param to be past to requests, can be a bool (to enable/disable SSL verification) or a path to a CA bundle
self.verify = verify
# Flag to control if the log timestamp should be replaced with the current time
self.replace_timestamp = replace_timestamp

self._session: Optional[requests.Session] = None
self._lock = threading.Lock()
Expand Down Expand Up @@ -147,8 +150,7 @@ def build_tags(self, record: logging.LogRecord, line: str) -> Dict[str, Any]:
def build_payload(self, record: logging.LogRecord, line: str) -> dict:
"""Build JSON payload with a log entry."""
labels = self.build_tags(record, line)
ns = 1e9
ts = str(int(time.time() * ns))
ts = str(time.time_ns()) if self.replace_timestamp else str(int(record.created * 1e9))

line = json.dumps(record, default=lambda obj: obj.__dict__) if self.as_json else line

Expand Down
7 changes: 4 additions & 3 deletions logging_loki/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(
props_to_labels: Optional[list[str]] = None,
level_tag: Optional[str] = const.level_tag,
logger_tag: Optional[str] = const.logger_tag,
verify: Union[bool, str] = True
verify: Union[bool, str] = True,
replace_timestamp: Optional[bool] = False,
):
"""
Create new Loki logging handler.
Expand All @@ -86,10 +87,10 @@ def __init__(
level_tag: Label name indicating logging level.
logger_tag: Label name indicating logger name.
verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use.

replace_timestamp: If enabled, the timestamp in the log line will be replaced with `time.time_ns()`. Be careful when using this option with `batching` enabled, as the logs will be sent in batches, and the timestamp will be the time of the batch, not the time of the log.
"""
super().__init__()
self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag, verify)
self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag, verify, replace_timestamp=replace_timestamp)

def handleError(self, exc: Exception): # noqa: N802
"""Close emitter and let default handler take actions on error."""
Expand Down
Loading