Skip to content

Commit

Permalink
Merge pull request #86 from appleboy/hidetoken
Browse files Browse the repository at this point in the history
Fixed #85 Add hideToken func.
  • Loading branch information
appleboy committed May 16, 2016
2 parents 3ebcbbe + b40bfbe commit a918e55
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ log:
access_level: "debug"
error_log: "stderr" # stderr: output to console, or define log path like "log/error_log"
error_level: "error"
hide_token: true

stat:
engine: "memory" # support memory, redis or boltdb
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type SectionLog struct {
AccessLevel string `yaml:"access_level"`
ErrorLog string `yaml:"error_log"`
ErrorLevel string `yaml:"error_level"`
HideToken bool `yaml:"hide_token"`
}

// SectionStat is sub seciont of config.
Expand Down Expand Up @@ -115,6 +116,7 @@ func BuildDefaultPushConf() ConfYaml {
conf.Log.AccessLevel = "debug"
conf.Log.ErrorLog = "stderr"
conf.Log.ErrorLevel = "error"
conf.Log.HideToken = true

conf.Stat.Engine = "memory"
conf.Stat.Redis.Addr = "localhost:6379"
Expand Down
1 change: 1 addition & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ log:
access_level: "debug"
error_log: "stderr"
error_level: "error"
hide_token: true

stat:
engine: "memory"
Expand Down
24 changes: 23 additions & 1 deletion gorush/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"os"
// "time"
"strings"
)

var (
Expand Down Expand Up @@ -181,6 +181,24 @@ func typeForPlatForm(platform int) string {
}
}

func hideToken(token string, markLen int) string {
if len(token) == 0 {
return ""
}

if len(token) < markLen*2 {
return strings.Repeat("*", len(token))
}

start := token[len(token)-markLen:]
end := token[0:markLen]

result := strings.Replace(token, start, strings.Repeat("*", markLen), -1)
result = strings.Replace(result, end, strings.Repeat("*", markLen), -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
Expand All @@ -193,6 +211,10 @@ func LogPush(status, token string, req PushNotification, errPush error) {
errMsg = errPush.Error()
}

if PushConf.Log.HideToken == true {
token = hideToken(token, 10)
}

log := &LogPushEntry{
Type: status,
Platform: plat,
Expand Down
6 changes: 6 additions & 0 deletions gorush/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ 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))
assert.Equal(t, "*****", hideToken("12345", 10))
}

0 comments on commit a918e55

Please sign in to comment.