Skip to content

Commit

Permalink
RUM-1492 Improve Logs Fragment in sample App
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Oct 31, 2023
1 parent d8f8c22 commit 5868187
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
package com.datadog.android.sample.logs

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.datadog.android.log.Logger
Expand All @@ -23,6 +27,9 @@ internal class LogsFragment :
private var interactionsCount = 0
private lateinit var viewModel: LogsViewModel

private lateinit var messageInput: EditText
private lateinit var levelSpinner: Spinner

@Suppress("CheckInternal")
private val logger: Logger by lazy {
Logger.Builder()
Expand All @@ -43,9 +50,19 @@ internal class LogsFragment :
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_logs, container, false)
rootView.findViewById<View>(R.id.log_warning).setOnClickListener(this)
rootView.findViewById<View>(R.id.log_error).setOnClickListener(this)
rootView.findViewById<View>(R.id.log_critical).setOnClickListener(this)
messageInput = rootView.findViewById(R.id.message)
levelSpinner = rootView.findViewById(R.id.level_spinner)
rootView.findViewById<View>(R.id.send_log).setOnClickListener(this)

ArrayAdapter.createFromResource(
requireContext(),
R.array.log_levels,
android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
levelSpinner.adapter = adapter
}

return rootView
}

Expand All @@ -61,17 +78,9 @@ internal class LogsFragment :

override fun onClick(v: View) {
interactionsCount++
when (v.id) {
R.id.log_warning -> logger.w("User triggered a warning")
R.id.log_error -> logger.e(
"User triggered an error",
IllegalStateException("Oops")
)
R.id.log_critical -> logger.wtf(
"User triggered a critical event",
UnsupportedOperationException("Oops")
)
}
val message = messageInput.text.toString()
val level = levelSpinner.selectedItemPosition + Log.VERBOSE
logger.log(level, message)
}

//endregion
Expand Down
104 changes: 60 additions & 44 deletions sample/kotlin/src/main/res/layout/fragment_logs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,70 @@
~ This product includes software developed at Datadog (https://www.datadoghq.com/).
~ Copyright 2016-Present Datadog, Inc.
-->

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".NavActivity">
android:layout_width="match_parent">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="@string/title_logs"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/log_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_warning"
app:layout_constraintEnd_toStartOf="@id/log_error"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/log_error"
android:layout_width="wrap_content"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_error"
app:layout_constraintEnd_toStartOf="@id/log_critical"
app:layout_constraintStart_toEndOf="@id/log_warning"
app:layout_constraintTop_toBottomOf="@id/title" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/log_critical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_critical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/log_error"
app:layout_constraintTop_toBottomOf="@id/title" />
android:padding="10dp"
tools:context=".NavActivity">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center_horizontal"
android:text="@string/title_logs"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_message"
tools:text="@tools:sample/lorem" />

</com.google.android.material.textfield.TextInputLayout>

<Spinner
android:id="@+id/level_spinner"
android:layout_width="match_parent"
android:layout_height="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_layout"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/send_log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:drawableLeft="@drawable/ic_logs_black_24dp"
android:text="@string/button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/level_spinner" />


</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
14 changes: 11 additions & 3 deletions sample/kotlin/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
<string name="msg_clear_all_data">Are you sure you want to clear all unsent data. You’ll lose any data still on disk. Note that this won’t delete data already sent to Datadog servers.</string>
<string name="msg_all_data_cleared">All unsent data has been cleared.</string>

<string name="button_warning">⚠️</string>
<string name="button_error">🚨</string>
<string name="button_critical">💥</string>
<string name="button_send">Send</string>
<string name="button_anr">Simulate ANR</string>
<string name="button_oom">Simulate OOM</string>
<string name="button_crash">Crash</string>
Expand All @@ -79,6 +77,15 @@
<string name="choice_sqlite">SQLite</string>
<string name="choice_sqldelight">SQLDelight</string>

<string-array name="log_levels">
<item>💬 Verbose</item>
<item>⚙️ Debug</item>
<item>ℹ️ Info</item>
<item>⚠️ Warn</item>
<item>🚨 Error</item>
<item>💥 Assert</item>
</string-array>


<string name="foreground_service_title">Sample App Logs Foreground Service</string>
<string name="foreground_service_desc">Used to send logs from a different process</string>
Expand All @@ -90,6 +97,7 @@
<string name="hint_user_email">User email</string>
<string name="hint_user_gender">Gender</string>
<string name="hint_user_age">Age</string>
<string name="hint_message">Message</string>

<string name="load_picture">Load picture</string>
<string name="widget_description">Datadog Sample Widget</string>
Expand Down

0 comments on commit 5868187

Please sign in to comment.