This question is a simpler version of this 4 years old question without answer Django management command doesn't show logging output from python library:
I have a command:
class Command(BaseCommand):
def handle(self, *args, **options):
...
MyParser(data)
And in MyParser:
logger = logging.getLogger(__name__)
Class MyParser:
def __init__(self, data):
logger.info('hello')
Why the logger does not display to stdout when I run the command? With a print it is OK but I need a logger
PS:
I tried this but it does not change anything
from parser import logger
root_logger = logger
root_logger.setLevel(logging.INFO)
MyParser(data)
I will base this on the fact that you haven't properly configured your logging configuration which is the most likely. By default, there is no logging on anything except django.requests
(or something along those lines.
If you want your logging messages to appear, you would need something that catches & forward your messages to the appropriate handlers.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console': {
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'console',
},
},
'loggers': {
'': {
'level': 'INFO',
'handlers': ['console'],
},
},
}
(I've - shamelessly - extracted & stripped this code block from this blog post on django logging)