File size: 951 Bytes
cb9e677 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import datetime
import logging
import sys
import time
class DeltaTimeFormatter(logging.Formatter):
def format(self, record):
delta = datetime.timedelta(
seconds=int(record.relativeCreated / 1000)
) # no milliseconds
record.delta = delta
return super().format(record)
def set_logger(level: int = logging.INFO):
root = logging.getLogger()
root.handlers.clear()
root.setLevel(level)
tz, *_ = time.tzname
LOGFORMAT = "%(asctime)s - %(delta)s - %(name)s - %(levelname)s - %(message)s"
TIMEFORMAT = f"%Y-%m-%d %H:%M:%S ({tz})"
formatter = DeltaTimeFormatter(LOGFORMAT, TIMEFORMAT)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(level)
handler.setFormatter(formatter)
root.addHandler(handler)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.WARNING)
handler.setFormatter(formatter)
root.addHandler(handler)
|