Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/logs-public-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"posthog": minor
"posthog-android": minor
---

Add public Logs API: `PostHog.logger.trace/debug/info/warn/error/fatal(message, attrs?)` plus a `PostHogLogsConfig` for serviceName, environment, resourceAttributes, rate cap, and `addBeforeSend` redaction hooks. Logs ship via OTLP/JSON to `/i/v1/logs` and pick up auto-attached attributes (`app.state`, distinctId, sessionId, screen name, feature flags). Matches the equivalent surfaces on posthog-ios and posthog-react-native.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
android:theme="@style/Theme.PostHogAndroidSample"
tools:targetApi="34"
android:name=".MyApp">
<activity
android:name="com.posthog.android.sample.LogsActivity"
android:exported="false"
android:theme="@style/Theme.PostHogAndroidSample" />

<activity
android:name="com.posthog.android.sample.NormalActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.posthog.android.sample

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.posthog.PostHog
import com.posthog.android.sample.ui.theme.postHogAndroidSampleTheme

class LogsActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
postHogAndroidSampleTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
logsDemo()
}
}
}
}
}
}

/**
* Dogfood section for the logs API. Tap each row to capture a log; the
* "Last action" line shows what fired.
*/
@Composable
fun logsDemo(modifier: Modifier = Modifier) {
var lastAction by remember { mutableStateOf("") }
val attrs = mapOf("source" to "android-sample")
val items =
listOf(
"trace" to { PostHog.logger.trace("LogsActivity trace tap", attrs) },
"debug" to { PostHog.logger.debug("LogsActivity debug tap", attrs) },
"info" to { PostHog.logger.info("LogsActivity info tap", attrs) },
"warn" to { PostHog.logger.warn("LogsActivity warn tap", attrs) },
"error" to { PostHog.logger.error("LogsActivity error tap", attrs) },
"fatal" to { PostHog.logger.fatal("LogsActivity fatal tap", attrs) },
)
Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = modifier) {
Text("Logs (tap to capture):")
items.forEach { (label, onTap) ->
Text(
text = " • $label",
modifier =
Modifier.clickable {
onTap()
lastAction = "Sent $label"
},
)
}
Text(
text = " • flush all queues",
modifier =
Modifier.clickable {
PostHog.flush()
lastAction = "flush() called"
},
)
if (lastAction.isNotEmpty()) {
Text("Last action: $lastAction")
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class MyApp : Application() {
sessionReplayConfig.screenshot = true
surveys = false
errorTrackingConfig.autoCapture = false
logs.serviceName = "posthog-android-sample"
logs.environment = "development"
}
PostHogAndroid.setup(this, config)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class NormalActivity : ComponentActivity() {
startActivity(Intent(this, SessionReplayActivity::class.java))
}

val logsButton = findViewById<Button>(R.id.logsButton)
logsButton.setOnClickListener {
startActivity(Intent(this, LogsActivity::class.java))
}

val button = findViewById<Button>(R.id.button)
// val editText = findViewById<EditText>(R.id.editText)
// val imvAndroid = findViewById<ImageView>(R.id.imvAndroid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
android:id="@+id/sessionReplayButton"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logs"
android:id="@+id/logsButton"
/>

<!-- &lt;!&ndash; <View&ndash;&gt;-->
<!-- &lt;!&ndash; android:layout_width="match_parent"&ndash;&gt;-->
<!-- &lt;!&ndash; android:layout_height="5dp"&ndash;&gt;-->
Expand Down
Loading
Loading