Skip to content

fix: Prefix SDK log messages#682

Merged
marandaneto merged 3 commits into
mainfrom
fix/prefix-sdk-logs-149
Jun 19, 2026
Merged

fix: Prefix SDK log messages#682
marandaneto merged 3 commits into
mainfrom
fix/prefix-sdk-logs-149

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Fixes #149.

Applications that configure logging with a message-only formatter, such as '%(message)s', could see SDK logs like queueing: or consumer exited. without any PostHog identifier. This makes SDK-originated logs difficult to distinguish from application logs.

💚 How did you test it?

  • uv run --extra test pytest -q posthog/test/test_client.py posthog/test/test_consumer.py posthog/test/test_request.py
  • uv run --extra dev ruff format --check posthog/logging_utils.py posthog/client.py posthog/request.py posthog/consumer.py posthog/test/logging_helpers.py posthog/test/test_client.py posthog/test/test_consumer.py posthog/test/test_request.py posthog/test/test_exception_capture.py
  • uv run --extra dev ruff check posthog/logging_utils.py posthog/client.py posthog/request.py posthog/consumer.py posthog/test/logging_helpers.py posthog/test/test_client.py posthog/test/test_consumer.py posthog/test/test_request.py posthog/test/test_exception_capture.py
  • uv run --extra test pytest -q posthog/test/test_exception_capture.py

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

A Pi worker agent implemented the issue-specific fix in a dedicated worktree. The change uses a centralized PostHog logging filter so SDK records are rendered with a [PostHog] prefix in message-only formatter setups while avoiding duplicate prefixes when a message is already prefixed.

Focused tests cover debug, info, and error log output rendered via a '%(message)s' formatter, plus an existing exception-capture assertion was updated for the prefixed debug output.

@marandaneto marandaneto self-assigned this Jun 19, 2026
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/test/logging_helpers.py:9
The logger name `"posthog"` is hardcoded here rather than using `POSTHOG_LOGGER_NAME` from `logging_utils.py`, which already defines this constant. If the logger name ever changes, this file would be missed, and the helper would silently capture the wrong logger's output.

```suggestion
    from posthog.logging_utils import POSTHOG_LOGGER_NAME

    logger = logging.getLogger(POSTHOG_LOGGER_NAME)
```

Reviews (1): Last reviewed commit: "fix: Prefix SDK log messages" | Re-trigger Greptile

Comment thread posthog/test/logging_helpers.py Outdated
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-19 18:37:54 UTC
Duration: 540108ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10006ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10006ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18018ms
Retry Behavior.Does Not Retry On 400 12003ms
Retry Behavior.Does Not Retry On 401 10008ms
Retry Behavior.Respects Retry After Header 16013ms
Retry Behavior.Implements Backoff 32026ms
Retry Behavior.Retries On 500 16001ms
Retry Behavior.Retries On 502 16009ms
Retry Behavior.Retries On 504 16011ms
Retry Behavior.Max Retries Respected 32027ms
Deduplication.Generates Unique Uuids 9992ms
Deduplication.Preserves Uuid On Retry 16015ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16005ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10006ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12008ms
Error Handling.Does Not Retry On 413 10007ms
Error Handling.Retries On 408 14013ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9501ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10006ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10006ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10006ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9511ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10002ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10510ms

@marandaneto marandaneto force-pushed the fix/prefix-sdk-logs-149 branch from 476a124 to 51e3c98 Compare June 19, 2026 11:12
@marandaneto marandaneto marked this pull request as ready for review June 19, 2026 12:11
@marandaneto marandaneto requested a review from a team as a code owner June 19, 2026 12:11
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/_logging.py:24-27
**Redundant initialization calls across three modules**

`_configure_posthog_logging()` is called at module level in `client.py`, `consumer.py`, and `request.py`. Since all three already `import` from `_logging`, simply invoking `_configure_posthog_logging()` once at the bottom of `_logging.py` itself would be sufficient — the function runs the first time the module is imported, and the idempotency guard handles any subsequent imports. The three callers could then drop both the import and the call, keeping the initialization in exactly one place.

Reviews (2): Last reviewed commit: "fix: Prefix SDK log messages" | Re-trigger Greptile

@marandaneto marandaneto enabled auto-merge (squash) June 19, 2026 14:51
@marandaneto marandaneto merged commit 95598a2 into main Jun 19, 2026
30 checks passed
@marandaneto marandaneto deleted the fix/prefix-sdk-logs-149 branch June 19, 2026 18:34
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.

Logging should have a prefix

2 participants