Refactor logging configuration to both Django management commands lintmigrations and makemigrations

This commit is contained in:
David Wobrock committed 2022-03-13 16:51:29 +01:00
1 parent 26a8aaa993
commit 5f11f9417f
3 files changed
+18 -11

No files matched your search

@@ -1,6 +1,5 @@
import configparser import configparser
import itertools import itertools
import logging
import os import os
import sys import sys
from importlib import import_module from importlib import import_module
@@ -12,7 +11,7 @@ from django.core.management.base import BaseCommand
from ...constants import __version__ from ...constants import __version__
from ...migration_linter import MessageType, MigrationLinter from ...migration_linter import MessageType, MigrationLinter
from ...sql_analyser.analyser import ANALYSER_STRING_MAPPING from ...sql_analyser.analyser import ANALYSER_STRING_MAPPING
from ..utils import register_linting_configuration_options from ..utils import configure_logging, register_linting_configuration_options
CONFIG_NAME = "django_migration_linter" CONFIG_NAME = "django_migration_linter"
PYPROJECT_TOML = "pyproject.toml" PYPROJECT_TOML = "pyproject.toml"
@@ -22,8 +21,6 @@ DEFAULT_CONFIG_FILES = (
"tox.ini", "tox.ini",
) )
logger = logging.getLogger("django_migration_linter")
class Command(BaseCommand): class Command(BaseCommand):
help = "Lint your migrations" help = "Lint your migrations"
@@ -145,12 +142,7 @@ class Command(BaseCommand):
import_module(os.getenv("DJANGO_SETTINGS_MODULE")).__file__ import_module(os.getenv("DJANGO_SETTINGS_MODULE")).__file__
) )
if options["verbosity"] > 1: configure_logging(options["verbosity"])
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
elif options["verbosity"] == 0:
logger.disabled = True
else:
logging.basicConfig(format="%(message)s")
django_settings_options = self.read_django_settings(options) django_settings_options = self.read_django_settings(options)
config_options = self.read_config_file(options) config_options = self.read_config_file(options)
@@ -9,7 +9,7 @@ from django.db.migrations.writer import MigrationWriter
from django_migration_linter import MigrationLinter from django_migration_linter import MigrationLinter
from ..utils import register_linting_configuration_options from ..utils import configure_logging, register_linting_configuration_options
def ask_should_keep_migration(): def ask_should_keep_migration():
@@ -42,6 +42,7 @@ class Command(MakeMigrationsCommand):
self.database = options["database"] self.database = options["database"]
self.exclude_migrations_tests = options["exclude_migration_tests"] self.exclude_migrations_tests = options["exclude_migration_tests"]
self.warnings_as_errors = options["warnings_as_errors"] self.warnings_as_errors = options["warnings_as_errors"]
configure_logging(options["verbosity"])
return super(Command, self).handle(*app_labels, **options) return super(Command, self).handle(*app_labels, **options)
def write_migration_files(self, changes): def write_migration_files(self, changes):
@@ -1,3 +1,6 @@
import logging
def register_linting_configuration_options(parser): def register_linting_configuration_options(parser):
parser.add_argument( parser.add_argument(
"--database", "--database",
@@ -21,3 +24,14 @@ def register_linting_configuration_options(parser):
action="store_true", action="store_true",
help="handle warnings as errors", help="handle warnings as errors",
) )
def configure_logging(verbosity):
logger = logging.getLogger("django_migration_linter")
if verbosity > 1:
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
elif verbosity == 0:
logger.disabled = True
else:
logging.basicConfig(format="%(message)s")