Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Androidのログ出力をいい感じにする #potatotips 9

Tatsuya Arai
September 24, 2014

Androidのログ出力をいい感じにする #potatotips 9

Tatsuya Arai

September 24, 2014
Tweet

More Decks by Tatsuya Arai

Other Decks in Programming

Transcript

  1. Build Variants • Build Types • ϏϧυઃఆΛ੾Γସ͑Δ࢓૊Έ • debug, release

    • Product Flavours • ΞϓϦʹผ໊Λ෇͚ͨΓɺҰ෦Λมߋͨ͠ΓͰ͖Δ࢓૊Έ • FlavorຖʹAndroidManifest΍ϦιʔεΛ࣋ͯΔ • Free, Normal • Staging, Production
  2. public class MainActivity extends Activity { private static final String

    TAG = MainActivity.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "MainActivity created!"); } } Α͋͘Δ࣮૷
  3. Α͋͘Δ࣮૷ public class Util { public static void log(String message)

    { if (BuildConfig.DEBUG) { Log.d("DemoAppLog", message); } } }
  4. Α͋͘Δ࣮૷2 public class Logger { public static final void d(String

    tag, String msg) { if (BuildConfig.DEBUG) { Log.d(tag, msg); } } public static final void e(String tag, String msg) { if (BuildConfig.DEBUG) { Log.e(tag, msg); } } public static final void i(String tag, String msg) { if (BuildConfig.DEBUG) { Log.i(tag, msg); } } }
  5. Tree public interface Tree { void v(String message, Object... args);

    void v(Throwable t, String message, Object... args); void d(String message, Object... args); void d(Throwable t, String message, Object... args); void i(String message, Object... args); void i(Throwable t, String message, Object... args); void w(String message, Object... args); void w(Throwable t, String message, Object... args); void e(String message, Object... args); void e(Throwable t, String message, Object... args); }
  6. ࢖͍ํ • ApplicationΫϥεͷonCreate಺ͰTreeΫϥεͷΠϯ ελϯεΛ࡞ͬͯTimberʹplant͢Δ public class ExampleApp extends Application {

    @Override public void onCreate() { super.onCreate(); if (BuildConfig.DEBUG) { Timber.plant(new DebugTree()); } else { Timber.plant(new CrashReportingTree()); } } }
  7. HollowTree 5SFFΠϯλϑΣʔεΛ࣮૷ͨ͠ςϯϓϨʔτΫϥε public static class HollowTree implements Tree { @Override

    public void v(String message, Object... args) { } @Override public void v(Throwable t, String message, Object... args) { } @Override public void d(String message, Object... args) { } @Override public void d(Throwable t, String message, Object... args) { } }
  8. ಠࣗͷTreeΛ࡞Δ private static class CrashReportingTree extends Timber.HollowTree { @Override public

    void i(String message, Object... args) { i(message, args); Crashlytics.log(String.format(message, args)); } @Override public void i(Throwable t, String message, Object... args) { i(message, args); } @Override public void e(String message, Object... args) { e("ERROR: " + message, args); } @Override public void e(Throwable t, String message, Object... args) { e(message, args); Crashlytics.logException(t); } }
  9. ࢖͍ํ2 (ϩάͷग़ྗ) • ΞϓϦ಺͔ΒTimberͷstaticϝιουΛݺͼग़͢ @Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); setContentView(R.layout.demo_activity); Timber.d("Activity Created”); }
  10. @OnClick({ R.id.hello, R.id.hey, R.id.hi }) public void greetingClicked(Button button) {

    Timber.i("A button with ID %s was clicked to say ‘%s'.", button.getId(), button.getText()); }
  11. Timber.plant public static void plant(Tree tree) { if (tree instanceof

    TaggedTree) { TAGGED_TREES.append(FOREST.size(), true); } FOREST.add(tree); }
  12. Timber.uproot /** Remove a planted tree. */ public static void

    uproot(Tree tree) { for (int i = 0, size = FOREST.size(); i < size; i++) { if (FOREST.get(i) == tree) { TAGGED_TREES.delete(i); FOREST.remove(i); return; } } throw new IllegalArgumentException( "Cannot uproot tree which is not planted: " + tree); }