代码拉取完成,页面将自动刷新
log
是一个生产可用的日志包,基于 zap
包封装。除了实现 Go
日志包的基本功能外,还实现了很多高级功能,log
具有如下特性:
Debug
、Info
、Warn
、Error
、Panic
、Fatal
。JSON
和 Text
两种日志格式。log
包。log
包和 glog
。创建一个 example2.go
文件,内容如下:
package main
import (
"gitee.com/amwfhv/tge/log"
)
func main() {
defer log.Flush()
// Debug、Info(with field)、Warnf、Errorw使用
log.Debug("This is a debug message")
log.Info("This is a info message", log.Int32("int_key", 10))
log.Warnf("This is a formatted %s message", "warn")
}
执行代码:
$ go run example2.go
2020-12-05 07:56:37.154 info example/example2.go:12 This is a info message {"int_key": 10}
2020-12-05 07:56:37.154 warn example/example2.go:13 This is a formatted warn message
上述代码使用 log
包默认的全局 logger
,分别在 Debug
、Info
和 Warn
级别打印了一条日志。
可以使用 Init
来初始化一个日志包,如下:
// logger配置
opts := &log.Options{
Level: "debug",
Format: "console",
EnableColor: true,
EnableCaller: true,
OutputPaths: []string{"test.log", "stdout"},
ErrorOutputPaths: []string{"error.log"},
}
// 初始化全局logger
log.Init(opts)
Format 支持 console
和 json
2 种格式:
2020-12-05 08:12:02.324 DEBUG example/example.go:43 This is a debug message
{"level":"debug","time":"2020-12-05 08:12:54.113","caller":"example/example.go:43","msg":"This is a debug message"}
OutputPaths,可以设置日志输出:
支持同时输出到多个输出。
EnableColor 为 true
开启颜色输出,为 false
关闭颜色输出。
log
也支持结构化日志打印,例如:
log.Info("This is a info message", log.Int32("int_key", 10))
log.Infow("Message printed with Errorw", "X-Request-ID", "fbf54504-64da-4088-9b86-67824a7fb508")
对应的输出结果为:
2020-12-05 08:16:18.749 INFO example/example.go:44 This is a info message {"int_key": 10}
2020-12-05 08:16:18.749 ERROR example/example.go:46 Message printed with Errorw {"X-Request-ID": "fbf54504-64da-4088-9b86-67824a7fb508"}
log.Info 这类函数需要指定具体的类型,以最大化的 提高日志的性能。log.Infow 这类函数,不用指定具体的类型,底层使用了反射,性能会差些。建议用在低频调用的函数中。
创建 v_level.go
,内容如下:
package main
import (
"gitee.com/amwfhv/tge/log"
)
func main() {
defer log.Flush()
log.V(0).Info("This is a V level message")
log.V(0).Infow("This is a V level message with fields", "X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
}
执行如上代码:
$ go run v_level.go
2020-12-05 08:20:37.763 info example/v_level.go:10 This is a V level message
2020-12-05 08:20:37.763 info example/v_level.go:11 This is a V level message with fields {"X-Request-ID": "7a7b9f24-4cae-4b2a-9464-69088b45b904"}
一个完整的示例请参考example.go。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。