Skip to content

Issue with Loguru Logging Not Printing in FastAPI Application #1280

Open
@BennisonDevadoss

Description

@BennisonDevadoss

I am facing an issue where Loguru is not printing logs in my FastAPI application when running the app with Uvicorn. I've configured Loguru for logging, but no logs appear in the console.

Here’s what I’ve tried so far:

  1. Removed Loguru's default handlers and added a custom handler for sys.stdout with the format:
    "{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}"
  2. Ensured that Loguru is properly configured before initializing the FastAPI app.

Code Example:
Here’s a simplified version of my setup:

from fastapi import FastAPI
from loguru import logger
import uvicorn

def configure_logging():
    logger.remove()
    logger.add("sys.stdout", format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}", level="INFO")

configure_logging()

app = FastAPI()

@app.get("/")
async def read_root():
    logger.info("This is an info log")
    return {"message": "Hello, world"}

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)

Expected Behavior:
Log messages should appear in the console when I access the / endpoint or start the FastAPI app.

Actual Behavior:
No logs are printed to the console.

Questions:

  1. Is there something wrong with my Loguru configuration?
  2. Do I need additional steps to ensure Loguru works correctly with FastAPI and Uvicorn?
  3. Are there known compatibility issues between Loguru and Uvicorn?

Any help or guidance would be appreciated!


Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions