浏览代码

打开mongo日志

xyxie 10 月之前
父节点
当前提交
dc1d6e6de8
共有 1 个文件被更改,包括 8 次插入4 次删除
  1. 8 4
      utils/mgodb/mgo_config.go

+ 8 - 4
utils/mgodb/mgo_config.go

@@ -2,9 +2,13 @@ package mgodb
 
 import (
 	"context"
+	"eta/eta_forum_task/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"
+	"sync"
 )
 
 var (
@@ -37,7 +41,7 @@ type MgoConfig struct {
 func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
 	// 创建cmdMonitor,用于打印SQL
 	//startedCommands := make(map[int64]bson.Raw)
-	/*startedCommands := sync.Map{} // map[int64]bson.Raw
+	startedCommands := sync.Map{} // map[int64]bson.Raw
 
 	cmdMonitor := &event.CommandMonitor{
 		Started: func(_ context.Context, evt *event.CommandStartedEvent) {
@@ -54,7 +58,7 @@ func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
 			if ok {
 				commands = v.(bson.Raw)
 			}
-			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] %v \n", float64(evt.Duration)/1e6, commands, evt.Reply)
+			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v]\n", float64(evt.Duration)/1e6, commands)
 		},
 		Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
 			//log.Printf("Command: %v Failure: %v\n",
@@ -68,11 +72,11 @@ func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
 			}
 			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
 		},
-	}*/
+	}
 
 	// 创建options
 	ops := options.ClientOptions{ClientOptions: &mgoptions.ClientOptions{}}
-	//ops.SetMonitor(cmdMonitor)
+	ops.SetMonitor(cmdMonitor)
 
 	ctx := context.Background()