1 Star 1 Fork 2

allan577 / go-lib-logger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fields.go 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
allan577 提交于 2021-04-11 21:26 . init
package internal
// Fields type defines the dynamic field collection of the log.
// After Fields are created, their stored keys will not change.
type Fields map[string]interface{}
// Clone returns a cloned Fields.
// If n is given, the returned fields will be pre-expanded with equal capacity.
func (fs Fields) Clone(n int) Fields {
if len(fs) == 0 {
return make(Fields, n)
}
r := make(Fields, len(fs)+n)
for k, v := range fs {
r[k] = v
}
return r
}
// With returns a cloned Fields and adds the given data to it.
func (fs Fields) With(src map[string]interface{}) Fields {
if len(src) == 0 {
return fs.Clone(0)
}
r := fs.Clone(len(src))
for k, v := range src {
r[k] = v
}
return r
}
// StandardiseFieldsForJSONEncoder standardizes the given log fields.
func StandardiseFieldsForJSONEncoder(src map[string]interface{}) map[string]interface{} {
dst := make(map[string]interface{}, len(src))
for k, v := range src {
switch o := v.(type) {
case error:
// The json.Marshal will convert some errors into "{}", we need to call
// the error.Error() method before JSON encoding.
dst[k] = o.Error()
default:
dst[k] = v
}
}
return dst
}
1
https://gitee.com/allan577/go-lib-logger.git
git@gitee.com:allan577/go-lib-logger.git
allan577
go-lib-logger
go-lib-logger
v1.0.0

搜索帮助