Skip to content

Commit

Permalink
Merge pull request #9 from m-mizutani/enhance/mask-string
Browse files Browse the repository at this point in the history
enhance(filter): Update MaskWithSymbol
  • Loading branch information
m-mizutani authored Sep 9, 2023
2 parents d9e3ee2 + 2fa7908 commit 58307ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 7 additions & 3 deletions redactor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package masq

import (
"fmt"
"reflect"
"strings"
)
Expand Down Expand Up @@ -31,9 +32,12 @@ func RedactString(redact func(s string) string) Redactor {
}
}

// MaskString is a redactor to redact string value with masked string that have the same length as the source string value. It can help the developer to know the length of the string value. The returned Redact function always returns true if the source value is string. Otherwise, it returns false.
func MaskString(masking rune) Redactor {
// MaskWithSymbol is a redactor to redact string value with masked string that have the same length as the source string value. It can help the developer to know the length of the string value. The returned Redact function always returns true if the source value is string. Otherwise, it returns false.
func MaskWithSymbol(symbol rune, max int) Redactor {
return RedactString(func(s string) string {
return strings.Repeat(string(masking), len(s))
if len(s) > max {
return strings.Repeat(string(symbol), max) + fmt.Sprintf(" (remained %d chars)", len(s)-max)
}
return strings.Repeat(string(symbol), len(s))
})
}
11 changes: 7 additions & 4 deletions redactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import (
"github.com/m-mizutani/masq"
)

func ExampleMaskString() {
func ExampleMaskWithSymbol() {
out := &fixedTimeWriter{}

type myRecord struct {
ID string
Phone string
Email string
}
record := myRecord{
ID: "m-mizutani",
Phone: "090-0000-0000",
// too long email address
Email: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@example.com",
}

logger := newLogger(out, masq.New(
// custom redactor
masq.WithFieldName("Phone", masq.MaskString('*')),
masq.WithFieldName("Phone", masq.MaskWithSymbol('*', 32)),
masq.WithFieldName("Email", masq.MaskWithSymbol('*', 12)),
))
logger.With("record", record).Info("Got record")
out.Flush()
// Output:
// {"level":"INFO","msg":"Got record","record":{"ID":"m-mizutani","Phone":"*************"},"time":"2022-12-25T09:00:00.123456789"}
// {"level":"INFO","msg":"Got record","record":{"Email":"************ (remained 36 chars)","ID":"m-mizutani","Phone":"*************"},"time":"2022-12-25T09:00:00.123456789"}
}

0 comments on commit 58307ed

Please sign in to comment.