728x90
오늘은 Timber를 활용해 간단한 예시를 만들어 보겠습니다.
Timber는 안드로이드 기본 Log 위에 유틸리티를 제공하는 라이브러리 입니다.
그래들 설정
=> 모듈 레벨 build.gradle 파일의 android{} 내부에 위와 같이 설정해 Buildconfig 클래스를 자동생성하게 해줍니다.
=> 모듈 레벨 build.gradle 파일의 dependencies{} 내부에 Timber 라이브러리를 추가해줍니다.
레이아웃 xml 파일 코드(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timber 예제"
android:textSize="24sp"
android:layout_marginBottom="32dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="눌려주세요"
android:padding="16dp" />
</LinearLayout>
Application 상속 클래스 코드
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// 디버그 빌드에서만 로그 출력
if (BuildConfig.DEBUG) {
//디버그 모드에서 timber를 활용시 호출하는 클래스에서 태그를 자동으로 추론하도록 설정
Timber.plant(Timber.DebugTree())
}
}
}
=> 관련 호출 메서드 설명은 공식문서를 참고해주세요
DebugTree
DebugTree TypesConstructorsFunctions Constructors Types Functions Log a debug message with optional format args. Log a debug exception and a message with optional format args. Log an error message with optional format args. Log an error exception and a mes
jakewharton.github.io
+
Manifest 파일 설정
=> application 태그 내에 android:name 속성을 활용해 Application 클래스의 상속 클래스를 사용하도록 설정합니다.
액티비티 코틀린 코드
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// onCreate 시 로그 출력하기
Timber.d("onCreate() - 액티비티 생성됨")
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
Timber.d("버튼을 눌렸습니다!")
}
}
// onStart 시 로그 출력하기
override fun onStart() {
super.onStart()
Timber.d("onStart() - 액티비티 시작됨")
}
// onResume 시 로그 출력하기
override fun onResume() {
super.onResume()
Timber.d("onResume() - 액티비티 화면에 보임")
}
// onPause 시 로그 출력하기
override fun onPause() {
super.onPause()
Timber.d("onPause() - 액티비티 일시정지")
}
// onStop 시 로그 출력하기
override fun onStop() {
super.onStop()
Timber.d("onStop() - 액티비티 정지됨")
}
// onDestroy 시 로그 출력하기
override fun onDestroy() {
super.onDestroy()
Timber.d("onDestroy() - 액티비티 종료됨")
}
}
실행 결과
Timber 관련 코틀린 코드로 인해 로그 기록이 출력됨을 확인할 수 있습니다.
'안드로이드 코틀린' 카테고리의 다른 글
[Kotlin][Android] Keystore를 사용해 암호화하기 (0) | 2025.07.03 |
---|---|
[Kotlin][Android] 코루틴(Coroutine) 사용하기 (2) | 2025.06.26 |
[Kotlin][Android] DefaultLifecycleObserver 활용 예시 만들기 (3) | 2025.06.14 |
[Kotlin][Android] 배터리 상태 보기 (0) | 2025.03.28 |
[Kotlin][Android] 음악 재생 예시 만들기 (0) | 2025.03.22 |