glog支持标准库context.Context接口对象中上下文变量的自动读取打印。
键名配置
我们推荐使用配置文件来对上下文中的键名进行配置,例如:
# 日志组件配置[logger]Path = "/var/log/my-app"Level = "all"Stdout = falseCtxKeys = ["Trace-Id"]
其中CtxKeys用于配置需要从context.Context接口对象中读取并输出的键名。
日志输出
在输出日志的时候,需要通过Ctx链式操作方法指定输出的context.Context接口对象,例如:
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")g.Log().Ctx(ctx).Error("runtime error")// May Output:// 2020-06-08 20:17:03.630 [ERRO] {Trace-Id: 123456789} runtime error// Stack:// ...
