mirror of
https://github.com/certbot/certbot.git
synced 2026-08-03 20:12:42 +02:00
PEP compliance logger.py
This commit is contained in:
@@ -9,24 +9,26 @@ from letsencrypt.client import display
|
|||||||
|
|
||||||
class Singleton(object):
|
class Singleton(object):
|
||||||
_instance = None
|
_instance = None
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if not cls._instance:
|
if not cls._instance:
|
||||||
cls._instance = super(Singleton, cls).__new__(
|
cls._instance = super(
|
||||||
cls, *args, **kwargs)
|
Singleton, cls).__new__(cls, *args, **kwargs)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
# log levels
|
# log levels
|
||||||
TRACE=5
|
TRACE = 5
|
||||||
DEBUG=4
|
DEBUG = 4
|
||||||
INFO=3
|
INFO = 3
|
||||||
WARN=2
|
WARN = 2
|
||||||
ERROR=1
|
ERROR = 1
|
||||||
FATAL=0
|
FATAL = 0
|
||||||
NONE=-1
|
NONE = -1
|
||||||
|
|
||||||
|
|
||||||
class Logger(Singleton):
|
class Logger(Singleton):
|
||||||
debugLevelStr = {TRACE:'TRACE', DEBUG:'DEBUG', INFO:'INFO', \
|
debugLevelStr = {TRACE: 'TRACE', DEBUG: 'DEBUG', INFO: 'INFO',
|
||||||
WARN:'WARN', ERROR:'ERROR', FATAL:'FATAL'}
|
WARN: 'WARN', ERROR: 'ERROR', FATAL: 'FATAL'}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.level = INFO
|
self.level = INFO
|
||||||
@@ -38,9 +40,10 @@ class Logger(Singleton):
|
|||||||
raise Exception("Error: no Logger defined")
|
raise Exception("Error: no Logger defined")
|
||||||
|
|
||||||
def timefmt(self, t=None):
|
def timefmt(self, t=None):
|
||||||
if t == None:
|
if t is None:
|
||||||
t = time.time()
|
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):
|
class FileLogger(Logger):
|
||||||
@@ -48,7 +51,6 @@ class FileLogger(Logger):
|
|||||||
def __init__(self, outfile):
|
def __init__(self, outfile):
|
||||||
self.outfile = outfile
|
self.outfile = outfile
|
||||||
|
|
||||||
|
|
||||||
def log(self, level, data):
|
def log(self, level, data):
|
||||||
msg = "%s [%s] %s" % (self.timefmt(), self.debugLevel(level), data)
|
msg = "%s [%s] %s" % (self.timefmt(), self.debugLevel(level), data)
|
||||||
wm = textwrap.fill(msg, 80)
|
wm = textwrap.fill(msg, 80)
|
||||||
@@ -59,8 +61,8 @@ class NcursesLogger(Logger):
|
|||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
firstmessage="",
|
firstmessage="",
|
||||||
height = display.HEIGHT,
|
height=display.HEIGHT,
|
||||||
width = display.WIDTH - 4):
|
width=display.WIDTH-4):
|
||||||
self.lines = []
|
self.lines = []
|
||||||
self.all_content = ""
|
self.all_content = ""
|
||||||
self.d = dialog.Dialog()
|
self.d = dialog.Dialog()
|
||||||
@@ -95,7 +97,6 @@ class NcursesLogger(Logger):
|
|||||||
if cur_out != '':
|
if cur_out != '':
|
||||||
self.lines.append(cur_out)
|
self.lines.append(cur_out)
|
||||||
|
|
||||||
|
|
||||||
# show last 16 lines
|
# show last 16 lines
|
||||||
self.content = '\n'.join(self.lines[-self.height:])
|
self.content = '\n'.join(self.lines[-self.height:])
|
||||||
self.show()
|
self.show()
|
||||||
@@ -107,65 +108,79 @@ class NcursesLogger(Logger):
|
|||||||
def log(self, level, data):
|
def log(self, level, data):
|
||||||
self.add(str(data) + "\n")
|
self.add(str(data) + "\n")
|
||||||
|
|
||||||
|
|
||||||
log_instance = None
|
log_instance = None
|
||||||
|
|
||||||
|
|
||||||
def setLogger(log_inst):
|
def setLogger(log_inst):
|
||||||
global log_instance
|
global log_instance
|
||||||
log_instance = log_inst
|
log_instance = log_inst
|
||||||
|
|
||||||
|
|
||||||
def setLogLevel(log_level):
|
def setLogLevel(log_level):
|
||||||
global log_instance
|
global log_instance
|
||||||
log_instance.level = log_level
|
log_instance.level = log_level
|
||||||
|
|
||||||
|
|
||||||
def log(level, data):
|
def log(level, data):
|
||||||
global log_instance
|
global log_instance
|
||||||
if level <= log_instance.level:
|
if level <= log_instance.level:
|
||||||
log_instance.log(level, data)
|
log_instance.log(level, data)
|
||||||
|
|
||||||
|
|
||||||
def trace(data):
|
def trace(data):
|
||||||
log(TRACE, data)
|
log(TRACE, data)
|
||||||
|
|
||||||
|
|
||||||
def debug(data):
|
def debug(data):
|
||||||
log(DEBUG, data)
|
log(DEBUG, data)
|
||||||
|
|
||||||
|
|
||||||
def info(data):
|
def info(data):
|
||||||
log(INFO, data)
|
log(INFO, data)
|
||||||
|
|
||||||
|
|
||||||
def warn(data):
|
def warn(data):
|
||||||
log(WARN, data)
|
log(WARN, data)
|
||||||
|
|
||||||
|
|
||||||
def error(data):
|
def error(data):
|
||||||
log(ERROR, data)
|
log(ERROR, data)
|
||||||
|
|
||||||
|
|
||||||
def fatal(data):
|
def fatal(data):
|
||||||
log(FATAL, data)
|
log(FATAL, data)
|
||||||
|
|
||||||
|
|
||||||
def none(data):
|
def none(data):
|
||||||
# Uh...what?
|
# Uh...what?
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Unit test/example usage:
|
# Unit test/example usage:
|
||||||
|
|
||||||
# Set the logging type you want to use (stdout logging):
|
# Set the logging type you want to use (stdout logging):
|
||||||
#logger.setLogger(FileLogger(sys.stdout))
|
# logger.setLogger(FileLogger(sys.stdout))
|
||||||
setLogger(NcursesLogger())
|
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)
|
setLogLevel(logger.TRACE)
|
||||||
|
|
||||||
# Log a message:
|
# Log a message:
|
||||||
#logger.log(logger.INFO, "logger!")
|
# logger.log(logger.INFO, "logger!")
|
||||||
|
|
||||||
time.sleep(0.01)
|
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")
|
info("aa " + "a"*70 + "B")
|
||||||
|
|
||||||
for i in range(20):
|
for i in range(20):
|
||||||
info("iteration #%d/20" % i)
|
info("iteration #%d/20" % i)
|
||||||
time.sleep(0.3)
|
time.sleep(0.3)
|
||||||
|
|
||||||
|
|
||||||
# Alternatively, use
|
# Alternatively, use
|
||||||
error("errrrr")
|
error("errrrr")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user