简体   繁体   中英

ValueError: unable to configure handler 'loggers'

I was writing code in vscode that logs user activity(logins and logouts) using signals on my django website but it keeps showing this error

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 723, in configure_handler
    klass = self.resolve(cname)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 383, in resolve
    name = s.split('.')
AttributeError: 'NoneType' object has no attribute 'split'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception  
    raise _exception[1]
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 810, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Users\pc\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 572, in configure
    raise ValueError('Unable to configure handler '
ValueError: Unable to configure handler 'loggers'

The code i was writing was

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s",
            'datefmt': "%Y/%b/%d %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'django': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(str(LOG_PATH), 'django.log'),
            'maxBytes': (1024 * 1024 * 10),
            'backupCount': 10,
            'formatter': 'verbose',
        },
        'user': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(str(LOG_PATH), 'user.log'),
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 10,
            'formatter': 'verbose',
        },
        'loggers': {
            'django': {
                'handlers': ['django', 'console'],
                'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
            },
            'user': {
                'handlers': ['user', 'console'],
                'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
            },
        },
    },
}

I even started the project from scratch just because of it but i still came back to this error.

Can anyone please tell me the problem. I can find it and am new to django and web development. Also forgive me if its something minor as i said im new to coding in general.

The structure of the LOGGING dictionary got mixed up. The loggers are now an element of the handlers , but should have an independent entry, like:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        # …
    },
    'handlers': {
        # …
    },
    'loggers': {
        # …
    },
}

So in this case it looks like:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s",
            'datefmt': "%Y/%b/%d %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'django': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(str(LOG_PATH), 'django.log'),
            'maxBytes': (1024 * 1024 * 10),
            'backupCount': 10,
            'formatter': 'verbose',
        },
        'user': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(str(LOG_PATH), 'user.log'),
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 10,
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['django', 'console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
        },
        'user': {
            'handlers': ['user', 'console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
    },

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM