From 44b7e2c18735c4b6e0529beb27bdf2e2a23f0ded Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 8 Apr 2016 16:56:32 +0800 Subject: [PATCH] support ios push log. Signed-off-by: Bo-Yi Wu --- gorush/const.go | 5 ++ gorush/log.go | 103 +++++++++++++++++++++++++++++++++++++---- gorush/notification.go | 13 +++++- 3 files changed, 109 insertions(+), 12 deletions(-) diff --git a/gorush/const.go b/gorush/const.go index 55e4ecd..6ffb1ff 100644 --- a/gorush/const.go +++ b/gorush/const.go @@ -8,3 +8,8 @@ const ( PlatFormIos = iota + 1 PlatFormAndroid ) + +const ( + StatusSucceededPush = "succeeded-push" + StatusFailedPush = "failed-push" +) diff --git a/gorush/log.go b/gorush/log.go index f4cd3f8..ad07a40 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -7,6 +7,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "os" + // "time" ) var ( @@ -28,6 +29,29 @@ type LogReq struct { Agent string `json:"agent"` } +type LogPushEntry struct { + Type string `json:"type"` + Platform string `json:"platform"` + Token string `json:"token"` + Message string `json:"message"` + Error string `json:"error"` + + // Android + To string `json:"to,omitempty"` + CollapseKey string `json:"collapse_key,omitempty"` + DelayWhileIdle bool `json:"delay_while_idle,omitempty"` + TimeToLive uint `json:"time_to_live,omitempty"` + RestrictedPackageName string `json:"restricted_package_name,omitempty"` + DryRun bool `json:"dry_run,omitempty"` + + // iOS + ApnsID string `json:"apns_id,omitempty"` + Topic string `json:"topic,omitempty"` + Badge int `json:"badge,omitempty"` + Sound string `json:"sound,omitempty"` + Category string `json:"category,omitempty"` +} + func InitLog() error { var err error @@ -37,13 +61,15 @@ func InitLog() error { LogError = logrus.New() LogAccess.Formatter = &logrus.TextFormatter{ - ForceColors: true, - FullTimestamp: true, + TimestampFormat: "2006/01/02 - 15:04:05", + ForceColors: true, + FullTimestamp: true, } LogError.Formatter = &logrus.TextFormatter{ - ForceColors: true, - FullTimestamp: true, + TimestampFormat: "2006/01/02 - 15:04:05", + ForceColors: true, + FullTimestamp: true, } // set logger @@ -108,12 +134,7 @@ func LogRequest(uri string, method string, ip string, contentType string, agent } if PushConf.Log.Format == "json" { - logJson, err := json.Marshal(log) - - if err != nil { - LogError.Error("Marshaling JSON error") - return - } + logJson, _ := json.Marshal(log) output = string(logJson) } else { @@ -124,6 +145,68 @@ func LogRequest(uri string, method string, ip string, contentType string, agent LogAccess.Info(output) } +func colorForPlatForm(platform int) string { + switch platform { + case PlatFormIos: + return blue + case PlatFormAndroid: + return cyan + default: + return reset + } +} + +func typeForPlatForm(platform int) string { + switch platform { + case PlatFormIos: + return "ios" + case PlatFormAndroid: + return "android" + default: + return "" + } +} + +func LogPush(status, token string, req RequestPushNotification, errPush error) { + var plat, platColor, output string + + platColor = colorForPlatForm(req.Platform) + plat = typeForPlatForm(req.Platform) + + errMsg := "" + if errPush != nil { + errMsg = errPush.Error() + } + + log := &LogPushEntry{ + Type: status, + Platform: plat, + Token: token, + Message: req.Message, + Error: errMsg, + } + + if PushConf.Log.Format == "json" { + logJson, _ := json.Marshal(log) + + output = string(logJson) + } else { + switch status { + case StatusSucceededPush: + output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] %s", green, log.Type, reset, platColor, log.Platform, reset, log.Token, log.Message) + case StatusFailedPush: + output = fmt.Sprintf("|%s %s %s| %s%s%s [%s] | %s | Error Message: %s", red, log.Type, reset, platColor, log.Platform, reset, log.Token, log.Message, log.Error) + } + } + + switch status { + case StatusSucceededPush: + LogAccess.Info(string(output)) + case StatusFailedPush: + LogError.Error(string(output)) + } +} + func LogMiddleware() gin.HandlerFunc { return func(c *gin.Context) { LogRequest(c.Request.URL.Path, c.Request.Method, c.ClientIP(), c.ContentType(), c.Request.Header.Get("User-Agent")) diff --git a/gorush/notification.go b/gorush/notification.go index f98b10e..6619bd9 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -229,13 +229,22 @@ func PushToIOS(req RequestPushNotification) bool { res, err := ApnsClient.Push(notification) if err != nil { - log.Println("There was an error: ", err) + // apns server error + LogPush(StatusFailedPush, token, req, err) + + return false + } + + if res.StatusCode != 200 { + // error message: + // ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65 + LogPush(StatusFailedPush, token, req, errors.New(res.Reason)) return false } if res.Sent() { - log.Println("APNs ID:", res.ApnsID) + LogPush(StatusSucceededPush, token, req, nil) } }