3

I'm trying to set up logging in my Django 2.1 app, but I can't get INFO logs to display. ERROR and WARNING show, but never INFO.

I added a custom LOGGING set in my settings.py, but it doesn't seem to affect the actual logging. I've tried changing the levels, changing the formatters, turning on disable_existing_loggers. I'm pretty certain that Django just ignores LOGGING, but https://docs.djangoproject.com/en/2.1/topics/logging/#configuring-logging and everywhere I look seems to indicate otherwise.

What do do?!

DETAILS:

I'm running this in Heroku with gunicorn, but also run via python manage.py runserver and it acts the same.

In my ./api/v1/serializers.py and ./users/models.py I've added logging like this:

import logging
logger = logging.getLogger(__name__)
...
...
...
logger.info('Test log INFO')
logger.warning('Test log WARNING')
logger.error('Test log ERROR')

My settings are at ./amp/settings.py and have this:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d: %(message)s',
        },
        'simple': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'INFO',
            'formatter': 'verbose',
            'filters': [],
        },
        'file': {
            'class': 'logging.FileHandler',
            'level': 'DEBUG',
            'filename': '/tmp/amp-server/debug.log',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        '': {
            'level': 'INFO',
            'propagate': True,
            'handlers': ['console', 'file'],
            'filters': [],
        },
        'django.request': {
            'level': 'DEBUG',
            'propagate': False,
            'handlers': ['console'],
        },
    },
}

There's nowhere else that references settings.py's LOGGING. I've tried adding explicit loggers named after my project and apps (amp, amp.api, api, etc) to no avail like:

        'amp': {
            'level': 'INFO',
            'propagate': True,
            'handlers': ['console', 'file'],
            'filters': [],
        },

And later logger = logging.getLogger('amp'), but that doesn't make a difference. I'm fairly certain Django ignores my settings.py LOGGING or overwrites it. (Before you ask, LOGGING is not referenced anywhere else in my codebase but settings.py.)

I've carefully combed through the Django LOGGING and Python dictConfig docs, copy-pasted-and-tweaked LOGGING settings from blogs and StackOverflow posts but nothing helps!

NOTE: This is not a duplicate of Why Django logging skips log entries? - those answers are trying to fix a broken LOGGING value, rather than dealing with the fact that LOGGING isn't being used at all. logging.basicConfig can change the logging settings, but the general LOGGING config is still ignored in my settings.py.

Soda Soda
  • 167
  • 1
  • 7
  • 1
    `logger = logging.getLogger('amp')` while having that `'amp'` log configured should do the trick. – henriquesalvaro Oct 26 '18 at 20:33
  • Possible duplicate of [Why Django logging skips log entries?](https://stackoverflow.com/questions/36571284/why-django-logging-skips-log-entries) – solarissmoke Oct 27 '18 at 02:56
  • @henriquesalvaro: Thanks for the idea. But I've tried that. Setting an 'amp' loggers and explicitly setting logging.getLogger('amp') doesn't make any difference. It still seems to pipe through the default configs, ignoring INFO but passing WARNING and above through. – Soda Soda Oct 29 '18 at 15:04
  • @solarissmoke: This is not a duplicate of https://stackoverflow.com/questions/36571284/why-django-logging-skips-log-entries - those answers are trying to fix a broken LOGGING value, rather than dealing with the fact that LOGGING isn't being used at all. One of the answers mentions logging.basicConfig, which can change the logging settings for me, but the general LOGGING config is still ignored in my settings.py. – Soda Soda Oct 29 '18 at 15:12

2 Answers2

10

So it seems that Django doesn't do anything with LOGGING in settings.py...you have to register your LOGGING config manually yourself.

AFAICT, this isn't how it's supposed to be, but reality doesn't match the documentation. Here's what I did that works:

# To prevent Django from configuring logging itself
LOGGING_CONFIG = None
LOGGING = {
    ...
}
import logging.config
logging.config.dictConfig(LOGGING)

If anyone has info on what the situation is or why this isn't mentioned anywhere, I'd be interested!

Soda Soda
  • 167
  • 1
  • 7
  • You only have to explicitly register with dictConfig() if you have ‘LOGGING_CONFIG = None’, otherwise your settings are merged with Django’s defaults and automatically registered. – taherh May 04 '22 at 22:13
  • @Soda Soda did you ever find out why this behaviour is different from the documentation? – N3mo Jul 06 '22 at 07:05
0

Found this solution helpful

LOGGING_CONFIG = None

Logging{...}

import logging.config
logging.config.dictConfig(Logging)

Make sure to follow sequences of statements

Source - https://docs.djangoproject.com/en/3.0/topics/logging/#disabling-logging-configuration