From 6a80518760bd1a9c77d5bc83d4b00608a99f6b8a Mon Sep 17 00:00:00 2001 From: Adwait Salankar Date: Tue, 12 Mar 2024 23:59:33 +0530 Subject: [PATCH] Support disabling file logging --- src/vorta/__main__.py | 3 ++- src/vorta/log.py | 39 +++++++++++++++++++++++++++---------- src/vorta/store/settings.py | 11 +++++++++++ src/vorta/views/misc_tab.py | 4 ++++ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/vorta/__main__.py b/src/vorta/__main__.py index 29d24beb0..851f817fc 100644 --- a/src/vorta/__main__.py +++ b/src/vorta/__main__.py @@ -9,7 +9,7 @@ from vorta import config from vorta._version import __version__ from vorta.i18n import trans_late, translate -from vorta.log import init_logger, logger +from vorta.log import init_file_logging, init_logger, logger from vorta.store.connection import init_db from vorta.updater import get_updater from vorta.utils import DEFAULT_DIR_FLAG, parse_args @@ -79,6 +79,7 @@ def exception_handler(type, value, tb): }, ) init_db(sqlite_db) + init_file_logging() # Init app after database is available from vorta.application import VortaApp diff --git a/src/vorta/log.py b/src/vorta/log.py index 0c87e72db..6f0dd879e 100644 --- a/src/vorta/log.py +++ b/src/vorta/log.py @@ -13,22 +13,21 @@ logger = logging.getLogger() +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + +fh = TimedRotatingFileHandler(config.LOG_DIR / 'vorta.log', when='d', interval=1, backupCount=5) +fh.namer = lambda log_name: log_name.replace(".log", "") + ".log" +fh.setLevel(logging.DEBUG) +fh.setFormatter(formatter) + def init_logger(background=False): logger.setLevel(logging.DEBUG) logging.getLogger('peewee').setLevel(logging.INFO) logging.getLogger('PyQt6').setLevel(logging.INFO) - # create logging format - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - - # create handlers - fh = TimedRotatingFileHandler(config.LOG_DIR / 'vorta.log', when='d', interval=1, backupCount=5) - # ensure ".log" suffix - fh.namer = lambda log_name: log_name.replace(".log", "") + ".log" - fh.setLevel(logging.DEBUG) - fh.setFormatter(formatter) - logger.addHandler(fh) + # Enable file logging by default at the start, since SettingsModel isn't initialised + toggle_file_logging(True) if background: pass @@ -37,3 +36,23 @@ def init_logger(background=False): ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) logger.addHandler(ch) + + +def init_file_logging(): + """ + Decides file logging based on the user's preference + """ + from vorta.store.models import SettingsModel + + toggle_file_logging(SettingsModel.get(key='enable_logging_to_file').value) + + +def toggle_file_logging(should_log_to_file): + """ + Enables file logging according to the input + """ + + if should_log_to_file: + logger.addHandler(fh) + else: + logger.removeHandler(fh) diff --git a/src/vorta/store/settings.py b/src/vorta/store/settings.py index 6bac5f6a9..7398d6f22 100644 --- a/src/vorta/store/settings.py +++ b/src/vorta/store/settings.py @@ -18,6 +18,7 @@ def get_misc_settings() -> List[Dict[str, str]]: startup = trans_late('settings', 'Startup') information = trans_late('settings', 'Information') security = trans_late('settings', 'Security') + logging = trans_late('settings', 'Logging') # Default settings for all platforms. settings = [ @@ -98,6 +99,16 @@ def get_misc_settings() -> List[Dict[str, str]]: ), 'tooltip': trans_late('settings', 'Set owner to current user and umask to 0277'), }, + { + 'key': 'enable_logging_to_file', + 'value': True, + 'type': 'checkbox', + 'group': logging, + 'label': trans_late('settings', 'Enable logging to file'), + 'tooltip': trans_late( + 'settings', 'When disabled, logging to a file will be turned off, and logs will not be saved in a file' + ), + }, { 'key': 'previous_profile_id', 'str_value': '1', diff --git a/src/vorta/views/misc_tab.py b/src/vorta/views/misc_tab.py index 6a3375352..5a8d16770 100644 --- a/src/vorta/views/misc_tab.py +++ b/src/vorta/views/misc_tab.py @@ -13,6 +13,7 @@ ) from vorta.i18n import translate +from vorta.log import toggle_file_logging from vorta.store.models import BackupProfileMixin, SettingsModel from vorta.store.settings import get_misc_settings from vorta.utils import get_asset, search @@ -99,6 +100,9 @@ def populate(self): if setting.key == 'enable_fixed_units': cb.stateChanged.connect(self.refresh_archive.emit) + if setting.key == 'enable_logging_to_file': + cb.stateChanged.connect(toggle_file_logging) + tb = ToolTipButton() tb.setToolTip(setting.tooltip)