Skip to content

main.py

setup_logging(logging_level=logging.INFO)

Explicitly configure all loggers

Source code in app/main.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def setup_logging(logging_level=logging.INFO):
    """Explicitly configure all loggers"""

    # Create console handler
    ch = logging.StreamHandler()
    console_formatter = colorlog.ColoredFormatter(
        '%(log_color)s%(levelname)s%(reset)s | %(asctime)s | %(name)s | %(message)s')
    ch.setFormatter(console_formatter)
    ch.setLevel(logging_level)

    # Configure request logger
    request_logger = logging.getLogger('request')
    request_logger.setLevel(logging_level)
    request_logger.handlers.clear()
    request_logger.addHandler(ch)
    request_logger.propagate = False

    # Configure uvicorn loggers
    logging.getLogger('uvicorn.access').handlers.clear()
    logging.getLogger('uvicorn.access').addHandler(ch)
    # INFO logs all requests including regular internal checks
    # which occur every few seconds. Turn down to WARNING
    logging.getLogger('uvicorn.access').setLevel(logging.WARNING)
    logging.getLogger('uvicorn.access').propagate = False

    logging.getLogger('uvicorn.error').handlers.clear()
    logging.getLogger('uvicorn.error').addHandler(ch)
    logging.getLogger('uvicorn.error').propagate = False

    # Configure app logger
    # Log application startup (these messages appear once for each uvicorn
    # worker as it starts).
    app_logger = logging.getLogger('app')
    app_logger.setLevel(logging_level)
    app_logger.addHandler(ch)
    app_logger.propagate = True

    # Start logging
    app_logger.info(
        f"Starting app instance: "
        f"'logging_level': {logging.getLevelName(logging_level)}")