Opened 20 months ago

Last modified 6 months ago

#21429 assigned New feature

BaseCommand should use logging instead of custom output wrappers

Reported by: Nical Owned by: berkerpeksag
Component: Core (Management commands) Version: master
Severity: Normal Keywords: command output logger
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Since python offers the powerful logging class, django.core.management.base.BaseCommand should use it to output messages instead of using custom output wrappers (django.core.management.base.OutputWrapper).

Doing so, we could override those loggers in the settings.py file.

Instead of having two wrappers in a command and having to do:

class Command(BaseCommand):
  def handle(self):
    self.stdout.write('this is an info message')
    self.stderr.write('this is an error message')

We could do

class Command(BaseCommand):
  def handle(self):
    self.output.info('this is an info message')
    self.output.error('this is an error message')
    # and even
    self.output.warning('this is a warning message')

The style_func and ending arguments that the django.core.management.base.OutputWrapper.write method takes could be removed and be configured at once in a overriding custom logger.

Change History (5)

comment:1 Changed 20 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.6 to master

I worked on that idea in #15107 (that I just closed).

comment:2 Changed 8 months ago by berkerpeksag

  • Has patch set
  • Owner changed from nobody to berkerpeksag
  • Status changed from new to assigned

https://github.com/django/django/pull/3467

This PR implements an initial integration of logging module in BaseCommand. I've also updated the documentation a bit. If this approach is okay, I'll update all management commands and tests.

comment:3 Changed 8 months ago by claudep

Do we want to keep the old stdout/stderr way of outputting messages in addition to the new API? This would make self.stdout.write/self.info and self.stderr.write/self.error redundants. Or did you intend to deprecate self.stdout/self.stderr?

comment:4 Changed 8 months ago by berkerpeksag

Or did you intend to deprecate self.stdout/self.stderr?

Yes, but I couldn't find a simple way to deprecate them yesterday. Today, here is my approach: https://github.com/berkerpeksag/django/commit/b439b472842bfd2d04c36b28d079a80fac5814bb

PR updated to address Simon's comments.

comment:5 Changed 6 months ago by timgraham

  • Patch needs improvement set
Note: See TracTickets for help on using tickets.
Back to Top