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: if not log_level:
log_level = "debug" if args.verbose else "warn" 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)", log.info("Starting BitBot %s (Python v%s, db %s)",
[IRCBot.VERSION, platform.python_version(), DATABASE]) [IRCBot.VERSION, platform.python_version(), DATABASE])

View file

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

View file

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