Skip to content

Commit

Permalink
Format log timestamp like Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
bersace committed Jul 22, 2024
1 parent e14b68a commit 50d9ea8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions ui/temboardui/toolkit/log.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import logging
import sys
import time
from logging.config import dictConfig
from logging.handlers import SysLogHandler

Expand Down Expand Up @@ -132,17 +133,12 @@ def generate_logging_config(
stderr_handler = "logging.StreamHandler"
if sys.stderr.isatty():
stderr_handler = __name__ + ".ColoredStreamHandler"
timestamp = "%(asctime)s "
datefmt = "%H:%M:%S"
else:
# strftime does not support milliseconds. Modifying datefmt disables Python hack
# to append milliseconds to timestamp. Thus, hardcode timezone in message format
# rather than datefmt.
timestamp = "%(asctime)s " + localoffset() + " "
datefmt = None

minimal_fmt = "%(levelname)s: %(lastname)s: %(message)s"
verbose_fmt = timestamp + core + "[%(process)d] " + minimal_fmt
verbose_fmt = "%(asctime)s " + core + "[%(process)d] " + minimal_fmt
syslog_fmt = core + "[%(process)d] %(levelname)s: %(lastname)s: %(message)s"

logging_config = {
Expand All @@ -157,7 +153,7 @@ def generate_logging_config(
},
"dated_syslog": {
"()": __name__ + ".MultilineFormatter",
"format": timestamp + syslog_fmt,
"format": "%(asctime)s " + syslog_fmt,
},
"syslog": {"()": __name__ + ".MultilineFormatter", "format": syslog_fmt},
"systemd": {
Expand Down Expand Up @@ -193,3 +189,9 @@ def localoffset():
hours = offset.seconds // 3600
minutes = (offset.seconds // 60) % 60
return "+%02d%02d" % (hours, minutes)


# Make logging format consistent with Prometheus ts= format.
logging.Formatter.converter = time.gmtime
logging.Formatter.default_time_format = "%Y-%m-%dT%H:%M:%S"
logging.Formatter.default_msec_format = "%s.%03dZ" # Append UTC timezone.

0 comments on commit 50d9ea8

Please sign in to comment.