Opened 6 years ago

Last modified 15 months ago

#21429 assigned New feature

BaseCommand should use logging instead of custom output wrappers

Reported by: Nical Owned by: John Kang
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


Since python offers the powerful logging class, should use it to output messages instead of using custom output wrappers (

Doing so, we could override those loggers in the 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):'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 method takes could be removed and be configured at once in a overriding custom logger.

Change History (10)

comment:1 Changed 6 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted
Version: 1.6master

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

comment:2 Changed 5 years ago by Berker Peksag

Has patch: set
Owner: changed from nobody to Berker Peksag
Status: newassigned

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 5 years ago by Claude Paroz

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/ and self.stderr.write/self.error redundants. Or did you intend to deprecate self.stdout/self.stderr?

comment:4 Changed 5 years ago by Berker Peksag

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:

PR updated to address Simon's comments.

comment:5 Changed 4 years ago by Tim Graham

Patch needs improvement: set

comment:6 Changed 3 years ago by Stephane Poss

I saw that the pull request was closed on github. Are there any plans to revive this? It would be very nice to be able to configure logging like everywhere else...

comment:7 Changed 3 years ago by Tim Graham

Feel free to resume work on the patch if you like.

comment:10 Changed 21 months ago by John Kang

Owner: set to John Kang

comment:11 in reply to:  10 Changed 16 months ago by John Kang

Hi all, going to resume work on this patch.

comment:12 in reply to:  10 Changed 15 months ago by John Kang


I was working through this ticket when I noticed that some of the command outputs (migrations for example) output a status on the same line. Example would be:
"migrating-xx.. OK"

Python loggers implicitly add new line characters at the end.
Should I leave these log outputs with newlines or is there a better way to append command statuses on the same line?

Note: See TracTickets for help on using tickets.
Back to Top