From 905311d96ac085653cd9bb7618fcb323e3e7674c Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Thu, 12 Sep 2024 13:49:31 +0800 Subject: [PATCH 1/2] improve log, use QID --- db/connector.go | 19 ++++++++++++++++++- infrastructure/config/config.go | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/db/connector.go b/db/connector.go index 898c0da..24dd8aa 100644 --- a/db/connector.go +++ b/db/connector.go @@ -3,6 +3,7 @@ package db import ( "context" "database/sql" + "encoding/json" "fmt" "os" "strings" @@ -84,6 +85,20 @@ func (c *Connector) Exec(ctx context.Context, sql string, qid uint64) (int64, er return res.RowsAffected() } +func logData(data *Data, logger *logrus.Entry) { + if data == nil { + logger.Tracef("No data to display") + return + } + + jsonData, err := json.Marshal(data) + if err != nil { + logger.Errorf("Failed to marshal data to JSON: %v", err) + return + } + logger.Tracef("%s", jsonData) +} + func (c *Connector) Query(ctx context.Context, sql string, qid uint64) (*Data, error) { dbLogger = dbLogger.WithFields(logrus.Fields{config.ReqIDKey: qid}) ctx = context.WithValue(ctx, common.ReqIDKey, int64(qid)) @@ -132,7 +147,9 @@ func (c *Connector) Query(ctx context.Context, sql string, qid uint64) (*Data, e data.Data = append(data.Data, tmp) } - dbLogger.Tracef("get data:%v", data) + if dbLogger.Logger.IsLevelEnabled(logrus.TraceLevel) { + logData(data, dbLogger) + } return data, nil } diff --git a/infrastructure/config/config.go b/infrastructure/config/config.go index fb4b054..f55984b 100644 --- a/infrastructure/config/config.go +++ b/infrastructure/config/config.go @@ -15,7 +15,7 @@ import ( var Name = fmt.Sprintf("%skeeper", version.CUS_PROMPT) -const ReqIDKey = "qid" +const ReqIDKey = "QID" const ModelKey = "model" type Config struct { From 14a29973f7381db15b19862a693e8be16f9dd073 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Thu, 12 Sep 2024 13:55:59 +0800 Subject: [PATCH 2/2] improve log --- api/gen_metric.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/api/gen_metric.go b/api/gen_metric.go index 5fafae5..5814a1c 100644 --- a/api/gen_metric.go +++ b/api/gen_metric.go @@ -602,10 +602,6 @@ func (gm *GeneralMetric) initColumnSeqMap() error { return err } - if logger.Logger.IsLevelEnabled(logrus.TraceLevel) { - gmLogger.Tracef("data:%v", data) - } - if len(data.Data) < 1 || len(data.Data[0]) < 4 { return fmt.Errorf("desc %s.%s error", gm.database, tableName) }