【问题标题】:django logging can't set maxBytesdjango 日志记录无法设置 maxBytes
【发布时间】:2018-11-13 13:56:18
【问题描述】:

我无法将 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”参数,它将引发异常。

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

非常感谢你hhhhhhhhhhhhhhhhhhhhhhhh

【问题讨论】:

  • 那么您是如何设置日志记录的?请分享settings.py关于日志的部分(可以隐藏一些不想分享的细节)
  • @WillemVanOnsem 添加

标签: django python-3.x logging


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    django doc,你也可以设置backupCount来保留多个文件。

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'verbose': {
                'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
                'style': '{',
            },
        },
        'handlers': {
            'debugRotatingFileHandler': {
                'level':'DEBUG',
                'class':'logging.handlers.RotatingFileHandler',
                'formatter': 'verbose',
                'maxBytes': 1024*1024*1000, # 1000 MB
                'backupCount': 0, # Keep 1 file
                'filename': os.path.join(BASE_DIR, 'django.log'),
            },
        },
        'loggers': {
            'django': {
                'handlers': ['debugRotatingFileHandler'],
                'level': 'DEBUG',
                'propagate': True,
            },
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2014-11-09
      • 2013-09-11
      • 2010-12-08
      • 2014-04-17
      • 2013-08-04
      • 2012-09-03
      相关资源
      最近更新 更多