Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

Android에서 Timber로 로깅 경험 향상

<시간/>

목재 라이브러리는 Android Log의 확장 라이브러리입니다. Android 애플리케이션을 개발하는 동안 대부분의 개발자는 Android 로그를 선호합니다. 그러나 여기서 문제는 Android 프로젝트를 배포하는 동안 깨끗한 로그에 관한 것입니다. Timber 라이브러리를 사용하여 이 프로세스를 방지합니다.

이 예제는 Android에서 Timber를 통합하는 방법을 보여줍니다.

1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.

2단계 − 아래와 같이 build.gradle에 Timber 라이브러리를 추가합니다.

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   implementation 'com.jakewharton.timber:timber:4.7.1'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

3단계 − 아래와 같이 MainActivity의 onCreate 메소드에서 Timber를 초기화해야 합니다.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      if (BuildConfig.DEBUG) {
         Timber.plant(new Timber.DebugTree());
      }
   }
}

4단계 − 목재는 아래와 같이 오류 및 경고 방법이 다릅니다.

Timber.v("Some Text");- It indicates about verbose error
Timber.d("Some Text ");- It indicates about debug error
Timber.i("Some Text ");- It indicates about information error
Timber.w("Some Text ");- It indicates about warning error
Timber.e("Some Text ");- It indicates about error

5단계 − Timber의 간단한 예는 아래와 같습니다.

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      if (BuildConfig.DEBUG) {
         Timber.plant(new Timber.DebugTree());
      } else {
         Timber.plant(new ReleaseTree());
      }
      Timber.v("Some Text");
      Timber.d("Some Text ");
      Timber.i("Some Text ");
      Timber.w("Some Text ");
      Timber.e("Some Text ");
   }
}

6단계 − 위의 예에서 우리는 보고 명시하기 위해 아무 것도 변경하지 않습니다.

위 코드의 샘플 출력은 아래와 같습니다 -

Android에서 Timber로 로깅 경험 향상