Skip to content

Commit

Permalink
Merge pull request #19 from m-mizutani/feature/tag-key
Browse files Browse the repository at this point in the history
Add WithCustomTagKey option
  • Loading branch information
m-mizutani authored Nov 12, 2024
2 parents 0077b46 + 4c1a36f commit 8100258
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ logger := slog.New(
ReplaceAttr: masq.New(
// By user defined custom type
masq.WithType[AccessToken](),

// By regex of phone number as e164 format
masq.WithRegex(regexp.MustCompile(`^\+[1-9]\d{1,14}$`)),

// By field tag such as masq:"secret"
masq.WithTag("secret"),

// By by field name prefix. Concealing SecureXxx field
masq.WithFieldPrefix("Secure"),
),
Expand Down Expand Up @@ -168,6 +168,30 @@ out.Flush()
// {"level":"INFO","msg":"Got record","record":{"EMail":"[FILTERED]","ID":"m-mizutani"},"time":"2022-12-25T09:00:00.123456789"}
```

You can change the tag key by `masq.WithCustomTagKey` option.

```go
type myRecord struct {
ID string
EMail string `custom:"secret"`
}

record := myRecord{
ID: "m-mizutani",
EMail: "[email protected]",
}

logger := newLogger(out, masq.New(
masq.WithCustomTagKey("custom"),
masq.WithTag("secret"),
))

logger.With("record", record).Info("Got record")
out.Flush()
// Output:
// {"level":"INFO","msg":"Got record","record":{"EMail":"[REDACTED]","ID":"m-mizutani"},"time":"2022-12-25T09:00:00.123456789"}
```

### With struct field name

```go
Expand Down
2 changes: 1 addition & 1 deletion clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (x *masq) clone(ctx context.Context, fieldName string, src reflect.Value, t
srcValue = reflect.NewAt(srcValue.Type(), unsafe.Pointer(srcValue.UnsafeAddr())).Elem()
}

tagValue := f.Tag.Get("masq")
tagValue := f.Tag.Get(x.tagKey)
copied := x.clone(ctx, f.Name, srcValue, tagValue)
dstValue.Set(copied)
}
Expand Down
6 changes: 6 additions & 0 deletions masq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

const (
// DefaultTagKey is a default key name of struct tag for masq. WithCustomTagKey option can change this value.
DefaultTagKey = "masq"

// DefaultRedactMessage is a default message to replace redacted value. WithRedactMessage option can change this value.
DefaultRedactMessage = "[REDACTED]"
)

Expand All @@ -17,6 +21,7 @@ type masq struct {
allowedTypes map[reflect.Type]struct{}

defaultRedactor Redactor
tagKey string
}

type Filter struct {
Expand All @@ -30,6 +35,7 @@ func newMasq(options ...Option) *masq {
m := &masq{
redactMessage: DefaultRedactMessage,
allowedTypes: map[reflect.Type]struct{}{},
tagKey: DefaultTagKey,
}
m.defaultRedactor = func(src, dst reflect.Value) bool {
switch src.Kind() {
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func WithTag(tag string, redactors ...Redactor) Option {
return WithCensor(newTagCensor(tag), redactors...)
}

// WithCustomTagKey is an option to set the custom tag key. The default tag key is `masq`. If the field has the target tag in the custom tag key AND the field is matched with the target tag specified by WithTag, the field will be redacted. If tagKey is empty, WithCustomTagKey panics.
func WithCustomTagKey(tagKey string) Option {
if tagKey == "" {
panic("masq: tag key must not be empty")
}

return func(m *masq) {
m.tagKey = tagKey
}
}

// WithFieldName is an option to check if the field name is matched with the target field name. If the field name is the target field name, the field will be redacted.
func WithFieldName(fieldName string, redactors ...Redactor) Option {
return WithCensor(newFieldNameCensor(fieldName), redactors...)
Expand Down
34 changes: 34 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ func ExampleWithTag() {
// {"level":"INFO","msg":"Got record","record":{"EMail":"[REDACTED]","ID":"m-mizutani"},"time":"2022-12-25T09:00:00.123456789"}
}

func ExampleWithCustomTagKey() {
out := &fixedTimeWriter{}

type myRecord struct {
ID string
EMail string `custom:"secret"`
}

record := myRecord{
ID: "m-mizutani",
EMail: "[email protected]",
}

logger := newLogger(out, masq.New(
masq.WithCustomTagKey("custom"),
masq.WithTag("secret"),
))

logger.With("record", record).Info("Got record")
out.Flush()
// Output:
// {"level":"INFO","msg":"Got record","record":{"EMail":"[REDACTED]","ID":"m-mizutani"},"time":"2022-12-25T09:00:00.123456789"}
}

func TestCustomTagKeyPanic(t *testing.T) {
defer func() {
if recover() == nil {
t.Errorf("Failed to panic")
}
}()

masq.New(masq.WithCustomTagKey(""))
}

func ExampleWithFieldName() {
out := &fixedTimeWriter{}

Expand Down

0 comments on commit 8100258

Please sign in to comment.