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.
package main
import (
"context"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)
func main() {
ctx := context.TODO()
gtimer.SetTimeout(ctx, 3*time.Second, func(ctx context.Context) {
g.Log().SetDebug(false)
})
for {
g.Log().Debug(ctx, gtime.Datetime())
g.Log().Info(ctx, gtime.Datetime())
time.Sleep(time.Second)
}
}
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.
2022-01-05 15:59:05.674 [DEBU] 2022-01-05 15:59:05
2022-01-05 15:59:05.675 [INFO] 2022-01-05 15:59:05
2022-01-05 15:59:06.684 [DEBU] 2022-01-05 15:59:06
2022-01-05 15:59:06.684 [INFO] 2022-01-05 15:59:06
2022-01-05 15:59:07.692 [DEBU] 2022-01-05 15:59:07
2022-01-05 15:59:07.692 [INFO] 2022-01-05 15:59:07
2022-01-05 15:59:08.708 [INFO] 2022-01-05 15:59:08
2022-01-05 15:59:09.717 [INFO] 2022-01-05 15:59:09
2022-01-05 15:59:10.728 [INFO] 2022-01-05 15:59: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.
- Modify the command line startup parameter -
gf.glog.debug=false
;- Modify the specified environment variable -
GF_GLOG_DEBUG=false
;