now logs in iso8601 datetime format, with offset/Z

This commit is contained in:
Jeffrey Paul 2019-08-27 18:08:55 +02:00
parent c52c640c6c
commit f81d9e39bd
3 changed files with 28 additions and 7 deletions

View File

@ -8,4 +8,4 @@ log.error("something went wrong.")
log.die("bailing out") # exits log.die("bailing out") # exits
print "no soup for you." print("no soup for you.")

View File

@ -2,7 +2,11 @@
#234567891123456789212345678931234567894123456789512345678961234567897123456789 #234567891123456789212345678931234567894123456789512345678961234567897123456789
# encoding: utf-8 # encoding: utf-8
from pytz import reference
import time
import colorlog import colorlog
import datetime
import logging import logging
import os import os
import sys import sys
@ -16,19 +20,34 @@ ch = logging.StreamHandler() # defaults to sys.stderr
log.addHandler(ch) log.addHandler(ch)
formatstr = '%(asctime)s [%(levelname)-.4s] %(message)s'
formatstr = '%(asctime)s.%(msecs)03d [%(levelname)-.4s] %(message)s'
datefmt = '%Y-%m-%dT%H:%M:%S'
localtime = reference.LocalTimezone()
if localtime.tzname(datetime.datetime.now()) == 'UTC':
# UTC time, just append Z
formatstr = '%(asctime)s.%(msecs)03dZ [%(levelname)-.4s] %(message)s'
else:
# not UTC, append offset of local system time
minute = (time.localtime().tm_gmtoff / 60) % 60
hour = ((time.localtime().tm_gmtoff / 60) - minute) / 60
utcoffset = "%.2d%.2d" %(hour, minute)
if utcoffset[0] != '-':
utcoffset = '+' + utcoffset
formatstr = '%(asctime)s.%(msecs)03d' + utcoffset + ' [%(levelname)-.4s] %(message)s'
colorFormatter = colorlog.ColoredFormatter( colorFormatter = colorlog.ColoredFormatter(
'%(log_color)s' + formatstr fmt='%(log_color)s' + formatstr, datefmt=datefmt
) )
formatter = logging.Formatter( formatter = logging.Formatter(
formatstr fmt=formatstr,
datefmt=datefmt
) )
log.notice = log.info log.notice = log.info
if sys.stdout.isatty(): if sys.stdout.isatty():
ch.setFormatter(colorFormatter) ch.setFormatter(colorFormatter)
else: else:
@ -39,7 +58,7 @@ if os.environ.get('LOG_TO_SYSLOG',False):
# default to UDP if no socket found # default to UDP if no socket found
address = ('localhost', 514) address = ('localhost', 514)
from logging.handlers import SysLogHandler from logging.handlers import SysLogHandler
locations = [ locations = [

View File

@ -11,5 +11,7 @@ setup(
description='Python logging for humans', description='Python logging for humans',
install_requires=[ install_requires=[
"colorlog", "colorlog",
"pytz",
"datetime"
], ],
) )