diff --git a/gorush/log.go b/gorush/log.go index 42486f9..e22a76a 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -7,7 +7,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "os" - // "time" + "strings" ) var ( @@ -181,6 +181,20 @@ func typeForPlatForm(platform int) string { } } +func hideToken(token string, starLen int) string { + if len(token) == 0 { + return "" + } + + start := token[len(token)-starLen:] + end := token[0:starLen] + + result := strings.Replace(token, start, strings.Repeat("*", starLen), -1) + result = strings.Replace(result, end, strings.Repeat("*", starLen), -1) + + return result +} + // LogPush record user push request and server response. func LogPush(status, token string, req PushNotification, errPush error) { var plat, platColor, output string diff --git a/gorush/log_test.go b/gorush/log_test.go index abbb698..8542d25 100644 --- a/gorush/log_test.go +++ b/gorush/log_test.go @@ -88,3 +88,8 @@ func TestPlatFormColor(t *testing.T) { assert.Equal(t, yellow, colorForPlatForm(PlatFormAndroid)) assert.Equal(t, reset, colorForPlatForm(1000000)) } + +func TestHideToken(t *testing.T) { + assert.Equal(t, "", hideToken("", 2)) + assert.Equal(t, "**345678**", hideToken("1234567890", 2)) +}