From 32e2156e1eca7b5bb245d73c7ed97c0ca4fead4a Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 10 Apr 2020 22:53:19 +0100 Subject: [PATCH 1/4] add bot.conf options so we can disable given log files --- bitbotd | 5 ++++- docs/bot.conf.example | 2 ++ src/Logging.py | 42 ++++++++++++++++++++++++------------------ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/bitbotd b/bitbotd index e3962dde..a77c0af0 100755 --- a/bitbotd +++ b/bitbotd @@ -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]) diff --git a/docs/bot.conf.example b/docs/bot.conf.example index 7b1ad465..fdb7a971 100644 --- a/docs/bot.conf.example +++ b/docs/bot.conf.example @@ -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 diff --git a/src/Logging.py b/src/Logging.py index ec8574bd..47096433 100644 --- a/src/Logging.py +++ b/src/Logging.py @@ -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) From bcaefe7eb7f80ce22b43415fdcfc2213474b57c5 Mon Sep 17 00:00:00 2001 From: Vitor Luis Date: Tue, 19 May 2020 22:47:52 +0200 Subject: [PATCH 2/4] Create bitbot_user.service Systemd script to be used by the user w/o the need of root access. Probably useful. Cheers --- docs/systemd/bitbot_user.service | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/systemd/bitbot_user.service diff --git a/docs/systemd/bitbot_user.service b/docs/systemd/bitbot_user.service new file mode 100644 index 00000000..d54aeba8 --- /dev/null +++ b/docs/systemd/bitbot_user.service @@ -0,0 +1,32 @@ +# This systemd script was made to be used in Ubuntu 18.04 LTS +# Check your distro and make the appropriate changes if needed +# In order to allow the user to run their own systemd scripts +# you need to type the following as root: +# loginctl enable-linger $USER (replace $USER with the shell username) +# User systemd scripts are placed inside /home/$USER/.config/systemd/user +# All commands are issued as: systemctl --user [start|stop|restart|reload|enable|disable] service_name.service +# If the folder doesn't exist, type: systemctl --user enable systemd-tmpfiles-clean.timer +# This will automatically create the folder and enable the tempfiles clean timer, +# which can be disabled with: systemctl --user disable systemd-tmpfiles-clean.timer +# +# After placing this script in the correct location, and with bitbot stopped, type: +# systemctl --user enable bitbot_user.service --now +# This will enable the systemd script and launch bitbot + +[Unit] +Description=BitBot Service (User) +Wants=dafault.target +After=default.target + +[Service] +# change any of the 4 following lines as applicable +# The %h will be replaced with the user home directory +# like /home/bitbot +WorkingDirectory=%h/bitbot +ExecStart=/usr/bin/env python3 %h/bitbot/bitbotd +ExecStop=/usr/bin/env python3 %h/bitbot/bitbotctl stop +ExecReload=/usr/bin/env python3 %h/bitbot/bitbotctl reload +Restart=always + +[Install] +WantedBy=dafault.target From 98573f1d49f25c90f9dc456b398cbd0b5815b2fd Mon Sep 17 00:00:00 2001 From: Vitor Luis Date: Tue, 19 May 2020 22:49:56 +0200 Subject: [PATCH 3/4] Update bitbot_user.service Fixed a stupid typo --- docs/systemd/bitbot_user.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/systemd/bitbot_user.service b/docs/systemd/bitbot_user.service index d54aeba8..fef2cc38 100644 --- a/docs/systemd/bitbot_user.service +++ b/docs/systemd/bitbot_user.service @@ -15,7 +15,7 @@ [Unit] Description=BitBot Service (User) -Wants=dafault.target +Wants=default.target After=default.target [Service] From 8a5a28b08b6d527c6a999c91de2a09456a4e7d01 Mon Sep 17 00:00:00 2001 From: Vitor Luis Date: Tue, 19 May 2020 22:52:07 +0200 Subject: [PATCH 4/4] Update bitbot_user.service Next time i need to triple check what I am doing. Another stupid typo --- docs/systemd/bitbot_user.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/systemd/bitbot_user.service b/docs/systemd/bitbot_user.service index fef2cc38..ed8d014b 100644 --- a/docs/systemd/bitbot_user.service +++ b/docs/systemd/bitbot_user.service @@ -29,4 +29,4 @@ ExecReload=/usr/bin/env python3 %h/bitbot/bitbotctl reload Restart=always [Install] -WantedBy=dafault.target +WantedBy=default.target