From 568f6abe80752831f5086666bb4e0f599ca10c6e Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Mon, 22 May 2023 10:08:04 +0800 Subject: [PATCH 1/2] remove logrus --- cmd/api/main.go | 1 - config/config.yaml | 4 - go.mod | 3 - internal/api/msg.go | 3 +- internal/msggateway/init.go | 6 +- .../online_msg_to_mongo_handler.go | 4 +- internal/push/offlinepush/getui/push.go | 2 +- internal/push/push_to_client.go | 13 +- internal/tools/cron_task.go | 4 +- pkg/common/config/config.go | 18 +- pkg/common/log/es_hk.go | 107 ----------- pkg/common/log/file_line_hk.go | 35 ---- pkg/common/log/lfs_hk.go | 39 ---- pkg/common/log/logrus.go | 176 ------------------ pkg/common/prome/grpc.go | 35 ---- pkg/startrpc/start.go | 3 - 16 files changed, 21 insertions(+), 432 deletions(-) delete mode 100644 pkg/common/log/es_hk.go delete mode 100644 pkg/common/log/file_line_hk.go delete mode 100644 pkg/common/log/lfs_hk.go delete mode 100644 pkg/common/log/logrus.go delete mode 100644 pkg/common/prome/grpc.go diff --git a/cmd/api/main.go b/cmd/api/main.go index f214bc23e..e559445ab 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -52,7 +52,6 @@ func run(port int) error { if err := client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, buf.Bytes()); err != nil { return err } - log.NewPrivateLog(constant.LogFileName) router := api.NewGinRouter(client, rdb) var address string if config.Config.Api.ListenIP != "" { diff --git a/config/config.yaml b/config/config.yaml index 95b8b4f65..53c8b756f 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -164,10 +164,6 @@ log: isStdout: false isJson: false withStack: false - elasticSearchSwitch: false - elasticSearchAddr: [ 127.0.0.1:9201 ] - elasticSearchUser: "" - elasticSearchPassword: "" modulename: #日志文件按模块命名,默认即可 longConnSvrName: msg_gateway diff --git a/go.mod b/go.mod index 0af1b50ad..32f7e63f0 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/minio/minio-go/v7 v7.0.22 github.com/mitchellh/mapstructure v1.4.2 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 - github.com/olivere/elastic/v7 v7.0.23 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.13.0 github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 @@ -82,13 +81,11 @@ require ( github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/klauspost/cpuid v1.3.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lithammer/shortuuid v3.0.0+incompatible // indirect - github.com/mailru/easyjson v0.7.7 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/minio/md5-simd v1.1.0 // indirect github.com/minio/sha256-simd v0.1.1 // indirect diff --git a/internal/api/msg.go b/internal/api/msg.go index 64946488b..477884c8b 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -9,7 +9,6 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg" @@ -98,7 +97,7 @@ func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementS tips.JsonDetail = utils.StructToJsonString(params.Content) pbData.MsgData.Content, err = proto.Marshal(&tips) if err != nil { - log.Error(mcontext.GetOperationID(c), "Marshal failed ", err.Error(), tips.String()) + log.ZError(c, "Marshal failed ", err, "tips", tips.String()) } } return &pbData diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index 9d0eee451..87f3c5464 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -2,14 +2,12 @@ package msggateway import ( "fmt" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "time" + + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { - log.NewPrivateLog(constant.LogFileName) fmt.Println("start rpc/msg_gateway server, port: ", rpcPort, wsPort, prometheusPort, ", OpenIM version: ", config.Version) longServer, err := NewWsServer( WithPort(wsPort), diff --git a/internal/msgtransfer/online_msg_to_mongo_handler.go b/internal/msgtransfer/online_msg_to_mongo_handler.go index 3b5fba2dd..43ec8f199 100644 --- a/internal/msgtransfer/online_msg_to_mongo_handler.go +++ b/internal/msgtransfer/online_msg_to_mongo_handler.go @@ -30,12 +30,12 @@ func NewOnlineHistoryMongoConsumerHandler(database controller.CommonMsgDatabase) return mc } -func (mc *OnlineHistoryMongoConsumerHandler) handleChatWs2Mongo(ctx context.Context, cMsg *sarama.ConsumerMessage, conversationID string, session sarama.ConsumerGroupSession) { +func (mc *OnlineHistoryMongoConsumerHandler) handleChatWs2Mongo(ctx context.Context, cMsg *sarama.ConsumerMessage, key string, session sarama.ConsumerGroupSession) { msg := cMsg.Value msgFromMQ := pbMsg.MsgDataToMongoByMQ{} err := proto.Unmarshal(msg, &msgFromMQ) if err != nil { - log.ZError(ctx, "unmarshall failed", err, "conversationID", conversationID, "len", len(msg)) + log.ZError(ctx, "unmarshall failed", err, "key", key, "len", len(msg)) return } if len(msgFromMQ.MsgData) == 0 { diff --git a/internal/push/offlinepush/getui/push.go b/internal/push/offlinepush/getui/push.go index 1fced52fe..1840d287c 100644 --- a/internal/push/offlinepush/getui/push.go +++ b/internal/push/offlinepush/getui/push.go @@ -75,7 +75,7 @@ func (g *Client) Push(ctx context.Context, userIDs []string, title, content stri go func(index int, userIDs []string) { defer wg.Done() if err2 := g.batchPush(ctx, token, userIDs, pushReq); err2 != nil { - log.NewError(mcontext.GetOperationID(ctx), "batchPush failed", i, token, pushReq) + log.ZError(ctx, "batchPush failed", err2, "index", index, "token", token, "req", pushReq) err = err2 } }(i, v.Item) diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index f91a23ea6..4634f206c 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/fcm" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/getui" @@ -14,7 +15,6 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/localcache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" @@ -127,8 +127,7 @@ func (p *Pusher) UnmarshalNotificationElem(bytes []byte, t interface{}) error { } func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws.MsgData) (err error) { - operationID := mcontext.GetOperationID(ctx) - log.Debug(operationID, "Get super group msg from msg_transfer And push msg", msg.String(), groupID) + log.ZDebug(ctx, "Get super group msg from msg_transfer and push msg", "msg", msg.String(), "groupID", groupID) var pushToUserIDs []string if err := callbackBeforeSuperGroupOnlinePush(ctx, groupID, msg, &pushToUserIDs); err != nil && err != errs.ErrCallbackContinue { return err @@ -167,7 +166,7 @@ func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws if err != nil { return err } - log.Debug(operationID, "push_result", wsResults, "sendData", msg) + log.ZDebug(ctx, "get conn and online push success", "result", wsResults, "msg", msg) p.successCount++ isOfflinePush := utils.GetSwitchFromOptions(msg.Options, constant.IsOfflinePush) if isOfflinePush { @@ -195,7 +194,7 @@ func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws if msg.ContentType != constant.SignalingNotification { notNotificationUserIDs, err := p.conversationLocalCache.GetRecvMsgNotNotifyUserIDs(ctx, groupID) if err != nil { - log.Error(operationID, utils.GetSelfFuncName(), "GetRecvMsgNotNotifyUserIDs failed", groupID) + log.ZError(ctx, "GetRecvMsgNotNotifyUserIDs failed", err, "groupID", groupID) return err } needOfflinePushUserIDs = utils.DifferenceString(notNotificationUserIDs, needOfflinePushUserIDs) @@ -212,12 +211,12 @@ func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws } err = p.offlinePushMsg(ctx, groupID, msg, offlinePushUserIDs) if err != nil { - log.NewError(operationID, "offlinePushMsg failed", groupID) + log.ZError(ctx, "offlinePushMsg failed", err, "groupID", groupID, "msg", msg) return err } _, err := p.GetConnsAndOnlinePush(ctx, msg, utils.IntersectString(needOfflinePushUserIDs, WebAndPcBackgroundUserIDs)) if err != nil { - log.NewError(operationID, "offlinePushMsg failed", groupID) + log.ZError(ctx, "offlinePushMsg failed", err, "groupID", groupID, "msg", msg, "userIDs", utils.IntersectString(needOfflinePushUserIDs, WebAndPcBackgroundUserIDs)) return err } } diff --git a/internal/tools/cron_task.go b/internal/tools/cron_task.go index 4cffe9990..fa19bffe0 100644 --- a/internal/tools/cron_task.go +++ b/internal/tools/cron_task.go @@ -1,6 +1,7 @@ package tools import ( + "context" "fmt" "sync" @@ -12,8 +13,7 @@ import ( const moduleName = "cron" func StartCronTask() error { - log.NewPrivateLog(moduleName) - log.NewInfo("StartCronTask", "start cron task", "cron config", config.Config.Mongo.ChatRecordsClearTime) + log.ZInfo(context.Background(), "start cron task", "cron config", config.Config.Mongo.ChatRecordsClearTime) fmt.Println("cron task start, config", config.Config.Mongo.ChatRecordsClearTime) msgTool, err := InitMsgTool() if err != nil { diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index fbada0991..848ff1472 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -183,17 +183,13 @@ type config struct { Password string `yaml:"password"` } `yaml:"zookeeper"` Log struct { - StorageLocation string `yaml:"storageLocation"` - RotationTime int `yaml:"rotationTime"` - RemainRotationCount uint `yaml:"remainRotationCount"` - RemainLogLevel int `yaml:"remainLogLevel"` - IsStdout bool `yaml:"isStdout"` - WithStack bool `yaml:"withStack"` - ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"` - ElasticSearchAddr []string `yaml:"elasticSearchAddr"` - ElasticSearchUser string `yaml:"elasticSearchUser"` - ElasticSearchPassword string `yaml:"elasticSearchPassword"` - IsJson bool `yaml:"isJson"` + StorageLocation string `yaml:"storageLocation"` + RotationTime int `yaml:"rotationTime"` + RemainRotationCount uint `yaml:"remainRotationCount"` + RemainLogLevel int `yaml:"remainLogLevel"` + IsStdout bool `yaml:"isStdout"` + WithStack bool `yaml:"withStack"` + IsJson bool `yaml:"isJson"` } ModuleName struct { LongConnSvrName string `yaml:"longConnSvrName"` diff --git a/pkg/common/log/es_hk.go b/pkg/common/log/es_hk.go deleted file mode 100644 index 4132ce6de..000000000 --- a/pkg/common/log/es_hk.go +++ /dev/null @@ -1,107 +0,0 @@ -/* -** description("Send logs to elasticsearch hook"). -** copyright('tuoyun,www.tuoyun.net'). -** author("fg,Gordon@tuoyun.net"). -** time(2021/3/26 17:05). - */ -package log - -import ( - "context" - "fmt" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - elasticV7 "github.com/olivere/elastic/v7" - "github.com/sirupsen/logrus" - "log" - "os" - "strings" - "time" -) - -// esHook CUSTOMIZED ES hook -type esHook struct { - moduleName string - client *elasticV7.Client -} - -// newEsHook Initialization -func newEsHook(moduleName string) *esHook { - //https://github.com/sohlich/elogrus - //client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200")) - //if err != nil { - // log.Panic(err) - //} - //hook, err := elogrus.NewAsyncElasticHook(client, "localhost", logrus.DebugLevel, "mylog") - //if err != nil { - // log.Panic(err) - //} - es, err := elasticV7.NewClient( - elasticV7.SetURL(config.Config.Log.ElasticSearchAddr...), - elasticV7.SetBasicAuth(config.Config.Log.ElasticSearchUser, config.Config.Log.ElasticSearchPassword), - elasticV7.SetSniff(false), - elasticV7.SetHealthcheckInterval(60*time.Second), - elasticV7.SetErrorLog(log.New(os.Stderr, "ES:", log.LstdFlags)), - ) - - if err != nil { - log.Fatal("failed to create Elastic V7 Client: ", err) - } - - //info, code, err := es.Ping(logConfig.ElasticSearch.EsAddr[0]).Do(context.Background()) - //if err != nil { - // panic(err) - //} - //fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number) - // - //esversion, err := es.ElasticsearchVersion(logConfig.ElasticSearch.EsAddr[0]) - //if err != nil { - // panic(err) - //} - //fmt.Printf("Elasticsearch version %s\n", esversion) - return &esHook{client: es, moduleName: moduleName} -} - -// Fire log hook interface -func (hook *esHook) Fire(entry *logrus.Entry) error { - doc := newEsLog(entry) - go hook.sendEs(doc) - return nil -} - -func (hook *esHook) Levels() []logrus.Level { - return logrus.AllLevels -} - -// sendEs -func (hook *esHook) sendEs(doc appLogDocModel) { - defer func() { - if r := recover(); r != nil { - fmt.Println("send entry to es failed: ", r) - } - }() - _, err := hook.client.Index().Index(hook.moduleName).Type(doc.indexName()).BodyJson(doc).Do(context.Background()) - if err != nil { - log.Println(err) - } - -} - -// appLogDocModel es model -type appLogDocModel map[string]interface{} - -func newEsLog(e *logrus.Entry) appLogDocModel { - ins := make(map[string]interface{}) - ins["level"] = strings.ToUpper(e.Level.String()) - ins["time"] = e.Time.Format("2006-01-02 15:04:05") - for kk, vv := range e.Data { - ins[kk] = vv - } - ins["tipInfo"] = e.Message - - return ins -} - -// indexName es index name -func (m *appLogDocModel) indexName() string { - return time.Now().Format("2006-01-02") -} diff --git a/pkg/common/log/file_line_hk.go b/pkg/common/log/file_line_hk.go deleted file mode 100644 index b3a6d84a6..000000000 --- a/pkg/common/log/file_line_hk.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -** description("get the name and line number of the calling file hook"). -** copyright('tuoyun,www.tuoyun.net'). -** author("fg,Gordon@tuoyun.net"). -** time(2021/3/16 11:26). - */ -package log - -import ( - "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" - "github.com/sirupsen/logrus" - "runtime" - "strings" -) - -type fileHook struct{} - -func newFileHook() *fileHook { - return &fileHook{} -} - -func (f *fileHook) Levels() []logrus.Level { - return logrus.AllLevels -} - -func (f *fileHook) Fire(entry *logrus.Entry) error { - var s string - _, file, line, _ := runtime.Caller(8) - i := strings.SplitAfter(file, "/") - if len(i) > 3 { - s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line) - } - entry.Data["FilePath"] = s - return nil -} diff --git a/pkg/common/log/lfs_hk.go b/pkg/common/log/lfs_hk.go deleted file mode 100644 index 3fd4ece26..000000000 --- a/pkg/common/log/lfs_hk.go +++ /dev/null @@ -1,39 +0,0 @@ -package log - -import ( - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - nested "github.com/antonfisher/nested-logrus-formatter" - rotatelogs "github.com/lestrrat-go/file-rotatelogs" - "github.com/rifflock/lfshook" - "github.com/sirupsen/logrus" - "time" -) - -func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook { - lfsHook := lfshook.NewHook(lfshook.WriterMap{ - logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName), - logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName), - logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName), - logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName), - }, &nested.Formatter{ - TimestampFormat: "2006-01-02 15:04:05.000", - HideKeys: false, - FieldsOrder: []string{"PID", "FilePath", "OperationID", "Msg"}, - }) - return lfsHook -} -func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs { - if moduleName != "" { - moduleName = moduleName + "." - } - writer, err := rotatelogs.New( - config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d", - rotatelogs.WithRotationTime(rotationTime), - rotatelogs.WithRotationCount(maxRemainNum), - ) - if err != nil { - panic(err.Error()) - } else { - return writer - } -} diff --git a/pkg/common/log/logrus.go b/pkg/common/log/logrus.go deleted file mode 100644 index 0d03134c5..000000000 --- a/pkg/common/log/logrus.go +++ /dev/null @@ -1,176 +0,0 @@ -package log - -import ( - "bufio" - "context" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" - - //"bufio" - "fmt" - "os" - "time" - - nested "github.com/antonfisher/nested-logrus-formatter" - "github.com/sirupsen/logrus" -) - -var logger *LogrusLogger -var ctxLogger *LogrusLogger - -type LogrusLogger struct { - *logrus.Logger - Pid int - Type string -} - -func init() { - logger = loggerInit("") - ctxLogger = ctxLoggerInit("") -} - -func NewPrivateLog(moduleName string) { - logger = loggerInit(moduleName) -} - -func ctxLoggerInit(moduleName string) *LogrusLogger { - var ctxLogger = logrus.New() - ctxLogger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel)) - src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend) - if err != nil { - panic(err.Error()) - } - writer := bufio.NewWriter(src) - ctxLogger.SetOutput(writer) - ctxLogger.SetFormatter(&nested.Formatter{ - TimestampFormat: "2006-01-02 15:04:05.000", - HideKeys: false, - FieldsOrder: []string{"PID", "FilePath", "OperationID"}, - }) - if config.Config.Log.ElasticSearchSwitch { - ctxLogger.AddHook(newEsHook(moduleName)) - } - //Log file segmentation hook - hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName) - ctxLogger.AddHook(hook) - return &LogrusLogger{ - ctxLogger, - os.Getpid(), - "ctxLogger", - } -} - -func loggerInit(moduleName string) *LogrusLogger { - var logger = logrus.New() - //All logs will be printed - logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel)) - //Close std console output - //os.O_WRONLY | os.O_CREATE | os.O_APPEND - src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend) - if err != nil { - panic(err.Error()) - } - writer := bufio.NewWriter(src) - logger.SetOutput(writer) - // logger.SetOutput(os.Stdout) - //Log Console Print Style Setting - - logger.SetFormatter(&nested.Formatter{ - TimestampFormat: "2006-01-02 15:04:05.000", - HideKeys: false, - FieldsOrder: []string{"PID", "FilePath", "OperationID"}, - }) - logger.SetFormatter(&logrus.JSONFormatter{}) - //File name and line number display hook - logger.AddHook(newFileHook()) - - //Send logs to elasticsearch hook - if config.Config.Log.ElasticSearchSwitch { - logger.AddHook(newEsHook(moduleName)) - } - //Log file segmentation hook - hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName) - logger.AddHook(hook) - return &LogrusLogger{ - logger, - os.Getpid(), - "", - } -} - -func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) { - operationID := mcontext.GetOperationID(ctx) - logger.WithFields(logrus.Fields{ - "OperationID": operationID, - "PID": logger.Pid, - "Msg": msg, - }).Infoln(keysAndValues) -} - -func Info(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Infoln(args) -} - -func Error(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Errorln(args) -} - -func Debug(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Debugln(args) -} - -// Deprecated -func Warning(token, OperationID, format string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "PID": logger.Pid, - "OperationID": OperationID, - }).Warningf(format, args...) - -} - -// internal method -func argsHandle(OperationID string, fields logrus.Fields, args []interface{}) { - for i := 0; i < len(args); i += 2 { - if i+1 < len(args) { - fields[fmt.Sprintf("%v", args[i])] = args[i+1] - } else { - fields[fmt.Sprintf("%v", args[i])] = "" - } - } - fields["OperationID"] = OperationID - fields["PID"] = logger.Pid -} -func NewInfo(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Infoln(args) -} -func NewError(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Errorln(args) -} -func NewDebug(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Debugln(args) -} -func NewWarn(OperationID string, args ...interface{}) { - logger.WithFields(logrus.Fields{ - "OperationID": OperationID, - "PID": logger.Pid, - }).Warnln(args) -} diff --git a/pkg/common/prome/grpc.go b/pkg/common/prome/grpc.go deleted file mode 100644 index 6cc411c56..000000000 --- a/pkg/common/prome/grpc.go +++ /dev/null @@ -1,35 +0,0 @@ -package prome - -import ( - "context" - "encoding/json" - "time" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" - - "google.golang.org/grpc" - "google.golang.org/grpc/peer" -) - -func UnaryServerInterceptorPrometheus(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { - remote, _ := peer.FromContext(ctx) - remoteAddr := remote.Addr.String() - - in, _ := json.Marshal(req) - inStr := string(in) - log.NewInfo("ip", remoteAddr, "access_start", info.FullMethod, "in", inStr) - - start := time.Now() - defer func() { - out, _ := json.Marshal(resp) - outStr := string(out) - duration := int64(time.Since(start) / time.Millisecond) - if duration >= 500 { - log.NewInfo("ip", remoteAddr, "access_end", info.FullMethod, "in", inStr, "out", outStr, "err", err, "duration/ms", duration) - } else { - log.NewInfo("ip", remoteAddr, "access_end", info.FullMethod, "in", inStr, "out", outStr, "err", err, "duration/ms", duration) - } - }() - resp, err = handler(ctx, req) - return -} diff --git a/pkg/startrpc/start.go b/pkg/startrpc/start.go index 8c4f48ab1..ba46da86e 100644 --- a/pkg/startrpc/start.go +++ b/pkg/startrpc/start.go @@ -7,8 +7,6 @@ import ( "time" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/network" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" @@ -22,7 +20,6 @@ import ( func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error { fmt.Println("start", rpcRegisterName, "server, port: ", rpcPort, "prometheusPort:", prometheusPort, ", OpenIM version: ", config.Version) - log.NewPrivateLog(constant.LogFileName) listener, err := net.Listen("tcp", net.JoinHostPort(config.Config.ListenIP, strconv.Itoa(rpcPort))) if err != nil { return err From fc49325bb82fbd28a3c9a6fd82c0327fc2a3d956 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Mon, 22 May 2023 10:08:30 +0800 Subject: [PATCH 2/2] remove mcontext --- internal/push/push_to_client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index 291b1eca7..ba92144a2 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -15,6 +15,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/localcache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs"