-
Notifications
You must be signed in to change notification settings - Fork 66
Open
Description
I have an agent that sometimes detects elements that need to be logged "into the past".
python-logging-loki reuses the python-logging mechanism, so I tried adding a timestamp filter as specifief in the stackoverflow message (see link in source code).
Can one 'tweak' the datetime on which a log line is sent to loki?
import logging
import logging_loki
import time
# https://stackoverflow.com/questions/47146514/python-logging-using-custom-timestamps-not-current-timestamp
class TimestampFilter (logging.Filter):
"""
This is a logging filter which will check for a `timestamp` attribute on a
given LogRecord, and if present it will override the LogRecord creation time
to be that of the timestamp (specified as a time.time()-style value).
This allows one to override the date/time output for log entries by specifying
`timestamp` in the `extra` option to the logging call.
"""
def filter(self, record):
if hasattr(record, 'timestamp'):
record.created = record.timestamp
return True
# --- Standard initialization
handler = logging_loki.LokiHandler(
url="http://git-lapp202.gtos.psa:3100/loki/api/v1/push",
tags={"application": "my-app"},
version="1",
)
logger = logging.getLogger("my-logger")
logger.addHandler(handler)
# --- Add timestampfilter ...
filter = TimestampFilter()
logger.addFilter(filter)
# --- Now log a record without time
logger.error( "Logging at actual time", extra={"tags": {"service": "my-service"}},)
# --- Now log a record 15 minutes ago
my_timestamp = time.time() -900
logger.error( "Logging in the past", extra={"tags": {"service": "my-service", 'timestamp' : my_timestamp}},)
Metadata
Metadata
Assignees
Labels
No labels