support ios push log.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-08 16:56:32 +08:00
parent 227c8ff582
commit 44b7e2c187
3 changed files with 109 additions and 12 deletions

View File

@ -8,3 +8,8 @@ const (
PlatFormIos = iota + 1 PlatFormIos = iota + 1
PlatFormAndroid PlatFormAndroid
) )
const (
StatusSucceededPush = "succeeded-push"
StatusFailedPush = "failed-push"
)

View File

@ -7,6 +7,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"os" "os"
// "time"
) )
var ( var (
@ -28,6 +29,29 @@ type LogReq struct {
Agent string `json:"agent"` 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 { func InitLog() error {
var err error var err error
@ -37,11 +61,13 @@ func InitLog() error {
LogError = logrus.New() LogError = logrus.New()
LogAccess.Formatter = &logrus.TextFormatter{ LogAccess.Formatter = &logrus.TextFormatter{
TimestampFormat: "2006/01/02 - 15:04:05",
ForceColors: true, ForceColors: true,
FullTimestamp: true, FullTimestamp: true,
} }
LogError.Formatter = &logrus.TextFormatter{ LogError.Formatter = &logrus.TextFormatter{
TimestampFormat: "2006/01/02 - 15:04:05",
ForceColors: true, ForceColors: true,
FullTimestamp: true, FullTimestamp: true,
} }
@ -108,12 +134,7 @@ func LogRequest(uri string, method string, ip string, contentType string, agent
} }
if PushConf.Log.Format == "json" { if PushConf.Log.Format == "json" {
logJson, err := json.Marshal(log) logJson, _ := json.Marshal(log)
if err != nil {
LogError.Error("Marshaling JSON error")
return
}
output = string(logJson) output = string(logJson)
} else { } else {
@ -124,6 +145,68 @@ func LogRequest(uri string, method string, ip string, contentType string, agent
LogAccess.Info(output) 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 { func LogMiddleware() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
LogRequest(c.Request.URL.Path, c.Request.Method, c.ClientIP(), c.ContentType(), c.Request.Header.Get("User-Agent")) LogRequest(c.Request.URL.Path, c.Request.Method, c.ClientIP(), c.ContentType(), c.Request.Header.Get("User-Agent"))

View File

@ -229,13 +229,22 @@ func PushToIOS(req RequestPushNotification) bool {
res, err := ApnsClient.Push(notification) res, err := ApnsClient.Push(notification)
if err != nil { 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 return false
} }
if res.Sent() { if res.Sent() {
log.Println("APNs ID:", res.ApnsID) LogPush(StatusSucceededPush, token, req, nil)
} }
} }