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

すっきり分かる Python のログ

kimihiro_n
October 27, 2017

すっきり分かる Python のログ

Python のロガー周りを理解するための資料です。(社内勉強会用)

コードとか:
https://github.com/pistatium/about_python_logging

kimihiro_n

October 27, 2017
Tweet

More Decks by kimihiro_n

Other Decks in Technology

Transcript

  1. ͖ͬ͢Γ෼͔Δ
    1ZUIPOͷϩά
    @kimihiro_n

    View full-size slide

  2. 1ZUIPOͰਖ਼͘͠ϩά͕ѻ͑ΔΑ͏ʹͳΔ
    ͞ΑͳΒ1SJOUσόοά
    ཉ͍͠ϩάΛཉ͍͠Α͏ʹʂ
    ඞཁͳՕॴʹߜͬͨϩάͷऔΓࠐΈ
    ϑΝΠϧ΍ίϯιʔϧ΁ͷग़͠෼͚
    -PHHFSͷ࢓૊Έࣗମ͸ଞͷݴޠͰ΋
    +BWBͱ͔+BWBͱ͔
    log.debug('Hello, logging world!!')
    ͜ͷൃදͷ໨త

    View full-size slide

  3. MPHHFSͷॳظԽ
    ϩάΛ࢖͏ͨΊͷ͓·͡ͳ͍
    -PHHFSͱݺ͹ΕΔΠϯελϯεΛ࡞Δ
    ϑΝΠϧ͝ͱʹ࡞੒
    ͱΓ͋͑ͣ࢖ͬͯΈΔ

    View full-size slide

  4. ϩάͷॻ͖ࠐΈ
    -PHHFSΠϯελϯεͷϝιουΛݺͿ͚ͩ
    ॏཁ౓߹͍ʹԠͯͭ͡ͷϨϕϧ͕࢖͑Δ
    ϩάΛॻ͖ࠐΉ

    View full-size slide

  5. ඪ४Τϥʔग़ྗʹग़͖ͯ·͢
    छྨͷϩά͕Ͱʜͯͳ͍
    ϩάΛݟΔ
    This is a Warning message
    This is a Error message
    This is a Critical message

    View full-size slide

  6. TFU-FWFMͱ͍͏ϝιουͰมߋ
    σϑΥϧτ͸8"3/*/(
    ͜ΕҎԼͷ΋ͷ͸ແࢹ͞ΕΔ
    ϨϕϧΛԼ͛ͯΈΔ

    View full-size slide

  7. ݁Ռ
    This is a Warning message
    This is a Error message
    This is a Critical message

    View full-size slide

  8. )BOEMFSͱ͍͏ͷΛॳظԽͯ͠ηοτ
    ͬͪ͜ʹ΋Ϩϕϧઃఆ͕͋Δ
    )BOEMFSͳΔ΋ͷ

    View full-size slide

  9. ݁Ռ
    This is a Debug message
    This is a Info message
    This is a Warning message
    This is a Error message
    This is a Critical message

    View full-size slide

  10. -PHHJOHͷ͘͠Έ

    View full-size slide

  11. __name__
    if __name__..ͷ͓·͡ͳ͍Ͱ͓ೃછΈ
    1ZUIPO͔Β௚઀ݺͿͱ__main__͕ೖΔ
    JNQPSUͯ͠࢖ͬͨ৔߹

    ɹ͜ͷϑΝΠϧͷϞδϡʔϧ໊͕ೖΔ
    NZBQQNPEFMTJUFNTQZͳΒ

    NZBQQNPEFMTJUFNT͕ೖΔ
    ͓·͡ͳ͍ͷಾ

    View full-size slide

  12. HFU-PHHFSͷҾ਺
    ࣮͸จࣈྻͳΒͳΜͰ΋͍͍
    ౉ͨ͠จࣈྻ͕-PHHFSͷ໊લʹ
    @@OBNF@@Λຖ౓ೖΕͯΔͷ͸
    ໊લܾΊΔͷ໘౗͍͔͘͞Β
    ศར͔ͩΒ
    ͓·͡ͳ͍ͷಾ

    View full-size slide

  13. -PHHFS͸֊૚Λ࣋ͯΔ
    -PHHFSͷ໊લΛ υοτ
    Ͱ۠੾Δͱ

    উखʹ֊૚Խ͞ΕΔ
    NZBQQNPEFMTJUFNTͱ͍͏໊લͷ-PHHFS͸
    NZBQQNPEFMT
    NZBQQ
    SPPU
    ͷ-PHHFSΛ਌ʹ࣋ͭ
    -PHHFSͷ֊૚ؔ܎

    View full-size slide

  14. )BOEMFSͷ໾ׂ
    ౉͖ͬͯͨϩάΛద੾ʹॲཧ͢Δ
    ෆཁͳϩάͷϑΟϧλʔ
    ϩάͷϑΥʔϚοτ
    ग़ྗઌ΁ϩάΛૹΔ
    ͭͷ-PHHFSʹରͯ͠ෳ਺ηοτͰ͖Δ
    MPHHFSBEE)BOEMFS
    )BOEMFSͱ͸

    View full-size slide

  15. 4USFBN)BOEMFS
    ඪ४ग़ྗͳͲͷετϦʔϜʹॻ͖ࠐΉ
    'JMF)BOEMFS
    ϑΝΠϧʹॻ͖ࠐΉ
    3PUBUJOH'JMF)BOEMFS
    'JMF)BOEMFSͷ3PUBUFͯ͘͠ΕΔ൛
    4.51)BOEMFS
    ϝʔϧΛૹΔ͍͢͝΍ͭͩΑ
    /VMM)BOEMFS
    Կ΋͠ͳ͍
    ΄͔
    )BOEMFSͷछྨ

    View full-size slide

  16. ϩάͷॻࣜΛࢦఆ͢ΔͨΊͷ΋ͷ
    )BOEMFSʹରͯ͠ࢦఆͰ͖Δ
    ࢖͑Δม਺Ұཡ
    IUUQTEPDTQZUIPOKQMJCSBSZ
    MPHHJOHIUNMMPHSFDPSEBUUSJCVUFT

    formatter = logging.Formatter(

    '[%(asctime)s:%(name)s:%(levelname)s] %(message)s'

    )
    handler.setFormatter(formatter)
    'PSNBUUFS

    View full-size slide

  17. IUUQTEPDTQZUIPOKQIPXUPMPHHJOHIUNMVTFGVMIBOEMFST

    View full-size slide

  18. -PHHFSͷಈ͖ํ
    جຊͷಈ͖
    ࣗ਎ͷ)BOEMFSΛݺͼग़͠
    ˠͭ਌ͷ-PHHFS΁Ҡಈ
    Λ܁Γฦ͢
    ʮ਌ͷ-PHHFSΛݺͼग़͔͢ʯ
    MPHHFSQSPQFSHBUF

    ͱ͍͏ଐੑͰ੍ޚ͍ͯ͠Δ
    'BMTFʹ͢Δͱଧͪ੾Γ
    σϑΥϧτ͸5SVF

    ؆ུԽͷͨΊϑΟϧλʔ౳লུ͍ͯ͠·͢

    View full-size slide

  19. )BOEMFSͷಈ͖
    جຊͷಈ͖
    ϩάͷϨϕϧΛΈͯɺ

    ຬ͍ͨͯͨ͠Βॻ͖ࠐΉ͚ͩ
    'PSNBUUFSͰ੔ܗͯ͠ɺ

    ࣗ਎ͷॻ͖ࠐΈઌ΁ग़ྗ

    ؆ུԽͷͨΊϑΟϧλʔΛলུ͍ͯ͠·͢

    View full-size slide

  20. ໰୊఺
    ϩάઃఆͷίʔυ͕௕͍
    ϩάઃఆΛॻ͘ͷ͸

    ΤϯτϦϙΠϯτͳͲ͕ଟ͍
    ௕͍ίʔυͰݟ௨͠ѱͨ͘͘͠ͳ͍
    ղܾࡦ
    ผϑΝΠϧʹॻ͍ͯJNQPSUʜʜ˚
    ઃఆϑΝΠϧʹͯ͠ಡΈࠐΉʜʜ˓
    ͖ͬ͢Γॻ͖͍ͨ

    View full-size slide

  21. +40/Ͱॻ͍ͯΈΔ
    EJDU$POpH
    ઃఆ͕ೖͬͨࣙॻΛ౉͢
    +40/ͷύʔε͸ࣗྗͰ
    ެࣜ%PDͰ͸:".-
    :".-ύʔαʔ͸ඇඪ४
    +40/͸ϏϧτΠϯ
    'JMF$POpHΛ࢖ͬͨ΍Γํ΋

    View full-size slide

  22. -PHHFS
    ϩάΛѻ͏ͨΊͷΠϯελϯε
    ϨϕϧʹΑΔϑΟϧλϦϯά͕Մೳ
    ֊૚Խ͞Ε໊ͨલΛ࣋ͭ
    ਌ͷ-PHHFS΁ͱ఻ൖ͢Δ
    )BOEMFS
    ϩάͷग़ྗΛ؅ཧ͢Δ
    -PHHFSʹର͠ରଟͰඥ෇͘
    ϨϕϧʹΑΔϑΟϧλϦϯά͕Մೳ
    ·ͱΊ

    View full-size slide

  23. -PHHJOH)08501ZUIPOެࣜυΩϡϝϯτ
    IUUQTEPDTQZUIPOKQIPXUPMPHHJOHIUNM
    ϩάग़ྗͷͨΊͷQSJOUͱJNQPSUMPHHJOH͸΍Ίͯ΄͍͠2JJUB
    IUUQTRJJUBDPNBNFEBNBJUFNTCCGDG
    ϊʔτϒοΫ
    IUUQTHJUIVCDPNQJTUBUJVNBCPVU@QZUIPO@MPHHJOHCMPCNBTUFS
    BCPVU@MPHHJOHJQZOC
    ࠓճͷ಺༰ΛKVQZUFSOPUFCPPLܗࣜͰ·ͱΊͯ·͢
    ࢀߟ

    View full-size slide