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

custom output should account for the current verbosity level #7

Merged
merged 3 commits into from
Mar 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion logbasecommand/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.2'
__version__ = "0.0.3"
28 changes: 17 additions & 11 deletions logbasecommand/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class LogBaseCommand(BaseCommand):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
prefix = getattr(settings, 'LOGBASECOMMAND_PREFIX', None) or __name__
self.logger = logging.getLogger(prefix + '.' + self.__module__.split('.')[-1])
prefix = getattr(settings, "LOGBASECOMMAND_PREFIX", None) or __name__
self.logger = logging.getLogger(prefix + "." + self.__module__.split(".")[-1])

def __handle_custom_std(self, ifstd, std, msg, *args):
if ifstd:
Expand All @@ -33,32 +33,38 @@ def log_debug(self, msg, *args, **kwargs):
return self.logger.debug(msg, *args, **kwargs)

def log(self, msg, *args, **kwargs):
self.__custom_stdout(msg, *args)
if self.logger.level <= logging.INFO:
gsilvapt marked this conversation as resolved.
Show resolved Hide resolved
self.__custom_stdout(msg, *args)
return self.logger.info(msg, *args, **kwargs)

def log_warning(self, msg, *args, **kwargs):
self.__custom_stderr(msg, *args)
if self.logger.level <= logging.WARNING:
self.__custom_stderr(msg, *args)
return self.logger.warning(msg, *args, **kwargs)

def log_error(self, msg, *args, **kwargs):
self.__custom_stderr(msg, *args)
if self.logger.level <= logging.ERROR:
self.__custom_stderr(msg, *args)
return self.logger.error(msg, *args, **kwargs)

def log_exception(self, msg, *args, **kwargs):
self.__custom_stderr(msg, *args)
return self.logger.exception(msg, *args, **kwargs)

def execute(self, *args, **options):
self.verbosity = options['verbosity']
self.verbosity = options["verbosity"]
self.logger.setLevel(
[logging.ERROR, max(self.logger.getEffectiveLevel(), logging.INFO), logging.DEBUG, logging.DEBUG][
self.verbosity
]
[
logging.ERROR,
max(self.logger.getEffectiveLevel(), logging.INFO),
logging.DEBUG,
logging.DEBUG,
][self.verbosity]
)

if options.get('stdout') is not None:
if options.get("stdout") is not None:
self.custom_stdout = True
if options.get('stderr') is not None:
if options.get("stderr") is not None:
self.custom_stderr = True

super().execute(*args, **options)
3 changes: 2 additions & 1 deletion testapp/testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
path("admin/", admin.site.urls),
]
2 changes: 1 addition & 1 deletion testapp/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ def test_silent(self):
out = StringIO()
err = StringIO()
call_command('some_command', verbosity=0, stdout=out, stderr=err)
self.assertEqual(out.getvalue(), 'info message\n')
self.assertEqual(out.getvalue(), '')
self.assertEqual(err.getvalue(), 'error message\nexception handled\n')