Modules

Jan 17, 2021

ValueError: Unable to configure logger ‘app_logger’

I'm trying to configure python logger using dictConfig. When I run my Python application, it gives me "ValueError: Unable to configure logger 'app_logger'" error in the terminal.

from pydantic import BaseModel

# pydanthic does cls to dict casting
class LogConfig(BaseModel):
    """Logging configuration to be set"""

    LOGGER_NAME: str = 'app_logger'
    LOG_FORMAT: str = (
        "%(levelprefix)s %(asctime)s [%(filename)s %(lineno)d] %(message)s"
    )
    LOG_LEVEL: str = 'DEBU'
    version: int = 1
    disable_existing_loggers: bool = False
    formatters: dict = {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": LOG_FORMAT,
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    }
    handlers: dict = {
        "default": {
            "formatter": "default",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stderr",
        },
    }
    loggers: dict = {
        LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL},
    }

This is how I create logger object.

def create_logger():
    from logging.config import dictConfig
    import logging
    from util.log import LogConfig

    dictConfig(LogConfig().dict())
    logger = logging.getLogger('app_logger')
    logger.info("Logger initialized")

How can I fix this error?

Comments

  • Avatar

    Danielle Carline

    Posted on

    LOG_LEVEL: str = 'DEBU' is not correct.

    It should be LOG_LEVEL: str = 'DEBUG'.

Write a comment

You can use the Markdown syntax to format your comment.

Tags: python