Go - logging

发布时间 2023-09-29 14:48:59作者: ZhangZhihuiAAA

The log package provides several functions that allow you to write logs. In particular, there are three sets of functions:
Print
• Prints the logs to the logger
Fatal
• Prints to the logger and calls os.Exit with an exit code of 1
Panic
• Prints to the logger and calls panic
Each set comes in a triplet of functions; for example, Print has Print and Printf , which allow formatting, and Println adds a newline after printing.

Fatal ends with exit code 1. Exit code 1 is a catch - all for general errors, meaning something went wrong with the program, and that’s why it has to exit.

Panic ends with exicode 2. Exit code 2, which is technically inaccurate because traditionally, exit code 2 means something like “incorrect arguments.” It will halt the current goroutine, run the deferred code, and return to the calling function, triggering another panic , which bubbles up eventually to main and finally exits.