add bot.conf options so we can disable given log files

This commit is contained in:
jesopo 2020-04-10 22:53:19 +01:00
parent 9dfbe3723c
commit 32e2156e1e
3 changed files with 30 additions and 19 deletions

View file

@ -62,7 +62,10 @@ log_level = args.log_level
if not log_level:
log_level = "debug" if args.verbose else "warn"
log = Logging.Log(not args.no_logging, log_level, LOG_DIR)
log_level_files = config.get("log-levels", "TRACE,INFO,WARN"
).split(",")
log = Logging.Log(not args.no_logging, log_level, LOG_DIR, log_level_files)
log.info("Starting BitBot %s (Python v%s, db %s)",
[IRCBot.VERSION, platform.python_version(), DATABASE])

View file

@ -11,6 +11,8 @@
#lock-file = {DATA}/bot.lock
#sock-file = {DATA}/bot.sock
#log-levels = TRACE,INFO,WARN
# database - currently only supports sqlite3
#database = sqlite3:{DATA}/bot.db

View file

@ -27,7 +27,10 @@ class HookedHandler(logging.StreamHandler):
class Log(object):
_hooks: typing.List[typing.Callable[[int, str], None]]
def __init__(self, to_file: bool, level: str, location: str):
def __init__(self, to_file: bool,
level: str,
location: str,
file_levels: typing.List[str]):
self._hooks = []
logging.addLevelName(LEVELS["trace"], "TRACE")
@ -52,25 +55,28 @@ class Log(object):
self.logger.addHandler(hook_handler)
if to_file:
trace_path = os.path.join(location, "trace.log")
trace_handler = logging.handlers.TimedRotatingFileHandler(
trace_path, when="midnight", backupCount=5)
trace_handler.setLevel(LEVELS["trace"])
trace_handler.setFormatter(formatter)
self.logger.addHandler(trace_handler)
if "TRACE" in file_levels:
trace_path = os.path.join(location, "trace.log")
trace_handler = logging.handlers.TimedRotatingFileHandler(
trace_path, when="midnight", backupCount=5)
trace_handler.setLevel(LEVELS["trace"])
trace_handler.setFormatter(formatter)
self.logger.addHandler(trace_handler)
info_path = os.path.join(location, "info.log")
info_handler = logging.handlers.TimedRotatingFileHandler(
info_path, when="midnight", backupCount=1)
info_handler.setLevel(LEVELS["info"])
info_handler.setFormatter(formatter)
self.logger.addHandler(info_handler)
if "INFO" in file_levels:
info_path = os.path.join(location, "info.log")
info_handler = logging.handlers.TimedRotatingFileHandler(
info_path, when="midnight", backupCount=1)
info_handler.setLevel(LEVELS["info"])
info_handler.setFormatter(formatter)
self.logger.addHandler(info_handler)
warn_path = os.path.join(location, "warn.log")
warn_handler = logging.FileHandler(warn_path)
warn_handler.setLevel(LEVELS["warn"])
warn_handler.setFormatter(formatter)
self.logger.addHandler(warn_handler)
if "WARN" in file_levels:
warn_path = os.path.join(location, "warn.log")
warn_handler = logging.FileHandler(warn_path)
warn_handler.setLevel(LEVELS["warn"])
warn_handler.setFormatter(formatter)
self.logger.addHandler(warn_handler)
def hook(self, func: typing.Callable[[int, str], None]):
self._hooks.append(func)