Skip to content

If json_logs: true, the proxy server will outputs access logs in JSON #10025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yokonao
Copy link

@yokonao yokonao commented Apr 15, 2025

Title

If json_logs: true, the proxy server will outputs access logs in JSON

Relevant issues

Fixes #9906

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on (make test-unit)[https://docs.litellm.ai/docs/extras/contributing_code]
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🆕 New Feature

Changes

  • Added a new file json_logging.py that implements JSON-formatted logging for the proxy server
    • Created a custom JSON log formatter for general logs and access logs
    • Logs include timestamp, log level, message, and other metadata in JSON format
    • Added support for exception handling and extra data in logs
  • Modified proxy_cli.py to:
    • Replace the previous JSON logging setup with the new custom JSON formatter Instead of setting log_config to None, it now uses the new uvicorn_json_log_config.
    • This change provides more structured and detailed JSON logs for better observability

The main purpose is to improve logging capabilities by providing a more structured JSON format for logs, making it easier to parse and analyze log data.

In fact, the following log is output:

{"timestamp": "2025-04-15T18:24:25.982Z", "level": "INFO", "message": "Application startup complete.", "logger_name": "uvicorn.error", "process": 55706, "thread": "MainThread"}
{"timestamp": "2025-04-15T18:24:25.983Z", "level": "INFO", "message": "Uvicorn running on http://0.0.0.0:4000 (Press CTRL+C to quit)", "logger_name": "uvicorn.error", "process": 55706, "thread": "MainThread"}
{"timestamp": "2025-04-15T18:24:28.314Z", "level": "INFO", "logger_name": "uvicorn.access", "process": 55706, "thread": "MainThread", "client_addr": "127.0.0.1:62232", "method": "GET", "full_path": "/health", "http_version": "1.1", "status_code": 200}

ScreenShots

poetry run pytest tests/proxy_unit_tests/test_json_logging.py:

スクリーンショット 2025-04-16 4 27 45

Copy link

vercel bot commented Apr 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 15, 2025 7:27pm

@CLAassistant
Copy link

CLAassistant commented Apr 22, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: If json_logs: true, the proxy server will outputs access logs in JSON
2 participants