【问题标题】:Django - Logging not overriding default settingsDjango - 日志记录不覆盖默认设置
【发布时间】:2014-04-17 15:24:16
【问题描述】:

我目前正在使用 Django 1.6.2 开发网站,但无法正确进行日志记录配置。

我在设置文件中设置了“LOGGING”,但它似乎被忽略了。这是我的设置文件:

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    import os
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))

    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = "duummy"

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    TEMPLATE_DEBUG = True

    ALLOWED_HOSTS = []


    # Application definition

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'channels'
    )

    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    )

    ROOT_URLCONF = 'my_app.urls'

    WSGI_APPLICATION = 'my_app.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/1.6/ref/settings/#databases

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'db.sqlite3',
        }
    }

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': '127.0.0.1:11211',
        }
    }

    # Internationalization
    # https://docs.djangoproject.com/en/1.6/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'Brazil/East'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.6/howto/static-files/

    STATIC_URL = '/static/'

    # Logging
    #

    LOGGING_DIR = os.path.join(BASE_DIR, 'logs/')

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'verbose': {
                'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
            },
            'simple': {
                'format': '%(asctime)s - %(module)s -> %(levelname)s - %(message)s'
            }
        },
        'filters': {
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            }
         },
        'handlers': {
            'console':{
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose'
            },
            'debug_log_handler': {
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': os.path.join(LOGGING_DIR, 'debug_log'),
                'formatter':'verbose'
            },
            'db_log_handler': {
                'level': 'ERROR',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': os.path.join(LOGGING_DIR, 'db_log'),
                'when':'D',
                'interval':1,
                'formatter':'simple'
            },
            'request_log_handler': {
                'level': 'ERROR',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': os.path.join(LOGGING_DIR, 'request_log'),
                'when':'D',
                'interval':1,
                'formatter':'simple'
            },
            'site_log_handler': {
                'level': 'ERROR',
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'filename': os.path.join(LOGGING_DIR, 'log'),
                'when':'D',
                'interval':1,
                'formatter':'simple'
            }
        },
        'loggers': {
            'django': {
                'handlers': ['console', 'debug_log_handler'],
                'level': 'DEBUG',
                'propagate': True,
                'filters': ['require_debug_true']
            },
            'django.db.backends': {
                'handlers': ['db_log_handler'],
                'level': 'ERROR',
                'propagate': True
            },
            'django.request': {
                'handlers': ['request_log_handler'],
                'level': 'ERROR',
                'propagate': True
            },
            'test': {
                'handlers': ['site_log_handler'],
                'level': 'ERROR',
                'propagate': True
            },
        }
    }

当我运行服务器时,控制台输出如下信息:

[13/Mar/2014 01:25:12] "GET / HTTP/1.1" 404 2015

它不打印调试信息,也不以我为控制台指定的格式打印。

我错过了什么吗?

谢谢!

【问题讨论】:

    标签: python django debugging logging


    【解决方案1】:

    当您运行服务器时,似乎执行了“GET /”,Django 会将请求记录到“django.request”记录器。而且由于您将 LOGGING.disable_existing_loggers 设置为 True,它必须使用您配置的 django.request 设置。

    在 LOGGING.loggers 中,您将 django.request 的级别设置为“错误”。但是,根据the documentation,4XX 仅记录为警告。只有 5XX 被记录为错误。

    【讨论】:

      【解决方案2】:

      Django 仅记录到四个记录器,如文档中的 heredjangodjango.requestdjango.db.backendsdjango.security。其中第一个不直接记录,您已将下两个的级别设置为 ERROR - 这意味着它们不会记录任何 DEBUG 消息 - 最后一个仅在安全时记录出现问题。

      尝试将test 记录器的级别设置为DEBUG,将console_handler 添加到其handlers 列表中,并从您的一个视图向其中记录一些DEBUG 消息 - 您应该会看到一些输出。

      【讨论】:

        猜你喜欢
        • 2011-07-23
        • 2022-01-27
        • 2016-11-02
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多