'Django docker logging

I am running docker on my Mac. I am having an issue with my views.py and I am trying to print out to the Mac docker desktop some messages to try to debug my code. I have followed

Django and docker: outputting information to console

and it doesn't seem to work for me. The only thing that gets printed out to the docker desktop logs is just errors when they occur, Normally all I get is just the 200 ok from the http request. I have used the print statement and the info statement and neither of them print out.

------------------------------------ setttings.py ------------------------------------ 

from pathlib import Path
import os
import logging.config
import json


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True



#Logging Config

LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'default': {
                'format': '[DJANGO] %(levelname)s %(asctime)s %(module)s '
                          '%(name)s.%(funcName)s:%(lineno)s: %(message)s'
            },
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'default',
            }
        },
        'loggers': {
            '*': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': True,
            }
        },
    }

------------------------------------ views.py ------------------------------------ 

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from .login_model import LoginForm
import logging
logger = logging.getLogger(__name__)


# Create your views here.

def load_index(request):
    logger.info('in index from logger\n')
    return render(request, 'index.html')


def login(request):
    logger.info('in login from logger\n')
    return render(request, 'login.html')

def login_validator(request):

    if request.method == 'POST':
        print(f"method is post")
        logger.info('in login validator')

    print("returning in login validator")
    return render(request, 'index.html')


Solution 1:[1]

Not sure why I am not able to see the logs on the Docker console, but as a workaround stdout.write works. Removed all the log stuff from the settings.py and added the following to the views.py file.

import sys

sys.stdout.write("Message goes here. \n")

in the docker console I am now able to see the messages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marvin The Martian