mirror of
https://github.com/certbot/certbot.git
synced 2026-06-06 07:12:54 -04:00
PEP compliance logger.py
This commit is contained in:
parent
8d1d9b0734
commit
9b1ab949c7
1 changed files with 37 additions and 22 deletions
|
|
@ -9,24 +9,26 @@ from letsencrypt.client import display
|
|||
|
||||
class Singleton(object):
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if not cls._instance:
|
||||
cls._instance = super(Singleton, cls).__new__(
|
||||
cls, *args, **kwargs)
|
||||
cls._instance = super(
|
||||
Singleton, cls).__new__(cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
# log levels
|
||||
TRACE=5
|
||||
DEBUG=4
|
||||
INFO=3
|
||||
WARN=2
|
||||
ERROR=1
|
||||
FATAL=0
|
||||
NONE=-1
|
||||
TRACE = 5
|
||||
DEBUG = 4
|
||||
INFO = 3
|
||||
WARN = 2
|
||||
ERROR = 1
|
||||
FATAL = 0
|
||||
NONE = -1
|
||||
|
||||
|
||||
class Logger(Singleton):
|
||||
debugLevelStr = {TRACE:'TRACE', DEBUG:'DEBUG', INFO:'INFO', \
|
||||
WARN:'WARN', ERROR:'ERROR', FATAL:'FATAL'}
|
||||
debugLevelStr = {TRACE: 'TRACE', DEBUG: 'DEBUG', INFO: 'INFO',
|
||||
WARN: 'WARN', ERROR: 'ERROR', FATAL: 'FATAL'}
|
||||
|
||||
def __init__(self):
|
||||
self.level = INFO
|
||||
|
|
@ -38,9 +40,10 @@ class Logger(Singleton):
|
|||
raise Exception("Error: no Logger defined")
|
||||
|
||||
def timefmt(self, t=None):
|
||||
if t == None:
|
||||
if t is None:
|
||||
t = time.time()
|
||||
return time.strftime("%b %d %Y %H:%M:%S", time.localtime(t)) + ('%.03f' % (t - int(t)))[1:]
|
||||
return time.strftime("%b %d %Y %H:%M:%S",
|
||||
time.localtime(t)) + ('%.03f' % (t - int(t)))[1:]
|
||||
|
||||
|
||||
class FileLogger(Logger):
|
||||
|
|
@ -48,7 +51,6 @@ class FileLogger(Logger):
|
|||
def __init__(self, outfile):
|
||||
self.outfile = outfile
|
||||
|
||||
|
||||
def log(self, level, data):
|
||||
msg = "%s [%s] %s" % (self.timefmt(), self.debugLevel(level), data)
|
||||
wm = textwrap.fill(msg, 80)
|
||||
|
|
@ -59,8 +61,8 @@ class NcursesLogger(Logger):
|
|||
|
||||
def __init__(self,
|
||||
firstmessage="",
|
||||
height = display.HEIGHT,
|
||||
width = display.WIDTH - 4):
|
||||
height=display.HEIGHT,
|
||||
width=display.WIDTH-4):
|
||||
self.lines = []
|
||||
self.all_content = ""
|
||||
self.d = dialog.Dialog()
|
||||
|
|
@ -95,7 +97,6 @@ class NcursesLogger(Logger):
|
|||
if cur_out != '':
|
||||
self.lines.append(cur_out)
|
||||
|
||||
|
||||
# show last 16 lines
|
||||
self.content = '\n'.join(self.lines[-self.height:])
|
||||
self.show()
|
||||
|
|
@ -107,65 +108,79 @@ class NcursesLogger(Logger):
|
|||
def log(self, level, data):
|
||||
self.add(str(data) + "\n")
|
||||
|
||||
|
||||
log_instance = None
|
||||
|
||||
|
||||
def setLogger(log_inst):
|
||||
global log_instance
|
||||
log_instance = log_inst
|
||||
|
||||
|
||||
def setLogLevel(log_level):
|
||||
global log_instance
|
||||
log_instance.level = log_level
|
||||
|
||||
|
||||
def log(level, data):
|
||||
global log_instance
|
||||
if level <= log_instance.level:
|
||||
log_instance.log(level, data)
|
||||
|
||||
|
||||
def trace(data):
|
||||
log(TRACE, data)
|
||||
|
||||
|
||||
def debug(data):
|
||||
log(DEBUG, data)
|
||||
|
||||
|
||||
def info(data):
|
||||
log(INFO, data)
|
||||
|
||||
|
||||
def warn(data):
|
||||
log(WARN, data)
|
||||
|
||||
|
||||
def error(data):
|
||||
log(ERROR, data)
|
||||
|
||||
|
||||
def fatal(data):
|
||||
log(FATAL, data)
|
||||
|
||||
|
||||
def none(data):
|
||||
# Uh...what?
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Unit test/example usage:
|
||||
|
||||
# Set the logging type you want to use (stdout logging):
|
||||
#logger.setLogger(FileLogger(sys.stdout))
|
||||
# logger.setLogger(FileLogger(sys.stdout))
|
||||
setLogger(NcursesLogger())
|
||||
|
||||
# Set the most verbose you want to log (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, NONE)
|
||||
# Set the most verbose you want to log
|
||||
# (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, NONE)
|
||||
setLogLevel(logger.TRACE)
|
||||
|
||||
# Log a message:
|
||||
#logger.log(logger.INFO, "logger!")
|
||||
# logger.log(logger.INFO, "logger!")
|
||||
|
||||
time.sleep(0.01)
|
||||
info("This is a long line, it's pretty long, butitalso hasbig wordsthat areprobably hardtobreak oninan easywayforthe ncurseslib, sowhatdoes itdo then?")
|
||||
info(("This is a long line, it's pretty long, butitalso hasbig wordsthat "
|
||||
"areprobably hardtobreak oninan easywayforthe ncurseslib, "
|
||||
"sowhatdoes itdo then?"))
|
||||
info("aa " + "a"*70 + "B")
|
||||
|
||||
for i in range(20):
|
||||
info("iteration #%d/20" % i)
|
||||
time.sleep(0.3)
|
||||
|
||||
|
||||
# Alternatively, use
|
||||
error("errrrr")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue