django日志记录无法设置maxBytes

时间:2018-06-04 09:10:28

标签: django python-3.x logging

我无法将maxBytes设置为记录文件。 我的django版本是2.0.3, Python 3.5.2 记录0.5.1.2

这是例外。

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" param,它会引发Exception。

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,
        },
    },
}

非常感谢你好,呵呵呵呵呵呵呵呵呵

1 个答案:

答案 0 :(得分:5)

maxBytesRotatingFileHandler而非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