Skip to content

Commit

Permalink
Support disabling file logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AdwaitSalankar committed Mar 12, 2024
1 parent b2cf5b1 commit 6a80518
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/vorta/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 29 additions & 10 deletions src/vorta/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
11 changes: 11 additions & 0 deletions src/vorta/store/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/vorta/views/misc_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 6a80518

Please sign in to comment.