refactor(jpush): add structured logging to JPush client methods
Add zap logger calls to Track, PushByRegistrationIDs, PushByAliases, PushAll, and GetDeviceInfo methods for improved observability. Logs include request debug info, response parsing status, success confirmations, and error tracking with relevant context like message IDs, counts, and registration IDs.
This commit is contained in:
@@ -193,10 +193,24 @@ func (c *Client) Push(payload map[string]any) (*PushResponse, *RateLimitInfo, er
|
|||||||
|
|
||||||
resp, respBody, rateLimit, err := c.doRequest("POST", PushAPIBaseURL+"/push", payload)
|
resp, respBody, rateLimit, err := c.doRequest("POST", PushAPIBaseURL+"/push", payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.logger.Error("jpush push request failed",
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
return nil, rateLimit, err
|
return nil, rateLimit, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, parseErr := c.parseResponse(resp, respBody)
|
result, parseErr := c.parseResponse(resp, respBody)
|
||||||
|
if parseErr != nil {
|
||||||
|
c.logger.Error("jpush push response parse failed",
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.Error(parseErr),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
c.logger.Debug("jpush push response",
|
||||||
|
zap.String("msg_id", result.MsgID),
|
||||||
|
zap.Int64("send_no", result.SendNo),
|
||||||
|
)
|
||||||
|
}
|
||||||
return result, rateLimit, parseErr
|
return result, rateLimit, parseErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,6 +222,12 @@ func (c *Client) PushByRegistrationIDs(registrationIDs []string, notification *N
|
|||||||
return nil, fmt.Errorf("registration IDs exceed 1000 limit")
|
return nil, fmt.Errorf("registration IDs exceed 1000 limit")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush push by registration_ids",
|
||||||
|
zap.Int("count", len(registrationIDs)),
|
||||||
|
zap.String("title", notification.Android.Title),
|
||||||
|
zap.String("alert", notification.Alert),
|
||||||
|
)
|
||||||
|
|
||||||
mergeExtras(notification, extras)
|
mergeExtras(notification, extras)
|
||||||
|
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
@@ -221,7 +241,19 @@ func (c *Client) PushByRegistrationIDs(registrationIDs []string, notification *N
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, _, err := c.Push(payload)
|
result, _, err := c.Push(payload)
|
||||||
return result, err
|
if err != nil {
|
||||||
|
c.logger.Error("jpush push by registration_ids failed",
|
||||||
|
zap.Int("count", len(registrationIDs)),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("jpush push by registration_ids success",
|
||||||
|
zap.Int("count", len(registrationIDs)),
|
||||||
|
zap.String("msg_id", result.MsgID),
|
||||||
|
)
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PushByAliases(aliases []string, notification *Notification, extras map[string]any) (*PushResponse, error) {
|
func (c *Client) PushByAliases(aliases []string, notification *Notification, extras map[string]any) (*PushResponse, error) {
|
||||||
@@ -232,6 +264,12 @@ func (c *Client) PushByAliases(aliases []string, notification *Notification, ext
|
|||||||
return nil, fmt.Errorf("aliases exceed 1000 limit")
|
return nil, fmt.Errorf("aliases exceed 1000 limit")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush push by aliases",
|
||||||
|
zap.Int("count", len(aliases)),
|
||||||
|
zap.String("title", notification.Android.Title),
|
||||||
|
zap.String("alert", notification.Alert),
|
||||||
|
)
|
||||||
|
|
||||||
mergeExtras(notification, extras)
|
mergeExtras(notification, extras)
|
||||||
|
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
@@ -245,10 +283,27 @@ func (c *Client) PushByAliases(aliases []string, notification *Notification, ext
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, _, err := c.Push(payload)
|
result, _, err := c.Push(payload)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("jpush push by aliases failed",
|
||||||
|
zap.Int("count", len(aliases)),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("jpush push by aliases success",
|
||||||
|
zap.Int("count", len(aliases)),
|
||||||
|
zap.String("msg_id", result.MsgID),
|
||||||
|
)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PushAll(notification *Notification) (*PushResponse, error) {
|
func (c *Client) PushAll(notification *Notification) (*PushResponse, error) {
|
||||||
|
c.logger.Info("jpush push all",
|
||||||
|
zap.String("title", notification.Android.Title),
|
||||||
|
zap.String("alert", notification.Alert),
|
||||||
|
)
|
||||||
|
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
"platform": "all",
|
"platform": "all",
|
||||||
"audience": "all",
|
"audience": "all",
|
||||||
@@ -260,6 +315,16 @@ func (c *Client) PushAll(notification *Notification) (*PushResponse, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, _, err := c.Push(payload)
|
result, _, err := c.Push(payload)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("jpush push all failed",
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("jpush push all success",
|
||||||
|
zap.String("msg_id", result.MsgID),
|
||||||
|
)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,6 +390,10 @@ type DeviceInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetDeviceInfo(registrationID string) (*DeviceInfo, error) {
|
func (c *Client) GetDeviceInfo(registrationID string) (*DeviceInfo, error) {
|
||||||
|
c.logger.Debug("jpush get device info",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
||||||
resp, body, _, err := c.doRequest("GET", path, nil)
|
resp, body, _, err := c.doRequest("GET", path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -332,6 +401,10 @@ func (c *Client) GetDeviceInfo(registrationID string) (*DeviceInfo, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.logger.Error("jpush get device info failed",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
)
|
||||||
return nil, fmt.Errorf("jpush get device info failed: HTTP %d", resp.StatusCode)
|
return nil, fmt.Errorf("jpush get device info failed: HTTP %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,10 +412,21 @@ func (c *Client) GetDeviceInfo(registrationID string) (*DeviceInfo, error) {
|
|||||||
if err := json.Unmarshal(body, &info); err != nil {
|
if err := json.Unmarshal(body, &info); err != nil {
|
||||||
return nil, fmt.Errorf("unmarshal device info: %w", err)
|
return nil, fmt.Errorf("unmarshal device info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush get device info success",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.String("alias", info.Alias),
|
||||||
|
zap.Strings("tags", info.Tags),
|
||||||
|
)
|
||||||
return &info, nil
|
return &info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetDeviceAlias(registrationID string, alias string) error {
|
func (c *Client) SetDeviceAlias(registrationID string, alias string) error {
|
||||||
|
c.logger.Info("jpush set device alias",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
"alias": alias,
|
"alias": alias,
|
||||||
@@ -354,12 +438,29 @@ func (c *Client) SetDeviceAlias(registrationID string, alias string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.logger.Error("jpush set device alias failed",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.String("response", string(body)),
|
||||||
|
)
|
||||||
return fmt.Errorf("jpush set device alias failed: HTTP %d: %s", resp.StatusCode, string(body))
|
return fmt.Errorf("jpush set device alias failed: HTTP %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush set device alias success",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetDeviceTags(registrationID string, addTags, removeTags []string) error {
|
func (c *Client) SetDeviceTags(registrationID string, addTags, removeTags []string) error {
|
||||||
|
c.logger.Info("jpush set device tags",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.Strings("add_tags", addTags),
|
||||||
|
zap.Strings("remove_tags", removeTags),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
path := DeviceAPIBaseURL + "/v3/devices/" + url.PathEscape(registrationID)
|
||||||
tags := map[string]any{}
|
tags := map[string]any{}
|
||||||
if len(addTags) > 0 {
|
if len(addTags) > 0 {
|
||||||
@@ -379,12 +480,25 @@ func (c *Client) SetDeviceTags(registrationID string, addTags, removeTags []stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.logger.Error("jpush set device tags failed",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.String("response", string(body)),
|
||||||
|
)
|
||||||
return fmt.Errorf("jpush set device tags failed: HTTP %d: %s", resp.StatusCode, string(body))
|
return fmt.Errorf("jpush set device tags failed: HTTP %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush set device tags success",
|
||||||
|
zap.String("registration_id", registrationID),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetAliasRegistrationIDs(alias string) ([]string, error) {
|
func (c *Client) GetAliasRegistrationIDs(alias string) ([]string, error) {
|
||||||
|
c.logger.Debug("jpush get alias",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
||||||
resp, body, _, err := c.doRequest("GET", path, nil)
|
resp, body, _, err := c.doRequest("GET", path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -392,6 +506,10 @@ func (c *Client) GetAliasRegistrationIDs(alias string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.logger.Error("jpush get alias failed",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
)
|
||||||
return nil, fmt.Errorf("jpush get alias failed: HTTP %d", resp.StatusCode)
|
return nil, fmt.Errorf("jpush get alias failed: HTTP %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,10 +519,19 @@ func (c *Client) GetAliasRegistrationIDs(alias string) ([]string, error) {
|
|||||||
if err := json.Unmarshal(body, &result); err != nil {
|
if err := json.Unmarshal(body, &result); err != nil {
|
||||||
return nil, fmt.Errorf("unmarshal alias response: %w", err)
|
return nil, fmt.Errorf("unmarshal alias response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush get alias success",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("count", len(result.RegistrationIDs)),
|
||||||
|
)
|
||||||
return result.RegistrationIDs, nil
|
return result.RegistrationIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteAlias(alias string) error {
|
func (c *Client) DeleteAlias(alias string) error {
|
||||||
|
c.logger.Info("jpush delete alias",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
||||||
resp, body, _, err := c.doRequest("DELETE", path, nil)
|
resp, body, _, err := c.doRequest("DELETE", path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -412,12 +539,26 @@ func (c *Client) DeleteAlias(alias string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
||||||
|
c.logger.Error("jpush delete alias failed",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.String("response", string(body)),
|
||||||
|
)
|
||||||
return fmt.Errorf("jpush delete alias failed: HTTP %d: %s", resp.StatusCode, string(body))
|
return fmt.Errorf("jpush delete alias failed: HTTP %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush delete alias success",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) RemoveAliasDevices(alias string, registrationIDs []string) error {
|
func (c *Client) RemoveAliasDevices(alias string, registrationIDs []string) error {
|
||||||
|
c.logger.Info("jpush remove alias devices",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("count", len(registrationIDs)),
|
||||||
|
)
|
||||||
|
|
||||||
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
path := DeviceAPIBaseURL + "/v3/aliases/" + url.PathEscape(alias)
|
||||||
payload := map[string]any{
|
payload := map[string]any{
|
||||||
"registration_ids": map[string]any{
|
"registration_ids": map[string]any{
|
||||||
@@ -431,7 +572,16 @@ func (c *Client) RemoveAliasDevices(alias string, registrationIDs []string) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
c.logger.Error("jpush remove alias devices failed",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.String("response", string(body)),
|
||||||
|
)
|
||||||
return fmt.Errorf("jpush remove alias devices failed: HTTP %d: %s", resp.StatusCode, string(body))
|
return fmt.Errorf("jpush remove alias devices failed: HTTP %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.logger.Debug("jpush remove alias devices success",
|
||||||
|
zap.String("alias", alias),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user