Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support disabling file logging #1965

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point config.LOG_DIR isn't initalized correctly yet. The following lines need to be executed first:

if want_development:
# if we're using the default dev dir
if want_development is DEFAULT_DIR_FLAG:
config.init_dev_mode(config.default_dev_dir())
else:
# if we're not using the default dev dir and
# instead we're using whatever dir is passed as an argument
config.init_dev_mode(want_development)
init_logger(background=want_background)

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
Loading