良いデバッグログはプロジェクトの資産である

26e1ba9a02729b2d6013604135221aae?s=47 yhara
July 30, 2016

 良いデバッグログはプロジェクトの資産である

http://eventdots.jp/event/591027

(2016-07-30追記:Rails 5.0からproductionでもDEBUGがデフォルトらしいです)
(2020-09-23追記:https://github.com/rails/rails/pull/39707 INFOに戻りそう)

26e1ba9a02729b2d6013604135221aae?s=128

yhara

July 30, 2016
Tweet

Transcript

  1. ྑ͍σόοάϩά͸ϓϩδΣΫτͷࢿ࢈Ͱ͋Δ Fluentd Meetup in Matsue (2016-07-30) yhara (Yutaka Hara) Fluentd

    Meetup in Matsue (2016-07-30) 1
  2. ΞδΣϯμ 1. ͳͥϩά͕ॏཁ͔ 2. ͳͥϩά͕ࢿ࢈ͳͷ͔ 3. Loggerͷ࢖͍ํ 4. ϩάͷೖΕํ 5.

    ϩάΛಡΈ΍͘͢͢Δ޻෉ Fluentd Meetup in Matsue (2016-07-30) 2
  3. ࣗݾ঺հ • @yhara (twi,er, github) • ࣎լݝ→দߐ (2008ʙ) • (ג)ωοτϫʔΫԠ༻௨৴ݚڀॴ

    • Rubyͷ·ͭ΋ͱ͞Μͷ͍Δձࣾ Fluentd Meetup in Matsue (2016-07-30) 3
  4. NaClඇެࣜϒϩά Fluentd Meetup in Matsue (2016-07-30) 4

  5. Fluentd Meetup in Matsue (2016-07-30) 5

  6. ϘϧμϦϯά΍ͬͯ ·͢ ʮদߐนͷ΅ΓʯͰݕࡧ Fluentd Meetup in Matsue (2016-07-30) 6

  7. ͻͱͪ͘ʹϩάͱ͍ͬͯ΋͍Ζ͍Ζ ͋Γ·͕͢ࠓ೔͸͜͏͍͏ϩάͷ࿩ require 'logger' LOGGER = Logger.new("log.txt") LOGGER.info("ىಈ͠·ͨ͠") Fluentd Meetup

    in Matsue (2016-07-30) 7
  8. ྑ͍ίʔυ ྑ͍ςετ Fluentd Meetup in Matsue (2016-07-30) 8

  9. ྑ͍ϩάʁ Fluentd Meetup in Matsue (2016-07-30) 9

  10. ྑ͘ͳ͍ϩά ʹಡΊͳ͍ϩά Fluentd Meetup in Matsue (2016-07-30) 10

  11. ϩάΛਓ͕ؒಡΉͱ͍͏લఏ͕ڞ༗͞Εͯͳ͍…ʁ ! Fluentd Meetup in Matsue (2016-07-30) 11

  12. ʘ!஌Βͳ͍ͷ͔… LoggerʹൿΊΒΕͨྗΛ…ʗ Fluentd Meetup in Matsue (2016-07-30) 12

  13. 1. ͳͥϩά͕ॏཁͳͷ͔ Fluentd Meetup in Matsue (2016-07-30) 13

  14. σόοάϩάͬͯɺσόοάͷͱ͖ ʹೖΕΔ΍ͭͰ͠ΐʁ Fluentd Meetup in Matsue (2016-07-30) 14

  15. σόοάϩάͬͯɺσόοάͷͱ͖ ʹೖΕΔ΍ͭͰ͠ΐʁ Fluentd Meetup in Matsue (2016-07-30) 15

  16. όά͕ग़͔ͯΒσόοάϩάΛ ೖΕ͍ͯͨͷͰ͸஗͍ Fluentd Meetup in Matsue (2016-07-30) 16

  17. ਖ਼͍͠σόοάखॱ 1. όάΛൃݟ͢Δ 2. ϩάΛݟΔ 3. ݪҼ͕Θ͔Δ Fluentd Meetup in

    Matsue (2016-07-30) 17
  18. ʘΘ͔Δʗ Fluentd Meetup in Matsue (2016-07-30) 18

  19. ڀۃͷϩά͕ೖΕΒΕͨϓϩδΣΫτʹ͓͍ͯ͸ɺσόοάʹ͔͔Δ࣌ؒ͸ݶ Γͳ͘୹͘ͳΓ·͢ɻ͜ͷঢ়ଶΛ໨ඪͱ͠·͢ Fluentd Meetup in Matsue (2016-07-30) 19

  20. 2. ͳͥϩά͸ࢿ࢈ͳͷ͔ Fluentd Meetup in Matsue (2016-07-30) 20

  21. ྑ͍ϩάͱ͸ • σόοάΛॿ͚Δ΋ͷ • ͦͷͨΊʹ͸ʁ • ʮԿ͕ى͔ͬͨ͜ʯ͕෼͔Γ΍͍͢ͷ͕େࣄ • σόοάϩά͸ਓ͕ؒಡΉ΋ͷͳͷͰɺಡΈ΍ ͕͢͞ॏཁ

    Fluentd Meetup in Matsue (2016-07-30) 21
  22. ࠷ॳ͔Β׬ᘳͳϩά͸ॻ͚ͳ͍ • ͲͷΑ͏ͳϩά͕໾ཱ͔ͭ͸ɺσόοάΛ΍ͬͯ Έͳ͍ͱ෼͔Βͳ͍ • ਪᏏ͕ඞཁ • ϓϩδΣΫτͷϑΣʔζʹΑͬͯɺʮద੾ͳϩ άʯ͸มԽ͢Δ Fluentd

    Meetup in Matsue (2016-07-30) 22
  23. ྫ ϓϩδΣΫτॳظ def some_task LOGGER.info("ॲཧAΛ։࢝͠·͢") do_A LOGGER.info("ॲཧA͕ऴྃ͠·ͨ͠") end Fluentd Meetup

    in Matsue (2016-07-30) 23
  24. ྫ ϓϩδΣΫτޙظ def some_task do_A LOGGER.info("ॲཧAΛߦ͍·ͨ͠") end • ॲཧAͰΤϥʔ͕΄ͱΜͲى͖ͳ͘ͳΕ͹ɺϩά ͸ʮߦ͍·ͨ͠ʯ͚ͩͰे෼

    Fluentd Meetup in Matsue (2016-07-30) 24
  25. େࣄͳ͜ͱ͸ • ʮྑ͍ϩάΛॻ͜͏ʯͱࢥ͏͜ͱ • ໨ࢦ͍ͯ͠ͳ͍ͱ͜Ζʹ͸ͨͲΓண͚ͳ͍ • ಡΈ΍͍͢ϩάʹͳΔΑ͏ɺదٓϝϯςφϯε͠ Α͏ Fluentd Meetup

    in Matsue (2016-07-30) 25
  26. 3. Loggerͷ࢖͍ํ Fluentd Meetup in Matsue (2016-07-30) 26

  27. ϩΨʔΛಋೖ͠Α͏ require 'logger' LOGGER = Logger.new(STDOUT) LOGGER.info("ىಈ͠·ͨ͠") ↓࣮ߦ݁Ռ I, [2016-07-27T21:50:08.054189

    #50431] INFO -- : ىಈ͠·ͨ͠ Fluentd Meetup in Matsue (2016-07-30) 27
  28. puts͡ΌͩΊͳͷʁ puts "ىಈ͠·ͨ͠" Fluentd Meetup in Matsue (2016-07-30) 28

  29. LoggerΛ࢖ͬͨํ͕͍͍ཧ༝(1) • ࣌ࠁ͕ग़Δ(ϩάͱϩάͷʮִؒʯ͕෼͔ΔͷͰศ ར) • ·ͱΊͯΦϯɾΦϑͰ͖Δ • Logger.new(nil) #=> ग़ྗ͞Εͳ͍

    Fluentd Meetup in Matsue (2016-07-30) 29
  30. LoggerΛ࢖ͬͨํ͕͍͍ཧ༝(2) • ग़ྗΛ·ͱΊͯ੔ܗͰ͖Δ(forma(er) σϑΥϧτ I, [2016-07-27T21:50:08.054189 #50431] INFO -- :

    ىಈ͠·ͨ͠ ྫ̍ 2016-07-27 21:50:08 +0900 [INFO] ىಈ͠·ͨ͠ ྫ̎ {"time":"2016-07-27 21:50:08 +0900","level":"INFO","msg":"ىಈ͠·ͨ͠"} Fluentd Meetup in Matsue (2016-07-30) 30
  31. LoggerΛ࢖ͬͨํ͕͍͍ཧ༝(3) • ϑΝΠϧʹग़͢͜ͱ΋Ͱ͖Δ • Logger.new("log.txt") • (Rubyͷ৔߹)ϩάϩʔςʔτ΋Ͱ͖Δͧ (shi+_age, shi+_size) •

    ϩάϨϕϧΛ੾Γସ͑ΒΕΔ Fluentd Meetup in Matsue (2016-07-30) 31
  32. ϩάϨϕϧ • DEBUG/INFO/WARN/ERROR/FATAL • ݴޠɾϥΠϒϥϦʹΑͬͯ͸ҧ͏͜ͱ΋͋Δ • ҎԼͷΑ͏ʹ͢ΔͱɺINFOҎ্ͷϩά͚ͩग़ΔΑ ͏ʹͳΔ • Railsͷ৔߹ɺproduc;on͸INFOҎ্

    LOGGER = Logger.new(STDOUT) LOGGER.level = Logger::INFO Fluentd Meetup in Matsue (2016-07-30) 32
  33. 4. ϩάͷೖΕํ Fluentd Meetup in Matsue (2016-07-30) 33

  34. Ͳͷϩά͔ΒೖΕΔ͔ʁ • FATAL • ERROR • WARN • INFO •

    DEBUG Fluentd Meetup in Matsue (2016-07-30) 34
  35. Ͳͷϩά͔ΒೖΕΔ͔ʁ • FATAL • ERROR • WARN • INFO •

    DEBUG Fluentd Meetup in Matsue (2016-07-30) 35
  36. INFOϩάΛೖΕΔ • ࣮گͷΑ͏ʹॻ͘ • ʮαʔόʹ઀ଓ͠·ͨ͠ʯ • ʮϢʔβΛ࡞੒͠·͢ʯ • ʮઓಆΛ։࢝͠·͢ʯ •

    σόοάͷى఺ʹͳΔ • ʮͲ͜·Ͱਖ਼ৗ͔ͩͬͨʯ͕Θ͔Δ Fluentd Meetup in Matsue (2016-07-30) 36
  37. INFOΛೖΕΔ΂͖৔ॴ • ॲཧͷઅ໨ • ྫ) όονॲཧͳΒɺ1߲໨ਐΉ͝ͱͱ͔ • "ϑΝΠϧΛૹ৴͠·ͨ͠ [12/30]" •

    ঢ়ଶ͕มΘͬͨͱ͖ • ྫ) ήʔϜϓϩάϥϜͳΒɺը໘͕ભҠͨ͠ͱ͖ Fluentd Meetup in Matsue (2016-07-30) 37
  38. ӕʹͳΒͳ͍Α͏ʹ͢Δ def create_user(name) logger.info("Ϣʔβ#{name}Λ࡞੒͠·ͨ͠") User.new(name: name).save end Fluentd Meetup in

    Matsue (2016-07-30) 38
  39. ӕʹͳΒͳ͍Α͏ʹ͢Δ def create_user(name) logger.info("Ϣʔβ#{name}Λ࡞੒͠·ͨ͠") User.new(name: name).save end • save͕ࣦഊ͢Δͱɺϩάʹʮ࡞੒͠·ͨ͠ʯͱग़ ͯΔͷʹ࣮ࡍ͸͞Ε͍ͯͳ͍

    Fluentd Meetup in Matsue (2016-07-30) 39
  40. ӕʹͳΒͳ͍Α͏ʹ͢Δ def create_user(name) User.new(name: name).save logger.info("Ϣʔβ#{name}Λ࡞੒͠·ͨ͠") end • saveͷ͋ͱʹϩάΛग़ͤ͹OK Fluentd

    Meetup in Matsue (2016-07-30) 40
  41. INFOϩάΛೖΕΔϝϦοτ • ʮ͜ͷϝιου͸ԿΛ͢Δͷ͔ʯΛݟ௚͢ػձʹͳΔ class Player # ๆଧͪͰ߈ܸ͢Δ def mineuchi @message

    = "҆৺͠Ζɺๆଧͪ͡Ό" ... if ... logger.info("ఢ#{name}Λࡴ͠·ͨ͠") ... end Fluentd Meetup in Matsue (2016-07-30) 41
  42. DEBUGϩάΛೖΕΔ • ม਺ͷ஋ͳͲΛग़ྗ͢Δɺ͕ • logger.debug("x: #{x}, y: #{y}") • શ෦Λίϛοτͯ͠͸͍͚ͳ͍

    • ϩά͕ग़͗͢ΔͱअຐʹͳΔ(ಡΊͳ͘ͳΔ)͔ Β • ࣍΋࢖͍ͦ͏ɺͱ͍͏΋ͷ͚ͩίϛοτ͢Δ Fluentd Meetup in Matsue (2016-07-30) 42
  43. DEBUGϩάΛೖΕΔ • ίϛοτ͢Δ΋ͷ͸ɺଞਓ͕ݟͯ΋෼͔ΔΑ͏ʹॻ͘ • logger.debug("࠲ඪ(#{x}, #{y})Λ୳ࡧ͠· ͢") • ݁ՌతʹɺINFOϩάͱಉ͡ॻ͖ํʹͳΔ(ʙ͠· ͢ɺ͠·ͨ͠)

    • INFOͱDEBUGͷ࢖͍෼͚ • τϥϒϧγϡʔςΟϯά༻ͷ΋ͷ͸DEBUGɺͱ͔ Fluentd Meetup in Matsue (2016-07-30) 43
  44. INFOͱDEBUGͷ࢖͍෼͚ͷྫ def connect_server ... @server = ... ... logger.info("αʔόʹ઀ଓ͠·ͨ͠(host: #{@host})")

    end • "Cannot connect to server" Έ͍ͨͳྫ֎͕ग़ͨͱ͢Δ • @host͕ؒҧ͍ͬͯΔʁ • @host͸߹ͬͯΔ͕αʔό͕མ͍ͪͯΔʁ Fluentd Meetup in Matsue (2016-07-30) 44
  45. INFOͱDEBUGͷ࢖͍෼͚ͷྫ def connect_server logger.debug("αʔόʹ઀ଓ͠·͢(host: #{@host})") ... @server = ... ...

    logger.info("αʔόʹ઀ଓ͠·ͨ͠(host: #{@host})") end Fluentd Meetup in Matsue (2016-07-30) 45
  46. 5. ϩάΛಡΈ΍͘͢͢Δ Fluentd Meetup in Matsue (2016-07-30) 46

  47. ϩά͕ಡΈ΍͍͢ ↓ ঢ়گ͕͙͢೺ѲͰ͖Δ ↓ σόοά͕ḿΔ ↓ ! Fluentd Meetup in

    Matsue (2016-07-30) 47
  48. ޻෉Λ͠Α͏ ΋ͬͱಡΈ΍͘͢Ͱ͖ͳ͍͔ʁ!Λৗʹߟ͑Δ Fluentd Meetup in Matsue (2016-07-30) 48

  49. ࠨ୺Λἧ͑Δ ! 2016-07-30 01:02:03 [foo/foo.swift] ॲཧAΛߦ͍·͢ 2016-07-30 01:02:04 [bar.swift] ॲཧBΛߦ͍·ͨ͠

    ! 2016-07-30 01:02:03 ॲཧAΛߦ͍·͢ [foo/foo.swift] 2016-07-30 01:02:04 ॲཧBΛߦ͍·ͨ͠ [bar.swift] Fluentd Meetup in Matsue (2016-07-30) 49
  50. ه߸Λ͏·͘࢖͏ ! 2016-07-30 01:02:03 ຯํ1͕ఢAΛ߈ܸ͠·ͨ͠ 2016-07-30 01:02:04 ఢB͕ຯํ2Λ߈ܸ͠·ͨ͠ Fluentd Meetup

    in Matsue (2016-07-30) 50
  51. ه߸Λ͏·͘࢖͏ ! 2016-07-30 01:02:03 ຯํ1͕ఢAΛ߈ܸ͠·ͨ͠ 2016-07-30 01:02:04 ఢB͕ຯํ2Λ߈ܸ͠·ͨ͠ ! 2016-07-30

    01:02:03 [ຯํ1 -> ఢA] 2016-07-30 01:02:04 [ຯํ2 <- ఢB] • จষͰͳ͍ͱ͍͚ͳ͍Θ͚͡Όͳ͍ • ঢ়گ͕͙͢೺ѲͰ͖Δͷ͕ྑ͍ϩά Fluentd Meetup in Matsue (2016-07-30) 51
  52. ద౓ʹ؆ܿʹ ! 2016-07-30 01:02:03 ΞϓϦέʔγϣϯAΛىಈ͠·ͨ͠ ! 2016-07-30 01:02:03 ΞϓϦAΛىಈ͠·ͨ͠ Fluentd

    Meetup in Matsue (2016-07-30) 52
  53. ద౓ʹ؆ܿʹ logger.info("Sent files (#{n_records/dt} records/sec)") 2016-07-30 01:02:03 Sent files (202.37704918032787

    records/sec) Fluentd Meetup in Matsue (2016-07-30) 53
  54. ద౓ʹ؆ܿʹ • ׂΓࢉͨ͋͠ͱ͸ɺformatΛ࢖ͬͯద౰ͳਫ਼౓ʹ ͢Δ msg = format("Sent files (%.2f records/sec)",

    n_records/dt) logger.info(msg) 2016-07-30 01:02:03 Sent files (202.38 records/sec) Fluentd Meetup in Matsue (2016-07-30) 54
  55. ΞʔϚʔυίΞίϐϖ ԼҐνʔϜ ʮΈΜͳԿ̖̘Ͱ͍͘ʔʁʯ ʮൿີ͆ʯ ʮ͓ΕύΠϧͩΘɹ·͋ԶύΠϥʔͩ͠ͳ ɹ·͋ͳɹ͍΍͍΍͘͢͝Ͷ͑ͬͯʂ͆ʯ ʮ࢛٭ʯ ʮ͔ͬͯ͜͜ԿϚοϓʁʯ ʮΘ͔ΒΜ͆ʯ Fluentd

    Meetup in Matsue (2016-07-30) 55
  56. ΞʔϚʔυίΞίϐϖ தҐνʔϜ ʮΑΖ͘͠ʔʯ ʮ߭ࢁ͸଴ͬͯΕ͹·ͣෛ͚ͳ͍͔Β·ͬͯΑ͏Ͷʯ ʮ͸͍ʯ ʮϔϦಛ߈ͱ͔͘Δ͔ͳʁ ʮ͏Μɻ̘̚ϩέοτ΋ͬͯ͜͏Ͷʔʯ Fluentd Meetup in

    Matsue (2016-07-30) 56
  57. ΞʔϚʔυίΞίϐϖ ্ҐνʔϜ ʮॏΧϥαϫΧϥαϫΠβφϛΠβφϛΞϥΩσΞϥΩσ ʯ ʮॏٯότότൣαϒΦοΫεΦοΫελϯΫΞϯνʯ ʮதΧϥαϫΧϥαϫΞϚςϥεΦοΫεΦοΫεʯ ʮωζϛ̠̚Φʔτ଎ετ଎ετλϯΫݞࢦఆϤϩʯ ʮ̛̫̩ʯɹʮ̛̫̩ʯɹʮൣαϒʯ ʮ̛̫̩έʔʯ ʮέʔʯ

    ʮϨσΟʔʯ ʮέʔʯ Fluentd Meetup in Matsue (2016-07-30) 57
  58. ద౓ʹ؆ܿʹ • νʔϜϝϯόʔ͕ཧղͰ͖ΔͳΒɺͦͷൣғͰ؆ ܿʹͯ͠Α͍ • ྫɿʮrpsʯͰ௨͡ΔͳΒͦΕͰ΋Α͍ 2016-07-30 01:02:03 Sent files

    (202.37 records/sec) ɹ↓ 2016-07-30 01:02:03 Sent files (202.37 rps) Fluentd Meetup in Matsue (2016-07-30) 58
  59. ·ͱΊ • ϩά͸ਓ͕ؒಡΉ΋ͷ→ಡΈ΍͕͢͞େࣄ • ྑ͍ϩά͕͋Ε͹σόοά͕ḿΔ • ϩάΛҭͯͯշదͳσόοάϥΠϑΛ! Fluentd Meetup in

    Matsue (2016-07-30) 59
  60. 6. ͦΕҎ֎ͷϩάʹ͍ͭͯ (༨ஊɺ༧උεϥΠυ) Fluentd Meetup in Matsue (2016-07-30) 60

  61. FATAL/ERROR/WARN • FATAL: ϓϩάϥϜ͕ଓ͚ΒΕͳ͍Α͏ͳக໋తΤϥʔ • ͍͍ͩͨERRORͰ୅༻͕ͪ͠ • fluentdͰ΋࢖ͬͯͳ͍ͬΆ͍ • ERROR:

    Τϥʔ • WARN: ܯࠂ • ΤϥʔͰ͸ͳ͍͕ؾʹͨ͠΄͏͕ྑ͍΋ͷ Fluentd Meetup in Matsue (2016-07-30) 61
  62. ERRORͳͷ͔INFOͳͷ͔໰୊ def find_user(name) if (user = @database.find(name)) return user else

    logger.error("Ϣʔβ#{name}͕ݟ͔ͭΓ·ͤΜͰͨ͠") return nil end end • ʮERRORϩά͕ग़ͯͳ͍͔ຖ೔֬ೝ͢ΔʯΈ͍ͨͳӡ ༻ͳΒɺINFO΍DEBUG͕ద੾͔΋͠Εͳ͍ • ͦͷ΁Μ͸͋͘·ͰϓϩδΣΫτ࣍ୈ Fluentd Meetup in Matsue (2016-07-30) 62
  63. ྫ֎Λه࿥͢Δ • ERRORϩάͷ୅දྫ • ҎԼ͸ςϯϓϨ begin MyProgram.new.main rescue Exception =>

    ex LOGGER.error("#{ex.class}: #{ex.message}") ex.backtrace.each do |line| LOGGER.error(line) end raise ex end Fluentd Meetup in Matsue (2016-07-30) 63
  64. ྫ֎Λه࿥͢Δ • Ұݟ؆͕ܿͩ…ʁ begin MyProgram.new.main rescue Exception => ex LOGGER.error("#{ex.class}:

    #{ex.message} "+ ex.backtrace.join('\n')) raise ex end • ϩάʹվߦ(\n)ΛೖΕΔͱɺ͋ͱ͔ΒϓϩάϥϜͰ ղੳ͢Δͷ͕໘౗ͳͷͰ͓͢͢Ί͠ͳ͍ • ϓϩηε͕ෳ਺͋Δͱߦ͕ࠞͬͯ͟෼͔Βͳ͘ͳͬͨΓͱ͔ Fluentd Meetup in Matsue (2016-07-30) 64
  65. DEBUGΑΓࡉ͔͍ϩά • TRACE • fluentd(ͱ͍͏͔serverengine)ʹΑΔಠ֦ࣗு • VERBOSE • CocoaLumberjackͱ͍͏iOS༻ͷϥΠϒϥϦ •

    ͋Δͱศརͳ͜ͱ΋ Fluentd Meetup in Matsue (2016-07-30) 65