package main
import (
"github.com/gogf/gf/os/glog"
)
func main() {
// 创建一个新的日志对象
logger := glog.New()
// 设置日志输出格式为 JSON
logger.SetConfigWithMap(map[string]interface{}{
"format": "{json}",
"stdoutPrint": true,
})
// 记录日志
logger.Info("This is an info message.")
logger.Error("This is an error message.")
}
在上述示例中,我们使用 SetConfigWithMap 方法设置了日志的输出格式为 JSON。其中 "format": "{json}" 表示输出为 JSON 格式。通过设置这个格式,日志将以 JSON 对象的形式输出。
输出结果类似于:
{"level":"INFO","time":"2023-12-28 09:00:00","file":"example.go:14","msg":"This is an info message."}
{"level":"ERROR","time":"2023-12-28 09:00:01","file":"example.go:15","msg":"This is an error message."}
你可以根据需要进行更多的配置,例如输出到文件、设置日志级别等。
转载请注明出处:http://www.zyzy.cn/article/detail/7553/GoFrame