在Uber Zap中隐藏敏感字段

huangapple go评论82阅读模式
英文:

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"}

huangapple
  • 本文由 发表于 2022年8月24日 15:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73469128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定