Improve logger to use custom timestamps.

This commit is contained in:
Vgr E.Barry 2015-01-04 17:05:53 -05:00
parent 20eda8c5d1
commit 4cc26e3ac2
2 changed files with 14 additions and 6 deletions

View File

@ -29,3 +29,6 @@ ALT_CHANNELS = ""
ALLOWED_ALT_CHANNELS_COMMANDS = []
USE_UTC = True # if True, will use the UTC time, else the local time
# {tzname} and {tzoffset} can both be used; respectively the timezone name (like UTC) and offset in the form +0000
# %Y is the year with century, %y is the year without, %m is the month, %d the day, %H hour, %M minute and %S seconds
TIMESTAMP_FORMAT = "[%Y-%m-%d %H:%M:%S{tzoffset}]"

View File

@ -5,14 +5,19 @@ import time
def get_timestamp():
"""Returns a timestamp with timezone + offset from UTC."""
if botconfig.USE_UTC:
return datetime.datetime.utcnow().strftime("[%Y-%m-%d] (%H:%M:%S) UTC +0000 ")
tmf = datetime.datetime.utcnow().strftime(botconfig.TIMESTAMP_FORMAT)
if tmf[-1] != " ":
tmf += " "
return tmf.format(tzname="UTC", tzoffset="+0000")
tmf = time.strftime(botconfig.TIMESTAMP_FORMAT)
if tmf[-1] != " ":
tmf += " "
tz = time.strftime("%Z")
utctime = datetime.datetime.utcnow().strftime("%H")
nowtime = datetime.datetime.now().strftime("%H")
offset = "+" if int(utctime) > int(nowtime) else "-"
tz = str(time.timezone // 36)
if len(tz) == 3:
tz = "0" + tz
return time.strftime("[%Y-%m-%d] (%H:%M:%S) %Z {0}{1} ").upper().format(offset, tz)
offset = "-" if int(utctime) > int(nowtime) else "+"
offset += str(time.timezone // 36).zfill(4)
return tmf.format(tzname=tz, tzoffset=offset).upper()
def logger(file, write=True, display=True):
def log(*output, write=write, display=display):