Debug/Debugf are very useful methods for recording debug information, commonly used in development/testing environments. When the application goes live, you can easily enable/disable them using SetDebug or configuration files.

    1. package main
    2. import (
    3. "context"
    4. "time"
    5. "github.com/gogf/gf/v2/frame/g"
    6. "github.com/gogf/gf/v2/os/gtime"
    7. "github.com/gogf/gf/v2/os/gtimer"
    8. )
    9. func main() {
    10. ctx := context.TODO()
    11. gtimer.SetTimeout(ctx, 3*time.Second, func(ctx context.Context) {
    12. g.Log().SetDebug(false)
    13. })
    14. for {
    15. g.Log().Debug(ctx, gtime.Datetime())
    16. g.Log().Info(ctx, gtime.Datetime())
    17. time.Sleep(time.Second)
    18. }
    19. }

    In this example, the glog.Debug method is used to output debug information. After 3 seconds, the output of debug information is turned off. After execution, the output result shows only 3 log entries. Subsequent debug log information is not output because it is turned off using the SetDebug method.

    1. 2022-01-05 15:59:05.674 [DEBU] 2022-01-05 15:59:05
    2. 2022-01-05 15:59:05.675 [INFO] 2022-01-05 15:59:05
    3. 2022-01-05 15:59:06.684 [DEBU] 2022-01-05 15:59:06
    4. 2022-01-05 15:59:06.684 [INFO] 2022-01-05 15:59:06
    5. 2022-01-05 15:59:07.692 [DEBU] 2022-01-05 15:59:07
    6. 2022-01-05 15:59:07.692 [INFO] 2022-01-05 15:59:07
    7. 2022-01-05 15:59:08.708 [INFO] 2022-01-05 15:59:08
    8. 2022-01-05 15:59:09.717 [INFO] 2022-01-05 15:59:09
    9. 2022-01-05 15:59:10.728 [INFO] 2022-01-05 15:59:10
    10. 2022-01-05 15:59:11.733 [INFO] 2022-01-05 15:59:11

    We can also disable debug information via command line parameters or system environment variables.

    1. Modify the command line startup parameter - gf.glog.debug=false;
    2. Modify the specified environment variable - GF_GLOG_DEBUG=false;