mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Log add automatic cutting function
This commit is contained in:
parent
536f511b38
commit
fbb079fb8c
@ -8,10 +8,10 @@ etcd:
|
|||||||
etcdAddr: [ 127.0.0.1:2379 ]
|
etcdAddr: [ 127.0.0.1:2379 ]
|
||||||
|
|
||||||
mysql:
|
mysql:
|
||||||
dbAddress: [ 127.0.0.1:3306 ]
|
dbMysqlAddress: [ 127.0.0.1:3306 ]
|
||||||
dbUserName: root
|
dbMysqlUserName: root
|
||||||
dbPassword: 123456
|
dbMysqlPassword: 123456
|
||||||
dbDatabaseName: openIM
|
dbMysqlDatabaseName: openIM
|
||||||
dbTableName: eMsg
|
dbTableName: eMsg
|
||||||
dbMsgTableNum: 1
|
dbMsgTableNum: 1
|
||||||
dbMaxOpenConns: 20
|
dbMaxOpenConns: 20
|
||||||
@ -90,6 +90,8 @@ rpcregistername:
|
|||||||
|
|
||||||
log:
|
log:
|
||||||
storageLocation: ../logs/
|
storageLocation: ../logs/
|
||||||
|
rotationTime: 12
|
||||||
|
remainRotationCount: 10
|
||||||
elasticSearchSwitch: false
|
elasticSearchSwitch: false
|
||||||
elasticSearchAddr: [ 47.112.160.66:9201 ]
|
elasticSearchAddr: [ 47.112.160.66:9201 ]
|
||||||
elasticSearchUser: ""
|
elasticSearchUser: ""
|
||||||
@ -128,3 +130,7 @@ tokenpolicy:
|
|||||||
# Token effective time seconds as a unit
|
# Token effective time seconds as a unit
|
||||||
#Seven days 7*24*60*60
|
#Seven days 7*24*60*60
|
||||||
accessExpire: 604800
|
accessExpire: 604800
|
||||||
|
|
||||||
|
messagecallback:
|
||||||
|
callbackSwitch: false
|
||||||
|
callbackUrl: "http://www.xxx.com/msg/judge"
|
@ -83,8 +83,8 @@ func UserSendMsg(c *gin.Context) {
|
|||||||
log.Info("", "", "api UserSendMsg call end..., [data: %s] [reply: %s]", pbData.String(), reply.String())
|
log.Info("", "", "api UserSendMsg call end..., [data: %s] [reply: %s]", pbData.String(), reply.String())
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"errCode": 0,
|
"errCode": reply.ErrCode,
|
||||||
"errMsg": "",
|
"errMsg": reply.ErrMsg,
|
||||||
"msgIncr": reply.MsgIncr,
|
"msgIncr": reply.MsgIncr,
|
||||||
"reqIdentifier": reply.ReqIdentifier,
|
"reqIdentifier": reply.ReqIdentifier,
|
||||||
"data": gin.H{
|
"data": gin.H{
|
||||||
|
@ -25,10 +25,10 @@ type config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Mysql struct {
|
Mysql struct {
|
||||||
DBAddress []string `yaml:"dbAddress"`
|
DBAddress []string `yaml:"dbMysqlAddress"`
|
||||||
DBUserName string `yaml:"dbUserName"`
|
DBUserName string `yaml:"dbMysqlUserName"`
|
||||||
DBPassword string `yaml:"dbPassword"`
|
DBPassword string `yaml:"dbMysqlPassword"`
|
||||||
DBDatabaseName string `yaml:"dbDatabaseName"`
|
DBDatabaseName string `yaml:"dbMysqlDatabaseName"`
|
||||||
DBTableName string `yaml:"DBTableName"`
|
DBTableName string `yaml:"DBTableName"`
|
||||||
DBMsgTableNum int `yaml:"dbMsgTableNum"`
|
DBMsgTableNum int `yaml:"dbMsgTableNum"`
|
||||||
DBMaxOpenConns int `yaml:"dbMaxOpenConns"`
|
DBMaxOpenConns int `yaml:"dbMaxOpenConns"`
|
||||||
@ -77,6 +77,8 @@ type config struct {
|
|||||||
}
|
}
|
||||||
Log struct {
|
Log struct {
|
||||||
StorageLocation string `yaml:"storageLocation"`
|
StorageLocation string `yaml:"storageLocation"`
|
||||||
|
RotationTime int `yaml:"rotationTime"`
|
||||||
|
RemainRotationCount uint `yaml:"remainRotationCount"`
|
||||||
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
||||||
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
||||||
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
||||||
@ -134,6 +136,10 @@ type config struct {
|
|||||||
AccessSecret string `yaml:"accessSecret"`
|
AccessSecret string `yaml:"accessSecret"`
|
||||||
AccessExpire int64 `yaml:"accessExpire"`
|
AccessExpire int64 `yaml:"accessExpire"`
|
||||||
}
|
}
|
||||||
|
MessageCallBack struct {
|
||||||
|
CallbackSwitch bool `yaml:"callbackSwitch"`
|
||||||
|
CallbackUrl string `yaml:"callbackUrl"`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -44,56 +44,19 @@ func loggerInit(moduleName string) *Logger {
|
|||||||
logger.AddHook(newEsHook(moduleName))
|
logger.AddHook(newEsHook(moduleName))
|
||||||
}
|
}
|
||||||
//Log file segmentation hook
|
//Log file segmentation hook
|
||||||
hook := NewLfsHook(config.Config.Log.StorageLocation+time.Now().Format("2006-01-02")+".log", 0, 5, moduleName)
|
hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName)
|
||||||
logger.AddHook(hook)
|
logger.AddHook(hook)
|
||||||
return &Logger{
|
return &Logger{
|
||||||
logger,
|
logger,
|
||||||
os.Getpid(),
|
os.Getpid(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func NewLfsHook(logName string, rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
|
func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
|
||||||
var fileNameSuffix string
|
|
||||||
if GetCurrentTimestamp() >= GetCurDayZeroTimestamp() && GetCurrentTimestamp() <= GetCurDayHalfTimestamp() {
|
|
||||||
fileNameSuffix = time.Now().Format("2006-01-02") + ".log"
|
|
||||||
} else {
|
|
||||||
fileNameSuffix = time.Now().Format("2006-01-02") + ".log"
|
|
||||||
}
|
|
||||||
writer, err := rotatelogs.New(
|
|
||||||
logName,
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
writeInfo, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+"/info."+fileNameSuffix,
|
|
||||||
rotatelogs.WithRotationTime(time.Duration(60)*time.Second),
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
writeError, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+"/error."+fileNameSuffix,
|
|
||||||
rotatelogs.WithRotationTime(time.Minute),
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
writeDebug, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+"/debug."+fileNameSuffix,
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
writeWarn, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+"/warn."+fileNameSuffix,
|
|
||||||
rotatelogs.WithRotationTime(time.Minute),
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
lfsHook := lfshook.NewHook(lfshook.WriterMap{
|
lfsHook := lfshook.NewHook(lfshook.WriterMap{
|
||||||
logrus.DebugLevel: writeDebug,
|
logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "debug", moduleName),
|
||||||
logrus.InfoLevel: writeInfo,
|
logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "info", moduleName),
|
||||||
logrus.WarnLevel: writeWarn,
|
logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "warn", moduleName),
|
||||||
logrus.ErrorLevel: writeError,
|
logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "error", moduleName),
|
||||||
logrus.FatalLevel: writer,
|
|
||||||
logrus.PanicLevel: writer,
|
|
||||||
}, &nested.Formatter{
|
}, &nested.Formatter{
|
||||||
TimestampFormat: "2006-01-02 15:04:05",
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
HideKeys: false,
|
HideKeys: false,
|
||||||
@ -102,6 +65,18 @@ func NewLfsHook(logName string, rotationTime time.Duration, maxRemainNum uint, m
|
|||||||
|
|
||||||
return lfsHook
|
return lfsHook
|
||||||
}
|
}
|
||||||
|
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
|
||||||
|
writer, err := rotatelogs.New(
|
||||||
|
config.Config.Log.StorageLocation+moduleName+"/"+level+"."+"%Y-%m-%d_%H-%M-%S",
|
||||||
|
rotatelogs.WithRotationTime(rotationTime),
|
||||||
|
rotatelogs.WithRotationCount(maxRemainNum),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
return writer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Info(token, OperationID, format string, args ...interface{}) {
|
func Info(token, OperationID, format string, args ...interface{}) {
|
||||||
if token == "" && OperationID == "" {
|
if token == "" && OperationID == "" {
|
||||||
|
@ -77,6 +77,7 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
|
|||||||
case constant.SyncSenderMsg:
|
case constant.SyncSenderMsg:
|
||||||
log.InfoByKv("come sync", in.OperationID, "args", in.String())
|
log.InfoByKv("come sync", in.OperationID, "args", in.String())
|
||||||
RecvID = in.GetSendID()
|
RecvID = in.GetSendID()
|
||||||
|
if in.MsgFrom != constant.SysMsgType {
|
||||||
for key, conn := range ws.wsUserToConn {
|
for key, conn := range ws.wsUserToConn {
|
||||||
UIDAndPID := strings.Split(key, " ")
|
UIDAndPID := strings.Split(key, " ")
|
||||||
if UIDAndPID[0] == RecvID && utils.PlatformIDToName(in.GetPlatformID()) != UIDAndPID[1] {
|
if UIDAndPID[0] == RecvID && utils.PlatformIDToName(in.GetPlatformID()) != UIDAndPID[1] {
|
||||||
@ -90,6 +91,7 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
log.InfoByKv("not come sync", in.OperationID, "args", in.String())
|
log.InfoByKv("not come sync", in.OperationID, "args", in.String())
|
||||||
switch in.SessionType {
|
switch in.SessionType {
|
||||||
@ -120,25 +122,6 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (r *RPCServer) SendMsgByWS(_ context.Context, in *pbRelay.SendMsgByWSReq) (*pbRelay.MsgToUserResp, error) {
|
|
||||||
// log.InfoByKv("SendMsgByWS is arriving ", in.OperationID, "args", in.String())
|
|
||||||
// resp := new(pbRelay.MsgToUserResp)
|
|
||||||
// MsgId := ws.genMsgNum()
|
|
||||||
// pbData := pbMsg.WSToMsgSvrChatMsg{}
|
|
||||||
// pbData.SendID = in.SendID
|
|
||||||
// pbData.RecvID = in.RecvID
|
|
||||||
// pbData.MsgID = MsgId
|
|
||||||
// pbData.SessionType = in.SessionType
|
|
||||||
// pbData.MsgFrom = in.MsgFrom
|
|
||||||
// pbData.Content = in.Content
|
|
||||||
// pbData.ContentType = in.ContentType
|
|
||||||
// pbData.OperationID = in.OperationID
|
|
||||||
// pbData.SendTime = in.SendTime
|
|
||||||
// pbData.PlatformID = in.PlatformID
|
|
||||||
// pKafka.writeMsg(&pbData)
|
|
||||||
// return resp, nil
|
|
||||||
//}
|
|
||||||
|
|
||||||
func sendMsgToUser(conn *websocket.Conn, bMsg []byte, in *pbRelay.MsgToUserReq, RecvPlatForm, RecvID string) (ResultCode int64) {
|
func sendMsgToUser(conn *websocket.Conn, bMsg []byte, in *pbRelay.MsgToUserReq, RecvPlatForm, RecvID string) (ResultCode int64) {
|
||||||
err := ws.writeMsg(conn, websocket.TextMessage, bMsg)
|
err := ws.writeMsg(conn, websocket.TextMessage, bMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//实现pb定义的rpc服务
|
|
||||||
package rpcChat
|
package rpcChat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -33,37 +32,6 @@ func (rpc *rpcChat) GetNewSeq(_ context.Context, in *pbMsg.GetNewSeqReq) (*pbMsg
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (s *MsgServer) PullMessage(_ context.Context, in *pbMsg.PullMessageReq) (*pbMsg.PullMessageResp, error) {
|
|
||||||
// log.InfoByArgs(fmt.Sprintf("rpc pullMessage is arriving,args=%s", in.String()))
|
|
||||||
// resp := new(pbMsg.PullMessageResp)
|
|
||||||
// var respMsgFormat []*pbMsg.MsgFormat
|
|
||||||
// var respUserMsgFormat []*pbMsg.UserMsgFormat
|
|
||||||
// conn := db.NewDbConnection()
|
|
||||||
// rows, err := conn.Table("receive r").Select("c.sender_id,c.receiver_id,"+
|
|
||||||
// "c.msg_type,c.push_msg_type,c.chat_type,c.msg_id,c.send_content,r.seq,c.send_time,c.sender_nickname,c.receiver_nickname,c.sender_head_url,c.receiver_head_url").
|
|
||||||
// Joins("INNER JOIN chat_log c ON r.msg_id = c.msg_id AND r.user_id = ? AND seq BETWEEN ? AND ?",
|
|
||||||
// in.UserID, in.SeqBegin, in.SeqEnd).Rows()
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Printf("pullMsg data error: %v\n", err)
|
|
||||||
// resp.ErrCode = 1
|
|
||||||
// resp.ErrMsg = err.Error()
|
|
||||||
// return resp, nil
|
|
||||||
// }
|
|
||||||
// defer rows.Close()
|
|
||||||
// for rows.Next() {
|
|
||||||
// tempResp := new(pbMsg.MsgFormat)
|
|
||||||
// rows.Scan(&tempResp.SendID, &tempResp.RecvID, &tempResp.MsgType, &tempResp.PushMsgType, &tempResp.ChatType,
|
|
||||||
// &tempResp.MsgID, &tempResp.Msg, &tempResp.Seq, &tempResp.Time, &tempResp.SendNickName, &tempResp.RecvNickName,
|
|
||||||
// &tempResp.SendHeadUrl, &tempResp.RecvHeadUrl)
|
|
||||||
// respMsgFormat = append(respMsgFormat, tempResp)
|
|
||||||
// }
|
|
||||||
// respUserMsgFormat = msgHandleByUser(respMsgFormat, in.UserID)
|
|
||||||
// return &pbMsg.PullMessageResp{
|
|
||||||
// ErrCode: 0,
|
|
||||||
// ErrMsg: "",
|
|
||||||
// UserMsg: respUserMsgFormat,
|
|
||||||
// }, nil
|
|
||||||
//}
|
|
||||||
func (rpc *rpcChat) PullMessage(_ context.Context, in *pbMsg.PullMessageReq) (*pbMsg.PullMessageResp, error) {
|
func (rpc *rpcChat) PullMessage(_ context.Context, in *pbMsg.PullMessageReq) (*pbMsg.PullMessageResp, error) {
|
||||||
log.InfoByKv("rpc pullMessage is arriving", in.OperationID, "args", in.String())
|
log.InfoByKv("rpc pullMessage is arriving", in.OperationID, "args", in.String())
|
||||||
resp := new(pbMsg.PullMessageResp)
|
resp := new(pbMsg.PullMessageResp)
|
||||||
@ -91,7 +59,7 @@ func singleMsgHandleByUser(allMsg []*pbMsg.MsgFormat, ownerId string) []*pbMsg.G
|
|||||||
var userid string
|
var userid string
|
||||||
var respMsgFormat []*pbMsg.GatherFormat
|
var respMsgFormat []*pbMsg.GatherFormat
|
||||||
m := make(map[string]MsgFormats)
|
m := make(map[string]MsgFormats)
|
||||||
//将消息以用户为维度聚集
|
//Gather messages in the dimension of users
|
||||||
for _, v := range allMsg {
|
for _, v := range allMsg {
|
||||||
if v.RecvID != ownerId {
|
if v.RecvID != ownerId {
|
||||||
userid = v.RecvID
|
userid = v.RecvID
|
||||||
@ -105,7 +73,7 @@ func singleMsgHandleByUser(allMsg []*pbMsg.MsgFormat, ownerId string) []*pbMsg.G
|
|||||||
m[userid] = append(value, v)
|
m[userid] = append(value, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//形成pb格式返回
|
//Return in pb format
|
||||||
for user, msg := range m {
|
for user, msg := range m {
|
||||||
tempUserMsg := new(pbMsg.GatherFormat)
|
tempUserMsg := new(pbMsg.GatherFormat)
|
||||||
tempUserMsg.ID = user
|
tempUserMsg.ID = user
|
||||||
@ -118,9 +86,9 @@ func singleMsgHandleByUser(allMsg []*pbMsg.MsgFormat, ownerId string) []*pbMsg.G
|
|||||||
func groupMsgHandleByUser(allMsg []*pbMsg.MsgFormat) []*pbMsg.GatherFormat {
|
func groupMsgHandleByUser(allMsg []*pbMsg.MsgFormat) []*pbMsg.GatherFormat {
|
||||||
var respMsgFormat []*pbMsg.GatherFormat
|
var respMsgFormat []*pbMsg.GatherFormat
|
||||||
m := make(map[string]MsgFormats)
|
m := make(map[string]MsgFormats)
|
||||||
//将消息以用户为维度聚集
|
//Gather messages in the dimension of users
|
||||||
for _, v := range allMsg {
|
for _, v := range allMsg {
|
||||||
//获得群ID
|
//Get group ID
|
||||||
groupID := strings.Split(v.RecvID, " ")[1]
|
groupID := strings.Split(v.RecvID, " ")[1]
|
||||||
if value, ok := m[groupID]; !ok {
|
if value, ok := m[groupID]; !ok {
|
||||||
var t MsgFormats
|
var t MsgFormats
|
||||||
@ -130,7 +98,7 @@ func groupMsgHandleByUser(allMsg []*pbMsg.MsgFormat) []*pbMsg.GatherFormat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//形成pb格式返回
|
//Return in pb format
|
||||||
for groupID, msg := range m {
|
for groupID, msg := range m {
|
||||||
tempUserMsg := new(pbMsg.GatherFormat)
|
tempUserMsg := new(pbMsg.GatherFormat)
|
||||||
tempUserMsg.ID = groupID
|
tempUserMsg.ID = groupID
|
||||||
@ -143,17 +111,17 @@ func groupMsgHandleByUser(allMsg []*pbMsg.MsgFormat) []*pbMsg.GatherFormat {
|
|||||||
|
|
||||||
type MsgFormats []*pbMsg.MsgFormat
|
type MsgFormats []*pbMsg.MsgFormat
|
||||||
|
|
||||||
// 实现sort.Interface接口取元素数量方法
|
// Implement the sort.Interface interface to get the number of elements method
|
||||||
func (s MsgFormats) Len() int {
|
func (s MsgFormats) Len() int {
|
||||||
return len(s)
|
return len(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实现sort.Interface接口比较元素方法
|
//Implement the sort.Interface interface comparison element method
|
||||||
func (s MsgFormats) Less(i, j int) bool {
|
func (s MsgFormats) Less(i, j int) bool {
|
||||||
return s[i].SendTime < s[j].SendTime
|
return s[i].SendTime < s[j].SendTime
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实现sort.Interface接口交换元素方法
|
//Implement the sort.Interface interface exchange element method
|
||||||
func (s MsgFormats) Swap(i, j int) {
|
func (s MsgFormats) Swap(i, j int) {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user