Skip to main content

Log events for the Live Event Monitor

Our Live Event Monitor can be a great alternative to LogCat. It's easy to use and accessible by everyone on the team. Occuring events, internal state or data exchanged with APIs and 3rd parties can easily be observed and verified. It's also possible to repurpose some of the logged data as test cases for your automated tests, e.g. to capture UI snapshots.

Activate SDK

To log events or capture test data from your test builds, you need to activate the SDK first.

In your application class' onCreate method, call the activate function of the SDK

class MyApp: Application() {
override fun onCreate() {
super.onCreate()

if (resources.getBoolean(R.bool.for_testing)) // only activate for test builds!
AvLogger.activate(app = this)
}
}
warning

Double-check that you only activate the AvLogger for build flavors that you distribute for testing purposes to avoid hitting any quotas.

Logging

Logging is as simple as calling sendEvent with a AvEvent. As the logging will frequently be done on the boundaries of a component or function, the AvEvent accepts args and/or a response property. This is mostly done to visually distinguish multiple values of the same log in our web UI.

// Example simple log of a Google Analytics event
AvLogger.sendEvent(AvEvent.Builder(group = "GA4", event = "logEvent", args = mutableListOf(eventName, eventValue)).build())

// Example log of argument and response
AvLogger.sendEvent(AvEvent.Builder(group = "User", event = "loadHomeId", args = mutableListOf(userId), response = homeIdLoaded).build())

Serializing objects

JSON

You often want to log entire objects with all their attributes. JSON is a primary citizen and the JSON viewer in our web UI allows for easy inspection of values. So serializing your objects as JSON is the way to go when logging with Appviewer.

With Kotlin the simplest way is to use the kotlinx.serialization library.

AvLogger.sendEvent(
AvEvent.Builder(group = "LoginScreen", event = "UiState", response = Json.encodeToString(loginUiState)).build()
)

Recording for UI snapshots

Saving some of your logged data to be later downloaded as test cases in your automated test runs works just the same way. If you log serialized objects (e.g. updates to a UI state), you select relevant test cases in the Live Event Monitor, and then download them in your tests with our SDK and deserialize again accordingly. Read more here

Custom serializer

If you follow a common serialization strategy for your objects, you can also pass your own Serializer to the activate method, that takes an object and returns a serialized String.

class MyApp: Application() {
override fun onCreate() {
super.onCreate()

if (resources.getBoolean(R.bool.for_testing)) // only activate for test builds!
AvLogger.activate(app = this, serializer = myCustomSerializer, log = false) // switch the boolean flag if you want to see debug logs from our SDK
}
}

Logging OkHttp traffic

A common requirement is to log the requests to your server API and the responses and payloads coming back. To make this a lot easier, our SDK includes a ready-to-use interceptor for OkHttp. So it's also easy to use with Retrofit.

val okHttpClient = OkHttpClient().newBuilder()
.addInterceptor(AvHttpInterceptor(context.getResources().getBoolean(R.bool.for_testing))) // Again, make sure you only activate this in your test builds
...
.build()

// if used with Retrofit:
val retrofit = Retrofit.Builder()
.client(okHttpClient)
...
.build()
warning

Double-check that you only activate the AvHttpInterceptor with the passed boolean flag for build flavors that you distribute for testing purposes to avoid hitting any quotas.