月牙天冲
月牙天冲

Reputation: 59

django logging can't set maxBytes

I Can't set maxBytes to logging files. My django version is 2.0.3, Python 3.5.2 logging 0.5.1.2

here is 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'

here is my settings.py. if i add "maxBytes" param,it will raise 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,
        },
    },
}

thank you very muchhhhhhhhhhhhhhhhhhhhhhhhh

Upvotes: 3

Views: 2858

Answers (2)

pushStack
pushStack

Reputation: 4313

From django doc, you can also set backupCount to keep multiple files.

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

Upvotes: 0

Will Keeling
Will Keeling

Reputation: 23004

maxBytes is an attribute of the RotatingFileHandler not the FileHandler. You will need to modify your logging config to:

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

Note that the package is logging.handlers.

Upvotes: 10

Related Questions