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'],
},
}
}
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")
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, ie:
'loggers': {
'': {
'handlers': ['rdm_logfile'],
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.