Reputation: 6005
When writing custom Django management commands, the docs recommend using self.stdout
and self.stderr
.
class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("Hello world!")
self.stderr.write("Something went horribly wrong")
I've typically used the logging
module in the past:
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
def handle(self, *args, **options):
logging.info("Hello world!")
logging.error("Something went horribly wrong")
A few questions:
BaseCommand
loggers and the logging
logger?BaseCommand
logger use the logging
module under the hood? If so, what level does self.stdout
and self.stderr
use?Upvotes: 2
Views: 146
Reputation: 477200
Is there a difference between the Django BaseCommand loggers and the logging logger?
Yes, a lot. stdout
and stderr
simply are I/O handlers that, by default, write to the standard output channel (stdout) [wiki] and standard error channel (stderr) [wiki]. These thus just write the content to these channels, and don't do any formatting, have no log level that might be ommited, and can not work with multiple handlers.
This is in essence what the logging package does: it allows one to format it properly, to omit it depending on the log level, and let it handle it by (multiple) custom handlers. For example to write it to the output channel, save it to a file, insert it in a database, etc.
Does the Django BaseCommand logger use the logging module under the hood? If so, what level does
self.stdout
andself.stderr
use?
No, the logging
module has a handler that will, by default write to the output channel, although you can configure it to use a different handler.
Upvotes: 2