diff options
author | Bjoern Riemer <bjoern.riemer@tu-berlin.de> | 2016-01-13 12:21:39 +0100 |
---|---|---|
committer | Bjoern Riemer <bjoern.riemer@tu-berlin.de> | 2016-01-13 12:21:39 +0100 |
commit | 4eb1cf8ae0ddb19514b789199cc195f811702975 (patch) | |
tree | 7c56dcfab6f325eb65a1cd09ad89ff2b4fea614a /voctocore/lib | |
parent | 8674a29f80e8dd6c8bb865274ae2a27932c82bc3 (diff) |
added timestamp support to logFormatter
Diffstat (limited to 'voctocore/lib')
-rw-r--r-- | voctocore/lib/loghandler.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/voctocore/lib/loghandler.py b/voctocore/lib/loghandler.py index 4e48c7d..7bfc669 100644 --- a/voctocore/lib/loghandler.py +++ b/voctocore/lib/loghandler.py @@ -1,10 +1,11 @@ #!/usr/bin/python3 -import logging +import logging, time class LogFormatter(logging.Formatter): - def __init__(self, docolor): + def __init__(self, docolor, timestamps=False): super().__init__() self.docolor = docolor + self.timestamps = timestamps def formatMessage(self, record): if self.docolor: @@ -21,11 +22,18 @@ class LogFormatter(logging.Formatter): c_lvl = 31 c_mod = 31 c_msg = 31 - - fmt = '\x1b['+str(c_lvl)+'m%(levelname)8s\x1b[0m \x1b['+str(c_mod)+'m%(name)s\x1b['+str(c_msg)+'m: %(message)s\x1b[0m' + if self.timestamps: + fmt = '%(asctime)s \x1b['+str(c_lvl)+'m%(levelname)8s\x1b[0m \x1b['+str(c_mod)+'m%(name)s\x1b['+str(c_msg)+'m: %(message)s\x1b[0m' + else: + fmt = '\x1b['+str(c_lvl)+'m%(levelname)8s\x1b[0m \x1b['+str(c_mod)+'m%(name)s\x1b['+str(c_msg)+'m: %(message)s\x1b[0m' else: - fmt = '%(levelname)8s %(name)s: %(message)s' + if self.timestamps: + fmt = '%(asctime)s %(levelname)8s %(name)s: %(message)s' + else: + fmt = '%(levelname)8s %(name)s: %(message)s' + if not 'asctime' in record.__dict__: + record.__dict__['asctime']=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(record.__dict__['created'])) return fmt % record.__dict__ |