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

Cb22bcbee04e5a2fb897d703e794ca8f?s=47 Tatsuya Arai
September 24, 2014

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

Cb22bcbee04e5a2fb897d703e794ca8f?s=128

Tatsuya Arai

September 24, 2014
Tweet

Transcript

  1. "OESPJEͷϩάग़ྗΛ ͍͍ײ͡ʹ͢Δ QPUBUPUJQT

  2. @cutmail • Fablic, Inc. • iOS / Android App Developer.

  3. Agenda • AndroidΞϓϦ։ൃʹ͓͚ΔLog • Α͋͘Δ໰୊ • Timber • Timberͷ࢖͍ํ •

    ·ͱΊ
  4. Log • Debug Log • Crash Log • Event Tracking

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

    • Product Flavours • ΞϓϦʹผ໊Λ෇͚ͨΓɺҰ෦Λมߋͨ͠ΓͰ͖Δ࢓૊Έ • FlavorຖʹAndroidManifest΍ϦιʔεΛ࣋ͯΔ • Free, Normal • Staging, Production
  6. #VJME7BSJBOUTͰ։ൃ൛"OESPJEΞϓϦΛ෼͚Δ IUUQOJOKJOLVOIBUFOBCMPHDPNFOUSZ 

  7. Ϗϧυ؀ڥʹΑΔ৔߹෼͚ EFCVH SFMFBTF ϩάग़ྗ ̋ ✕ $SBTIMZUJDT ✕ ̋ Πϕϯτϩά

    ✕ ̋
  8. σόοάϩάͬͯ Ͳ͏ͯ͠·͔͢ʁ

  9. Log.d(“MainActivity”, "Activity Created”);

  10. ΈΜͳେ޷͖ LogΫϥε

  11. 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!"); } } Α͋͘Δ࣮૷
  12. Α͋͘Δ࣮૷ public class Util { public static void log(String message)

    { if (BuildConfig.DEBUG) { Log.d("DemoAppLog", message); } } }
  13. Α͋͘Δ࣮૷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); } } }
  14. ى͜Γ͕ͪͳ͜ͱ • releaseϏϧυͳͷʹσόοάϩάग़ྗ • debugϏϧυͳͷʹCrashlyticsʹૹ৴͞ΕͯΔ

  15. AndroidͷLogϥΠϒϥϦ • android-logging-log4j • SLF4J • hugo

  16. None
  17. • https://github.com/JakeWharton/timber • ҆৺ͷSquareɺJakeWharton࡞ • AndroidͷLogΫϥεͷϥούʔ • ࣮ଶ͸1ͭͷϑΝΠϧ • ʮ໦ࡐʯ

  18. 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); }
  19. ࢖͍ํ • 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()); } } }
  20. 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) { } }
  21. ಠࣗͷ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); } }
  22. ࢖͍ํ2 (ϩάͷग़ྗ) • ΞϓϦ಺͔ΒTimberͷstaticϝιουΛݺͼग़͢ @Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); setContentView(R.layout.demo_activity); Timber.d("Activity Created”); }
  23. @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()); }
  24. Timberͷ͓࡞๏ • Behavior͸TreeΛ௨ͯ͠ఆٛ͢Δ • Timber.plantͰ೚ҙͷTreeΠϯελϯε͕Πϯε τʔϧͰ͖Δ • ෳ਺ͷTreeΛplantͰ͖Δ • ༻ҙ͞Ε͍ͯΔDebugTreeΛ࢖͏ͱɺݺͼग़͞Εͨ

    Ϋϥε͔ΒࣗಈͰTAGΛઃఆͯ͘͠ΕΔ
  25. Treeͱ͍͏֓೦ͱ ͦͷΠϯλϑΣʔε͕؊

  26. ͭ·Γ

  27. TreeΠϯλϑΣʔεΛ ௨ͯ͠

  28. ࣗ෼͚ͩͷTreeΛ࡞Ε

  29. ͱ͍͏͜ͱ

  30. Timber.plant public static void plant(Tree tree) { if (tree instanceof

    TaggedTree) { TAGGED_TREES.append(FOREST.size(), true); } FOREST.add(tree); }
  31. 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); }
  32. Timberͷྑ͍ͱ͜Ζ • LogΫϥεͷϥούʔͳͷͰϥΠϒϥϦࣗମ͕ബ͘ɺ ಋೖ͕؆୯ • TreeͷΠϯλϑΣʔε͕༻ҙ͞Ε͍ͯͯɺॻ͖΍͍͢ • LogΛ࢖͏ͱ͖ʹඞਢͩͬͨɺTAGΛॻ͘ඞཁ͕ͳ͍ • Ҿ਺ʹϑΥʔϚοτจͱΦϒδΣΫτΛ౉ͤΔ

    • JakeWharton੡ͳͷͰ҆৺
  33. ·ͱΊ • Timber͸LogΫϥεͷബ͍ϥούʔ • LogΫϥεΛͦͷ··࢖͏ͳΒɺTimber࢖͍·͠ΐ ͏

  34. ΤϯδχΞืूத