英文:
Hide sensitive fields in Uber Zap go
问题
在使用UberGo Zap时,有没有办法隐藏记录敏感字段的方式?如果有人能给出一个示例,我会很感激。
英文:
Is there any way in which I can hide logging sensitive fields while using ubergo zap.
Will appreciate if somebody can show an example
答案1
得分: 3
你可以将这些字段定义为自定义类型,并为它们实现Stringer接口以打印****
。例如:
type X string
func (x X) String() string {
return "****"
}
func main() {
x := X("aaaaa")
log.Infow("msg", "x", x)
}
将打印出msg {"x": "****"}
。
或者你可以实现自己的Encoder
版本,在EncodeEntry(ent Entry, fields []Field)
函数中按名称或它们的类型Interface
过滤字段。你可以以控制台或JSON中的任一现有编码器为基础。例如:
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
type MyEncoder struct {
zapcore.Encoder
}
func (m *MyEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
filtered := make([]zapcore.Field, 0, len(fields))
for _, field := range fields {
if field.Key == "skip" || field.Type == zapcore.Int64Type {
continue
}
filtered = append(filtered, field)
}
return m.Encoder.EncodeEntry(entry, filtered)
}
func main() {
_ = zap.RegisterEncoder("mine", func(config zapcore.EncoderConfig) (zapcore.Encoder, error) {
encoder := zapcore.NewConsoleEncoder(config)
return &MyEncoder{encoder}, nil
})
config := zap.NewDevelopmentConfig()
config.Encoding = "mine"
log, _ := config.Build()
sugar := log.Sugar()
sugar.Infow("Some message", "skip", "skipped", "notskip", "present", "alsoskipping", int64(1))
}
这将打印:
2022-08-24T13:25:54.368+0200 INFO testl/main.go:33 Some message {"notskip": "present"}
英文:
You can make those fields custom type and implement Stringer interface for them printing ****
. For example:
type X string
func (x X) String() string {
return "***"
}
func main() {
x := X("aaaaa")
log.Infow("msg", "x", x)
}
will print msg {"x": "***"}
.
Or you can implement your own version of Encoder
where you would filter fields by name or their type Interface
in EncodeEntry(ent Entry, fields []Field)
function. You can take one of the two existing encoders - console or json - as a base. For example:
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
type MyEncoder struct {
zapcore.Encoder
}
func (m *MyEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
filtered := make([]zapcore.Field, 0, len(fields))
for _, field := range fields {
if field.Key == "skip" || field.Type == zapcore.Int64Type {
continue
}
filtered = append(filtered, field)
}
return m.Encoder.EncodeEntry(entry, filtered)
}
func main() {
_ = zap.RegisterEncoder("mine", func(config zapcore.EncoderConfig) (zapcore.Encoder, error) {
encoder := zapcore.NewConsoleEncoder(config)
return &MyEncoder{encoder}, nil
})
config := zap.NewDevelopmentConfig()
config.Encoding = "mine"
log, _ := config.Build()
sugar := log.Sugar()
sugar.Infow("Some message", "skip", "skipped", "notskip", "present", "alsoskipping", int64(1))
}
This will print
2022-08-24T13:25:54.368+0200 INFO testl/main.go:33 Some message {"notskip": "present"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论