Python Logging

Recently, my colleagues asked that programs have to leave any trace stack in a log file. I thought it’s an issue.

In Python, there’s no clever message by default when some error occurs. You may want to use traceback.format_exc() to produce trace stack messages. When an error emerges, it yells and stops.

How to collect any errors at a program run? In exact, how to collect almost all errors? By Kodel’s theorem, we have known that a logging function may not take a log for errors of itself. Then, I took the following solution.

To leave logs for almost all errors, four parts are considered: 1) error-and-stop, by default; 2) error-and-continue; 3) alert-aloud; 4) error-reflexive.

To catch almost all errors, a big, external try-catch architecture is prepared. Then, error-and-stop is a natural way to present errors. Just let errors be captured by the safe-net.

Some errors need be caught without blocking the whole work, and error-and-continue is in need. Use internal try-catch to catch errors, and put it into a “recycle bin." The recycle bin is a save point to the log file, that any program may put error messages into it.

Some issues may exist in code right running. No error. No exception. Here there may be some unacceptable values, flags, or states, and those need be said loudly. So, check relative conditions, and raise custom errors.

Error-reflexive is most impossible to leave logs. Consider that when the logging code is broken, it won’t know and leave logs. Thus, it’s a issue worth reporting to developers for fix, but worth logging.

本篇發表於 Programming。將永久鏈結加入書籤。