litellm - ✅(Solved) Fix [Feature]: Structured Logs for 3rd Party filtering [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#23884Fetched 2026-04-08 00:54:02
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×2cross-referenced ×1

Error Message

"level": "ERROR",

Fix Action

Fixed

PR fix notes

PR #24447: feat(logging): add component and logger fields to JSON logs for 3rd p…

Description (problem / solution / changelog)

Relevant issues

Resolves #23884

Pre-Submission checklist

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

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

<!-- Select the type of Pull Request --> <!-- Keep only the necessary ones -->

🆕 New Feature

Changes

Add component and logger fields to structured JSON logs

Adds two fields to the JsonFormatter output:

  • component: the logger name (e.g. LiteLLM Proxy, LiteLLM Router)
  • logger: source file and line number (e.g. proxy_server.py:412)

Makes it easier to filter and route logs by component in third-party observability tools (Datadog, Grafana, CloudWatch) without parsing the message string.

Changed files

  • litellm/_logging.py (modified, +6/-0)
  • tests/test_litellm/test_logging.py (modified, +66/-0)
RAW_BUFFERClick to expand / collapse

Check for existing issues

  • I have searched the existing issues and checked that my issue is not a duplicate.

The Feature

We want more structure in the logs we send so they can filter it in 3rd party services

Motivation, pitch

This is all the information in the logs litellm produces that ends up in datadog: { "timestamp": "2026-03-17T16:41:05.597Z", "service": "llm-gateway", "message": "New secret value mismatch. Expected: sk-ioYFmsE1Hwgoyj3_KqCDUg, Got: sk-NntmAE_9peTemQlf0T2_DA", "attributes": { "stacktrace": "NoneType: None", "level": "ERROR", "timestamp": "2026-03-17T16:41:05.597447" } }

We are looking for litellm to implement basic structured logging and provide some attributes in the output to filter on, something like: "component": "xyz",

What part of LiteLLM is this about?

Proxy

LiteLLM is hiring a founding backend engineer, are you interested in joining us and shipping to all our users?

No

Twitter / LinkedIn details

No response

extent analysis

Fix Plan

To implement basic structured logging in LiteLLM, we will modify the logging configuration to include additional attributes.

Step-by-Step Solution

  • Update the logging format to include a component attribute:
import logging

# Create a custom logging formatter
class StructuredLoggingFormatter(logging.Formatter):
    def format(self, record):
        data = {
            'timestamp': self.formatTime(record),
            'service': 'llm-gateway',
            'message': record.getMessage(),
            'component': 'xyz',  # Add the component attribute
            'attributes': {
                'stacktrace': record.exc_text if record.exc_info else None,
                'level': record.levelname,
                'timestamp': self.formatTime(record)
            }
        }
        return data

# Create a logger with the custom formatter
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(StructuredLoggingFormatter())
logger.addHandler(handler)
  • Use the updated logger to log messages:
logger.error('New secret value mismatch. Expected: sk-ioYFmsE1Hwgoyj3_KqCDUg, Got: sk-NntmAE_9peTemQlf0T2_DA')

This will output a structured log message with the component attribute:

{
  "timestamp": "2026-03-17T16:41:05.597Z",
  "service": "llm-gateway",
  "message": "New secret value mismatch. Expected: sk-ioYFmsE1Hwgoyj3_KqCDUg, Got: sk-NntmAE_9peTemQlf0T2_DA",
  "component": "xyz",
  "attributes": {
    "stacktrace": "NoneType: None",
    "level": "ERROR",
    "timestamp": "2026-03-17T16:41:05.597447"
  }
}

Verification

Verify that the logs are being output in the expected structured format by checking the log output.

Extra Tips

  • Make sure to update the logging configuration to include the custom formatter for all log levels (e.g. INFO, WARNING, ERROR, etc.).
  • Consider using a logging library like structlog to simplify structured logging.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING