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

Changed logging settings from settings.py to late init #332

Merged
merged 2 commits into from
Sep 26, 2020
Merged
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
5 changes: 4 additions & 1 deletion dbbackup/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Apps for DBBackup"""

from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _

from dbbackup import log


class DbbackupConfig(AppConfig):
"""
Expand All @@ -12,4 +15,4 @@ class DbbackupConfig(AppConfig):
verbose_name = _('Backup and restore')

def ready(self):
from dbbackup import checks
log.load()
46 changes: 10 additions & 36 deletions dbbackup/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,6 @@
import django
from django.utils.log import AdminEmailHandler

DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'dbbackup.console': {
'formatter': 'base',
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
'dbbackup.mail_admins': {
'level': 'ERROR',
'class': 'dbbackup.log.DbbackupAdminEmailHandler',
'filters': ['require_dbbackup_mail_enabled'],
'include_html': True,
}
},
'filters': {
'require_dbbackup_mail_enabled': {
'()': 'dbbackup.log.MailEnabledFilter'
}
},
'formatters': {
'base': {'format': '%(message)s'},
'simple': {'format': '%(levelname)s %(message)s'}
},
'loggers': {
'dbbackup': {
'handlers': [
'dbbackup.mail_admins',
'dbbackup.console'
],
'level': 'INFO'
},
}
}


class DbbackupAdminEmailHandler(AdminEmailHandler):
def emit(self, record):
Expand All @@ -56,3 +20,13 @@ class MailEnabledFilter(logging.Filter):
def filter(self, record):
from .settings import SEND_EMAIL
return SEND_EMAIL


def load():
mail_admins_handler = DbbackupAdminEmailHandler(include_html=True)
mail_admins_handler.setLevel(logging.ERROR)
mail_admins_handler.addFilter(MailEnabledFilter())

logger = logging.getLogger("dbbackup")
logger.setLevel(logging.INFO)
logger.handlers = [mail_admins_handler]
9 changes: 2 additions & 7 deletions dbbackup/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

# Directory to use for temporary files
TMP_DIR = getattr(settings, 'DBBACKUP_TMP_DIR', tempfile.gettempdir())
TMP_FILE_MAX_SIZE = getattr(settings, 'DBBACKUP_TMP_FILE_MAX_SIZE', 10*1024*1024)
TMP_FILE_READ_SIZE = getattr(settings, 'DBBACKUP_TMP_FILE_READ_SIZE', 1024*1000)
TMP_FILE_MAX_SIZE = getattr(settings, 'DBBACKUP_TMP_FILE_MAX_SIZE', 10 * 1024 * 1024)
TMP_FILE_READ_SIZE = getattr(settings, 'DBBACKUP_TMP_FILE_READ_SIZE', 1024 * 1000)

# Days to keep
CLEANUP_KEEP = getattr(settings, 'DBBACKUP_CLEANUP_KEEP', 10)
Expand All @@ -37,11 +37,6 @@

CUSTOM_CONNECTOR_MAPPING = getattr(settings, 'DBBACKUP_CONNECTOR_MAPPING', {})

# Logging
LOGGING = getattr(settings, 'DBBACKUP_LOGGING', dbbackup.log.DEFAULT_LOGGING)
LOG_CONFIGURATOR = logging.config.DictConfigurator(LOGGING)
LOG_CONFIGURATOR.configure()
Comment on lines -41 to -43
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are the problematic lines: they override the user's logging config inside the project's settings.py


# Mail
SEND_EMAIL = getattr(settings, 'DBBACKUP_SEND_EMAIL', True)
SERVER_EMAIL = getattr(settings, 'DBBACKUP_SERVER_EMAIL', settings.SERVER_EMAIL)
Expand Down