안드로이드 자바

[Java][Android] Timber 활용 예시 만들기

teamnova 2025. 6. 6. 23:55
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 상속 클래스 코드

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // 디버그 빌드에서만 로그 출력
        if (BuildConfig.DEBUG) {
            //디버그 모드에서 timber를 활용시 호출하는 클래스에서 태그를 자동으로 추론하도록 설정
            Timber.plant(new Timber.DebugTree());
        }
    }
}

 

=> 관련 호출 메서드 설명은 공식문서를 참고해주세요

https://jakewharton.github.io/timber/docs/5.x/timber/timber.log/-timber/-debug-tree/index.html?query=open%20class%20DebugTree%20:%20Timber.Tree

 

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 클래스의 상속 클래스를 사용하도록 설정합니다.



 

 

 

 

액티비티 자바 코드

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // onCreate 시 로그 출력하기
        Timber.d("onCreate() - 액티비티 생성됨");

        Button button = findViewById(R.id.button);
        button.setOnClickListener(v -> {
            Timber.d("버튼을 눌렸습니다!");
        });
    }

    // onStart 시 로그 출력하기
    @Override
    protected void onStart() {
        super.onStart();
        Timber.d("onStart() - 액티비티 시작됨");
    }

    // onResume 시 로그 출력하기
    @Override
    protected void onResume() {
        super.onResume();
        Timber.d("onResume() - 액티비티 화면에 보임");
    }
    
    // onPause 시 로그 출력하기
    @Override
    protected void onPause() {
        super.onPause();
        Timber.d("onPause() - 액티비티 일시정지");
    }
    
    // onStop 시 로그 출력하기
    @Override
    protected void onStop() {
        super.onStop();
        Timber.d("onStop() - 액티비티 정지됨");
    }
    
    // onDestroy 시 로그 출력하기
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Timber.d("onDestroy() - 액티비티 종료됨");
    }
}


 

 

 

 

실행 결과

 

Timber 관련 코드로 인해 로그 기록이 출력됨을 확인할 수 있습니다.