Can't silence warnings that django-cms produces

半城伤御伤魂 提交于 2019-12-05 11:12:06

The surgical way to solve this is to create a logging filter that will only filter out warnings that are explicitly specified to be silenced. You know:

Errors should never pass silently.
Unless explicitly silenced.


With that in mind, here is my filter machinery, all in settings.py (for now anyways):

MY_IGNORED_WARNINGS = {
    'RemovedInDjango18Warning: `PublisherManager.get_query_set`',
    'RemovedInDjango18Warning: `PageManager.get_query_set`',
    'RemovedInDjango18Warning: `CMSChangeList.get_query_set`',
    'form PagePermissionInlineAdminForm needs updating',
    'form ViewRestrictionInlineAdminForm needs updating',
    'form PageUserForm needs updating',
    '/cms/admin/placeholderadmin.py:133: RemovedInDjango18Warning: Options.module_name has been deprecated',
    '/cms/admin/settingsadmin.py:28: RemovedInDjango18Warning: Options.module_name has been deprecated',
    '/cms/admin/pageadmin.py:111: RemovedInDjango18Warning: Options.module_name has been deprecated',
    'RemovedInDjango18Warning: `PagePermissionInlineAdmin.queryset',
    'RemovedInDjango18Warning: `ViewRestrictionInlineAdmin.queryset`',
    'RemovedInDjango18Warning: `PageUserAdmin.queryset`',

}

def filter_djangocms_warnings(record):
    for ignored in MY_IGNORED_WARNINGS:
        if ignored in record.args[0]:
            return False
    return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'filters': {
        'ignore_djangocms_warnings': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': filter_djangocms_warnings,
        },
    },
    'loggers': {
        'py.warnings': {
            'handlers': ['console', ],
            'filters': ['ignore_djangocms_warnings', ],
        }
    },
}

I imagine it can be further refined... any ideas?


Why bother?

For one, I can use now (again) use django bash autocomplete without a bunch of warnings echoed every time I press [tab].

Also, now that I don't have 20+ warnings form DjangoCMS, I can actually see what warnings come from my own code and fix it.

This is actually unrelated to Django CMS, even though you may see the warnings issued by a module that Django CMS uses.

The reason is that Django >= 1.5.x routes all warnings to the logging system as per this change. So to disable warnings in Django you have to add a logger for py.warnings (as documented in Python's warning module):

# settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
         'null': {
           'level': 'DEBUG',
           'class': 'logging.NullHandler',
         }
     },
    'loggers': {
         'py.warnings': {
             'propagate': False,
             'handlers': ['null']
          }
     },
}

You can use the -W flag to filter warnings.

python -W ignore manage.py runserver
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!