logRpc.go 1.61 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
package rpc

import (
	"encoding/json"
	"fmt"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/mysql"
	"strconv"
	"time"
)

type TypeRpc uint8

const (
//MatchConfirm   TypeRpc = 101
//CallReady      TypeRpc = 102
//AddTimeGift    TypeRpc = 103
//AddTimeFree    TypeRpc = 104
//RecallWindow   TypeRpc = 109
//Video          TypeRpc = 110
//VideoCallReady TypeRpc = 111
)

type RpcLog struct {
	ID       uint64 `gorm:"primary_key"`
	Type     TypeRpc
	UserId   string
	Msg      string
	Err      string
	FailUids string
}

func (RpcLog) TableName() string {
	month := time.Now().Format("200601")
	return fmt.Sprintf("rpc_log_%s", month)
}

func AddRpcLog(t TypeRpc, userId uint64, msg string, failUids []uint64, err error) {
	errStr := ""
	if err != nil {
		errStr = err.Error()
	}
	failUidStr, _ := json.Marshal(failUids)
	logRpc := RpcLog{
		Type:     t,
		UserId:   strconv.FormatUint(userId, 10),
		Msg:      msg,
		Err:      errStr,
		FailUids: string(failUidStr[:]),
	}
	if e := mysql.Db.Table(RpcLog{}.TableName()).Create(&logRpc).Error; e != nil {
		mylogrus.MyLog.Errorf("log rpc save fail, err:%v", e)
	}
}

func AddRpcLogs(t TypeRpc, userIds []uint64, msg string, failUids []uint64, err error) {
	errStr := ""
	if err != nil {
		errStr = err.Error()
	}
	failUidStr, _ := json.Marshal(failUids)
	userIdStr, _ := json.Marshal(userIds)
	logRpc := RpcLog{
		Type:     t,
		UserId:   string(userIdStr[:]),
		Msg:      msg,
		Err:      errStr,
		FailUids: string(failUidStr[:]),
	}
	if e := mysql.Db.Table(RpcLog{}.TableName()).Create(&logRpc).Error; e != nil {
		mylogrus.MyLog.Errorf("log rpc save fail, err:%v", e)
	}
}