寫好Log對我而言一直是一項重要的技能, 因為它可以讓問題發生時, 讓開發者用最短的時間找到並解決問題… 剛好在Hacker News上看到這篇 Logging in Python like a PRO , 順便紀錄一下這位作者的一些觀點…
好的Log所需具備的特性
* Descriptive
https://guicommits.com/how-to-log-in-python-like-a-pro/
* Contextual
* Reactive
They’re descriptive in the sense that they give you a piece of information, they’re contextual because they give you an overview of the state of things at the moment, and finally, they’re reactive because they allow you to take action only after something happened (even though your logs are sent/consumed real-time, there’s not really something you can do to change what just happened).
簡單來說如果一段log可以具備上述三個特性, 作者就認為可以讓log的讀者能夠更輕鬆的定位問題的發生點, 以及相對應的事件背景 !
Log 的時機點
As a rule of thumb consider logging:
https://guicommits.com/how-to-log-in-python-like-a-pro/
* At the start of relevant operations or flows (e.g. Connection to third-party, etc);
* At any relevant progress (e.g. Authentication successful, got a valid response code, etc);
* At the conclusion of an operation (e.g. EITHER succeeded or failed);
至於說何時要寫Log呢? 作者這邊指出事件的起始點, 相關的處理進度發生時, 以及事件處理完成時都是適合Log的時間點 !!!
Python 下的Logging的一些範例設定
Python上的一些Log設定我就不太熟了, 先紀錄一下之後可以看看….
總結
這篇文章算是簡單的提要了寫Log時的一些大方向, 這樣之後要寫也蠻適合當參考…