Browse Source

fix:mongo日志添加

Roc 11 months ago
parent
commit
26734543b9
2 changed files with 40 additions and 3 deletions
  1. 37 0
      utils/logs.go
  2. 3 3
      utils/mgodb/mgo_config.go

+ 37 - 0
utils/logs.go

@@ -16,6 +16,7 @@ const (
 var FileLog *logs.BeeLogger
 var ApiLog *logs.BeeLogger
 var Binlog *logs.BeeLogger
+var MongoLog *logs.BeeLogger
 
 func init() {
 	if LogMaxDays == 0 {
@@ -43,6 +44,7 @@ func init() {
 
 	initBinlog()
 	initApiLog()
+	initMgoLog()
 }
 
 type logConfig struct {
@@ -103,6 +105,41 @@ func initApiLog() {
 	ApiLog.EnableFuncCallDepth(true)
 }
 
+// initMgoLog
+// @Description: mongo日志
+// @author: Roc
+// @datetime 2024-05-06 16:42:47
+func initMgoLog() {
+	//mgo日志
+
+	var logPath string
+	{
+		binlogPath := BinLogPath
+		if binlogPath == "" {
+			binlogPath = DefaultBinlogPath
+		}
+		logPath = path.Dir(binlogPath)
+	}
+	if logPath == `` {
+		logPath = "./etalogs"
+	}
+
+	mongoLogPath := logPath + "/mongo"
+	mongoLogFile := "mongo.log"
+
+	os.MkdirAll(mongoLogPath, os.ModePerm)
+	logFileName := path.Join(mongoLogPath, mongoLogFile)
+	MongoLog = logs.NewLogger(1000000)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	//logConf.MaxLines = 10000000
+	//logConf.Rotate = true
+	b, _ := json.Marshal(logConf)
+	MongoLog.SetLogger(logs.AdapterFile, string(b))
+	MongoLog.EnableFuncCallDepth(true)
+}
+
 func getDefaultLogConfig() logConfig {
 	return logConfig{
 		FileName: "",

+ 3 - 3
utils/mgodb/mgo_config.go

@@ -2,12 +2,12 @@ package mgodb
 
 import (
 	"context"
+	"eta/eta_index_lib/utils"
 	"github.com/qiniu/qmgo"
 	"github.com/qiniu/qmgo/options"
 	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/event"
 	mgoptions "go.mongodb.org/mongo-driver/mongo/options"
-	"log"
 	"sync"
 )
 
@@ -58,7 +58,7 @@ func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
 			if ok {
 				commands = v.(bson.Raw)
 			}
-			log.Printf("\n【MongoDB】[%.3fms] [%v] %v \n", float64(evt.Duration)/1e6, commands, evt.Reply)
+			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] %v \n", float64(evt.Duration)/1e6, commands, evt.Reply)
 		},
 		Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
 			//log.Printf("Command: %v Failure: %v\n",
@@ -70,7 +70,7 @@ func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
 			if ok {
 				commands = v.(bson.Raw)
 			}
-			log.Printf("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
+			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
 		},
 	}