我无法将 maxBytes 设置为记录文件。
我的 Django 版本是 2.0.3,
python 3.5.2
记录 0.5.1.2
这是异常(exception)。
Traceback (most recent call last):
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/core/management/__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/moon/.env/envpy3/lib/python3.5/site-packages/django/utils/log.py", line 73, in configure_logging
logging_config_func(logging_settings)
File "/usr/lib/python3.5/logging/config.py", line 795, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python3.5/logging/config.py", line 566, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'file_database': __init__() got an unexpected keyword argument 'maxBytes'
这是我的settings.py。如果我添加“maxBytes”参数,它会引发异常。
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s:%(name)s: %(message)s '
'(%(asctime)s; %(filename)s:%(lineno)d)',
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': os.path.join(BASE_DIR, 'logs/debug.log'),
},
'file_database': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': os.path.join(BASE_DIR, 'logs/database.log'),
'maxBytes': 1024 * 1024 * 10, # 10 MB
},
},
'loggers': {
'django.db.backends': {
'handlers': ['file_database'],
'level': 'DEBUG',
'propagate': True,
},
},
}
非常感谢hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
最佳答案
maxBytes
是 RotatingFileHandler
的属性,而不是 FileHandler
。您需要将日志记录配置修改为:
'file_database': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler', # Use RotatingFileHandler
'filename': os.path.join(BASE_DIR, 'logs/database.log'),
'maxBytes': 1024 * 1024 * 10, # 10 MB
},
请注意,包是
logging.handlers
。关于django 日志无法设置 maxBytes,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50677053/