Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Last active June 2, 2023 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/a5c072227f8a63cbe49a5a2e2c41c669 to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/a5c072227f8a63cbe49a5a2e2c41c669 to your computer and use it in GitHub Desktop.
import logging.config
# 'format': '%(levelname)s %(name)s.%(funcName)s:%(lineno)s- %(message)s'
logging.config.dictConfig(
{
"version": 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(message)s'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
"loggers": {
"snowflake": {
"handlers": ["console"],
"level": "WARNING",
"propagate": True,
},
"": {
"handlers": ["console"],
"level": "INFO",
"propagate": True,
}
},
}
)

Why does Django not show my logs?

This is a common practice

import logging

logger = logging.getLogger(__name__)

logger.info(f'Titan Proxy: {API_URL}')

and yet it was not showing up in the console 😡

Django needs to have logging set up for anything outside the normal

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,    
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(asctime)s %(name)s.%(funcName)s:%(lineno)s- %(message)s'
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'simple',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
            'propagate': False,
        },
        'utils': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': True,
        },
    },    
}

In the loggers dictionary there are two entries:

  1. django will match any logger name that starts with django
    1. django.server
    2. django.request
  2. utils will match any logger name that starts with utils
    1. utils.proxy

If your module does not show up, check the module name and then check the loggers dictionary

What happens if I specify ``

There is no match Or it matches everything?

        '': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },

What happens if change the value of propagate

  1. True is the default, so it can be removed
  2. The value reflects whether the message is passed to the parent logger

References

  1. Good article explaining base python logging
  2. Where Django makes the API logging output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment