Skip to content

feat(logs): add public captureLog API and logger facade#592

Open
turnipdabeets wants to merge 1 commit intofeat/logs-storage-queuefrom
feat/logs-public-api
Open

feat(logs): add public captureLog API and logger facade#592
turnipdabeets wants to merge 1 commit intofeat/logs-storage-queuefrom
feat/logs-public-api

Conversation

@turnipdabeets
Copy link
Copy Markdown
Contributor

@turnipdabeets turnipdabeets commented May 6, 2026

💡 Motivation and Context

Public API for the logs feature on top of #590 (storage + queue). Stacked on feat/logs-storage-queue, not main.

PostHogSDK.shared.captureLog("hello", level: .info, attributes: ["k": "v"])
PostHogSDK.shared.logger.warn("careful")
PostHogSDK.shared.flush()  // drains events + replay + logs

Adds PostHogSDK.captureLog(_:level:attributes:traceId:spanId:traceFlags:) (@objc overloads + Swift default-arg variant) and PostHogSDK.logger, a lazy facade with trace / debug / info / warn / error / fatal. PostHogSDK.flush() now drains all three queues — no separate flushLogs(), matching the existing replay convention.

beforeSend runs at the SDK layer before logsQueue.add so sensitive data is scrubbed before entering any internal pipeline; rate cap stays at the queue layer. Capture-time context (distinctId, sessionId, screenName, appState, featureFlagKeys) is snapshotted on the calling thread so identity / session changes between capture and flush can't mutate a queued record. Screen name is cached on PostHogSDK via the existing screenViewPublisher subscription.

💚 How did you test it?

make test503 tests in 105 suites pass.

PostHogTests/PostHogLogsCaptureTest.swift covers main / background / concurrent capture, empty-body drop, opt-out drop, capture-time distinctId snapshotting (identify A → captureLog → identify B → flush → record carries A), logger.infocaptureLog level: .info, all six severity numbers on the wire, OTLP resource attributes, flush from background thread, and backgrounding triggers a logs flush.

PostHogExample/Views/LogsView.swift — demo each level, flush, traced-log, and a 1000-record flood (rate-cap demo).

📝 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 pnpm changeset to generate a changeset file (covers the combined feature).

@turnipdabeets turnipdabeets requested a review from a team as a code owner May 6, 2026 20:00
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

posthog-ios Compliance Report

Date: 2026-05-07 15:12:25 UTC
Duration: 4893ms

⚠️ Some Tests Failed

0/16 tests passed, 16 failed


Feature_Flags Tests

⚠️ 0/16 tests passed, 16 failed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 104ms
Request Payload.Flags Request Uses V2 Query Param 93ms
Request Payload.Flags Request Hits Flags Path Not Decide 123ms
Request Payload.Flags Request Omits Authorization Header 110ms
Request Payload.Token In Flags Body Matches Init 111ms
Request Payload.Groups Round Trip 187ms
Request Payload.Groups Default To Empty Object 71ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 186ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 130ms
Request Payload.Disable Geoip Omitted Defaults To False 146ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 150ms
Request Lifecycle.No Flags Request On Init Alone 88ms
Request Lifecycle.No Flags Request On Normal Capture 2373ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 72ms
Request Lifecycle.Mock Response Value Is Returned To Caller 146ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 71ms

Failures

request_payload.request_with_person_properties_device_id

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.flags_request_uses_v2_query_param

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.flags_request_hits_flags_path_not_decide

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.flags_request_omits_authorization_header

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.token_in_flags_body_matches_init

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.groups_round_trip

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.groups_default_to_empty_object

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.person_properties_distinct_id_auto_populated_when_caller_omits_it

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.disable_geoip_false_propagates_as_geoip_disable_false

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.disable_geoip_omitted_defaults_to_false

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_payload.flag_keys_to_evaluate_contains_only_requested_key

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_lifecycle.no_flags_request_on_init_alone

Expected 0 /flags requests, got 1

request_lifecycle.no_flags_request_on_normal_capture

Expected 0 /flags requests, got 1

request_lifecycle.two_flag_calls_produce_two_remote_requests

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

request_lifecycle.mock_response_value_is_returned_to_caller

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

side_effect_events.get_feature_flag_captures_feature_flag_called_event

404, message='Not Found', url='http://host.docker.internal:8080/get_feature_flag'

Comment thread PostHog/PostHogSDK.swift Outdated
@marandaneto marandaneto requested a review from a team May 6, 2026 22:12
Comment thread PostHog/PostHogSDK.swift Outdated
@marandaneto marandaneto requested a review from a team May 7, 2026 10:39
Comment thread PostHog/Logs/PostHogLogRecord.swift Outdated
Comment thread PostHog/PostHogSDK.swift

// Cache the latest screen name so `captureLog` can tag log records
// without needing main-thread access to UIViewController helpers.
screenViewToken = DI.main.screenViewPublisher.onScreenView.subscribe { [weak self] name in
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this should be responsibility of PostHogLogger instead?. PostHogSDK can read from logger instance logger.lastScreenName

Comment thread .changeset/logs-feature.md Outdated
Comment thread PostHog/PostHogSDK.swift
Comment on lines +969 to +971
/// - traceId: 32-character lowercase hex W3C trace id.
/// - spanId: 16-character lowercase hex W3C span id.
/// - traceFlags: W3C trace flags bitfield (bit 0 is the `sampled` flag).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we validate these at capture time you think? Probably a question for @PostHog/logs team

Comment thread PostHog/PostHogSDK.swift Outdated
let sId = sessionManager.getSessionId(readOnly: true)
let scr = currentScreenName
let appState = sessionManager.isAppInBackgroundSnapshot ? PostHogLogRecord.AppState.background : PostHogLogRecord.AppState.foreground
let flagKeys = remoteConfig?.getFeatureFlags()?.keys.map { String($0) } ?? []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be only active flags? For normal events in dynamicContext() we include only active flags I think. Also on the first PR, the comment for this field calls out for active flags only

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, updated!

@turnipdabeets turnipdabeets force-pushed the feat/logs-storage-queue branch 2 times, most recently from cd30009 to d7bc6bd Compare May 7, 2026 14:48
@turnipdabeets turnipdabeets force-pushed the feat/logs-public-api branch from 86c0645 to 0217342 Compare May 7, 2026 14:56
User-facing surface on top of the storage + queue infrastructure:

  - PostHogSDK.captureLog(_:level:attributes:traceId:spanId:traceFlags:)
    with @objc overloads and a Swift default-arg variant
  - PostHogSDK.logger — lazy facade with trace / debug / info / warn /
    error / fatal

The existing PostHogSDK.flush() now drains all three queues (events,
replay, logs) — no separate flushLogs() needed.

beforeSend runs at the SDK layer (before logsQueue.add) so sensitive
data is scrubbed before it can enter any internal pipeline. Rate cap
stays at the queue layer.

Capture-time context (distinctId, sessionId, screenName, appState,
featureFlagKeys) is snapshotted on the calling thread; identity or
session changes between capture and flush cannot mutate a queued
record. Screen name is cached on PostHogSDK via the existing
screen-view publisher so log records can be tagged without needing
main-thread access to UIViewController helpers.

Also adds:
  - PostHogTests/PostHogLogsCaptureTest.swift — integration tests
    covering main / background / concurrent capture, empty-body and
    opt-out drops, distinctId snapshotting, all six severity numbers
    on the wire, OTLP resource attributes, and backgrounding triggers
    flush.
  - PostHogExample/Views/LogsView.swift — demo each level, flush,
    traced-log, and a 1000-record flood.
  - .changeset/logs-feature.md — combined-feature entry.

fix test

no uppercase required

consts
@turnipdabeets turnipdabeets force-pushed the feat/logs-public-api branch from 0217342 to 5989997 Compare May 7, 2026 15:00
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.

3 participants