Skip to content

Commit

Permalink
custom output should account for the current verbosity level (#7) (#8)
Browse files Browse the repository at this point in the history
* custom output should account for the current verbosity level


---------

Signed-off-by: peterbppb <91122533+peterbppb@users.noreply.github.com>
Co-authored-by: Gustavo Silva <gustavosantaremsilva@gmail.com>
Co-authored-by: Bogdan Peter <bogdan.peter@paddypowerbetfair.com>
  • Loading branch information
3 people committed Mar 13, 2024
1 parent ddffb85 commit 02d30e2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
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:
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')

0 comments on commit 02d30e2

Please sign in to comment.