【问题标题】:Django Log Formatting Not Being Applied未应用 Django 日志格式
【发布时间】:2015-04-21 07:01:19
【问题描述】:

我已按照 django 网站 (https://docs.djangoproject.com/en/1.7/topics/logging/#examples) 的指示在我的 django 应用程序中添加了一些记录器,但无论出于何种原因,这些日志都没有应用这些格式。这是我的记录器设置:

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 %(name)s : %(message)s'
    },
},
'filters': {
    'require_debug_false': {
        '()': 'django.utils.log.RequireDebugFalse'
    },
'require_debug_true': {
    '()': 'django.utils.log.RequireDebugTrue',
    }
 },
'handlers': {
    'null': {
        'level': 'DEBUG',
        'class': 'logging.NullHandler',
    },
    'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false'],
        'class': 'django.utils.log.AdminEmailHandler'
        },
    'file_request': {
        'level': 'WARNING',
        'class': 'logging.handlers.RotatingFileHandler',   
        'filename': os.path.join(file_root, 'request' , 'wilkins_request.log'),
        'maxBytes': 1024*1024*1, # 1MB
        'backupCount': 0,
        },    
    'file_backend': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',
        'filters': ['require_debug_true'],    
        'filename': os.path.join(file_root, 'backend' , 'wilkins_backend.log'),
        'maxBytes': 1024*1024*6, # 6MB
        'backupCount': 0,
        },    
    'file_security': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',   
        'filename': os.path.join(file_root, 'backend' , 'wilkins_security.log'),
        'maxBytes': 1024*1024*6, # 6MB
        'backupCount': 0,
        },    
    'file_migrations': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler',   
        'filename': os.path.join(file_root, 'backend' , 'wilkins_migrations.log'),
        'maxBytes': 1024*1024*1, # 1MB
        'backupCount': 0,
        },    
    'file_debug': {
        'level': 'DEBUG',
        'class': 'logging.handlers.RotatingFileHandler', 
        'filters': ['require_debug_true'],  
        'filename': os.path.join(file_root, 'debug' , 'wilkins.log'),
        'filters': ['require_debug_true'],
        'maxBytes': 1024*1024*1, # 1MB
        'backupCount': 0,
        },    
 },
'loggers': {
    'django': {
        'handlers': ['null'],
        'propagate': True,
        'level': 'INFO',
        'formatter': 'simple'
        },
    'django.request': {
        'handlers': ['file_request'],
        'level': 'WARNING',
        'propagate': True,
        'formatter': 'simple'
        },
    'django.security': {
        'handlers': ['file_security'],
        'level': 'INFO',
        'propagate': True,
        'formatter': 'simple'
        },
    'django.db.backends': {
        'handlers': ['file_backend'],
        'level': 'DEBUG',
        'propagate': False,
        'formatter': 'simple'
        },
    'django.db.backends.schema': {
        'handlers': ['file_migrations'],
        'level': 'DEBUG',
        'propagate': False,
        'formatter': 'simple'
        },
    'wilkins': {
        'handlers': ['file_debug'],
        'level': 'DEBUG',
        'propagate': True,
        'formatter': 'simple'
        },
}

}

但我的输出是这样的:

(来自 wilkins_request.log)

Not Found: /accounts/login9
Not Found: /accounts/login9
Not Found: /accounts/login9
Not Found: /l
Not Found: /l
Not Found: /l
Not Found: /favicon.ico

(来自 wilkins.log)

Made it to the Projects view.
Made it to the Projects view.
Made it to the Projects view.
Made it to the Projects view.

我完全不知道为什么会发生这种情况。我使用的是 Django 1.7,所以除了这个日志变量之外,我没有更改 django 中的任何代码路径或设置。

【问题讨论】:

    标签: python django logging


    【解决方案1】:

    格式化程序适用于处理程序,而不是记录器。将那些 formatter: 行移到处理程序字典中,事情应该会按预期工作。

    【讨论】:

    • 谢谢。我不敢相信我在我的配置中没有做到这一点。我知道它又笨又小。
    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多