game_sync/mgrsrv/api/logchannel.go

58 lines
1.1 KiB
Go

package api
import (
"reflect"
"mongo.games.com/game/model"
"mongo.games.com/game/mq"
)
/*
api调用记录写入rabbitmq,然后dbproxy收到rabbitmq数据再写到数据库
*/
const (
LogChanelBlackHole = "_null_"
)
var LogChannelSington = &LogChannel{
cName: make(map[reflect.Type]string),
}
type LogChannel struct {
cName map[reflect.Type]string
}
func (c *LogChannel) RegisteLogCName(cname string, log interface{}) {
t := c.getLogType(log)
c.cName[t] = cname
}
func (c *LogChannel) getLogType(log interface{}) reflect.Type {
return reflect.Indirect(reflect.ValueOf(log)).Type()
}
func (c *LogChannel) getLogCName(log interface{}) string {
t := c.getLogType(log)
if name, exist := c.cName[t]; exist {
return name
}
return ""
}
func (c *LogChannel) WriteLog(log interface{}) {
cname := c.getLogCName(log)
if cname == "" {
cname = LogChanelBlackHole
}
mq.Send(cname, log)
}
func (c *LogChannel) WriteMQData(data *model.RabbitMQData) {
mq.Send(data.MQName, data.Data)
}
func init() {
LogChannelSington.RegisteLogCName(model.APILogCollName, &model.APILog{})
}