7

I have numerous complex data migrations set up in Django and would like to set up logging to see how they complete and catch errors. I could do this with regular python logging but Django has this semi-built-in and I was hoping the Django logging would work with migrations. However the following setup does not seem to work. Where am I going wrong or does Django logging only work during serve and test?

In a migration file:

import logging
logger = logging.getLogger('rdm')
stuff
logger.warning('message')
more stuff

In Settings.py:

LOGGING = {
     'version': 1,
     'disable_existing_loggers': False,
     'filters': {
         'require_debug_false': {
             '()': 'django.utils.log.RequireDebugFalse',
         },
         'require_debug_true': {
             '()': 'django.utils.log.RequireDebugTrue',
         },
     },
     'formatters': {
         'simple': {
             'format': '[%(asctime)s] %(levelname)s %(message)s',
             'datefmt': '%Y-%m-%d %H:%M:%S'
         },
     },
     'handlers': {
         'rdm_logfile': {
             'level': 'DEBUG', # should capture everything (warning, info, etc.)
             'filters': ['require_debug_false','require_debug_true'],
             'class': 'logging.handlers.RotatingFileHandler',
             'filename': os.path.join(BASE_DIR,'django_rdm.log'),
             'maxBytes': 1024*1024*100, # 100MB
             'backupCount': 5,
             'formatter': 'simple'
         },
    },
    'loggers': {
         'rdm': {
         'handlers': ['rdm_logfile'],
         },
    }
}
Austin Fox
  • 121
  • 1
  • 8

2 Answers2

3

I had the same problem. Somehow I had to re-configure the base logger 'Django':

from django.conf import settings
import logging
logger = logging.getLogger(__name__)


def your_migration(apps, schema_editor):
    logger.setLevel(logging.INFO)
    settings.LOGGING['loggers']['django'] = {
        'level': 'INFO',
        'handlers': ['console']
    }
    logger.info("Test")
Flexonze
  • 27
  • 1
  • 6
  • IMHO, You should avoid configuring logging in such places. Logging configuration should be placed in settings/configuration. This might be usefull, when you are debugging code. – 404pio Jul 10 '23 at 14:59
2

I found this blog post to be helpful.

The tl;dr is that you can configure a root logger to catch everything that isn't otherwise specified. This (apparently) applies to all code run via manage.py including migrations, etc.

Root loggers have an empty name, i.e.:

    'loggers': {
         '': {
         'handlers': ['rdm_logfile'],
         },
Owen
  • 3,063
  • 5
  • 30
  • 26