Bladeren bron

修改日志输出格式

qdy 2 maanden geleden
bovenliggende
commit
08630dee68

+ 20
- 19
factory/database/db_factory.go Bestand weergeven

@@ -2,9 +2,11 @@ package database
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"log"
5 6
 	"sync"
6 7
 
7 8
 	"git.x2erp.com/qdy/go-base/config"
9
+	"git.x2erp.com/qdy/go-base/ctx"
8 10
 	"git.x2erp.com/qdy/go-base/types"
9 11
 	"git.x2erp.com/qdy/go-db/drivers"
10 12
 	"git.x2erp.com/qdy/go-db/functions"
@@ -28,10 +30,10 @@ func GetDBFactory() (*DBFactory, error) {
28 30
 
29 31
 	once.Do(func() {
30 32
 		// 使用配置单例
31
-		cfg := config.GetConfig()
33
+		cfg, err := config.GetConfig()
32 34
 
33 35
 		// 检查配置初始化是否有错误
34
-		if err := config.GetInitError(); err != nil {
36
+		if err != nil {
35 37
 			initErr = fmt.Errorf("failed to load config: %v", err)
36 38
 			return
37 39
 		}
@@ -44,7 +46,7 @@ func GetDBFactory() (*DBFactory, error) {
44 46
 
45 47
 		// 显示所支持的数据库驱动
46 48
 		driversStr := drivers.GetAllDrivers()
47
-		fmt.Printf("Available database drivers: %v\n", driversStr)
49
+		log.Printf("Available database drivers: %v\n", driversStr)
48 50
 
49 51
 		dbConfig := cfg.GetDatabase()
50 52
 		dbType := dbConfig.Type
@@ -91,7 +93,7 @@ func GetDBFactory() (*DBFactory, error) {
91 93
 		return nil, initErr
92 94
 	}
93 95
 
94
-	fmt.Print(msg)
96
+	log.Print(msg)
95 97
 
96 98
 	return instanceDBFactory, nil
97 99
 }
@@ -106,7 +108,7 @@ func (f *DBFactory) Close() error {
106 108
 	if f.db != nil {
107 109
 		err := f.db.Close()
108 110
 		f.db = nil
109
-		fmt.Println("Database connection closed gracefully")
111
+		log.Println("Database connection closed gracefully")
110 112
 		return err
111 113
 	}
112 114
 	return nil
@@ -114,45 +116,44 @@ func (f *DBFactory) Close() error {
114 116
 
115 117
 // GetDBType 得到当前使用数据库类型
116 118
 func (f *DBFactory) GetDBType() string {
117
-	cfg := config.GetConfig()
118
-	dbConfig := cfg.GetDatabase()
119
+	dbConfig := config.GetDatabase()
119 120
 	return dbConfig.Type
120 121
 }
121 122
 
122 123
 // QueryToJSON 快捷查询,直接返回 JSON 字节流
123
-func (f *DBFactory) QueryToJSON(sql string) *types.QueryResult {
124
-	return functions.QueryToJSON(f.db, sql)
124
+func (f *DBFactory) QueryToJSON(sql string, reqCtx *ctx.RequestContext) *types.QueryResult {
125
+	return functions.QueryToJSON(f.db, sql, reqCtx)
125 126
 }
126 127
 
127 128
 // QueryParamsToJSON 位置参数查询并返回 JSON 字节数据
128
-func (f *DBFactory) QueryPositionalToJSON(sql string, params []interface{}) *types.QueryResult {
129
+func (f *DBFactory) QueryPositionalToJSON(sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
129 130
 
130
-	return functions.QueryPositionalToJSON(f.db, sql, params)
131
+	return functions.QueryPositionalToJSON(f.db, sql, params, reqCtx)
131 132
 }
132 133
 
133 134
 // QueryParamsNameToJSON 命名参数查询并返回 JSON 字节数据
134 135
 // params 可以是 map[string]interface{} 或结构体
135
-func (f *DBFactory) QueryParamsNameToJSON(sql string, params map[string]interface{}) *types.QueryResult {
136
+func (f *DBFactory) QueryParamsNameToJSON(sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
136 137
 
137
-	return functions.QueryParamsNameToJSON(f.db, sql, params)
138
+	return functions.QueryParamsNameToJSON(f.db, sql, params, reqCtx)
138 139
 }
139 140
 
140 141
 // QueryToCSV 快捷查询,直接返回 CSV 字符串(包含表头)
141
-func (f *DBFactory) QueryToCSV(sql string, writerHeader bool) ([]byte, error) {
142
-	return functions.QueryToCSV(f.db, sql, writerHeader)
142
+func (f *DBFactory) QueryToCSV(sql string, writerHeader bool, reqCtx *ctx.RequestContext) ([]byte, error) {
143
+	return functions.QueryToCSV(f.db, sql, writerHeader, reqCtx)
143 144
 }
144 145
 
145 146
 // QueryParamsToCSV 位置参数查询并返回 CSV 字节数据
146
-func (f *DBFactory) QueryPositionalToCSV(sql string, writerHeader bool, params []interface{}) ([]byte, error) {
147
+func (f *DBFactory) QueryPositionalToCSV(sql string, writerHeader bool, params []interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
147 148
 
148
-	return functions.QueryPositionalToCSV(f.db, sql, writerHeader, params)
149
+	return functions.QueryPositionalToCSV(f.db, sql, writerHeader, params, reqCtx)
149 150
 }
150 151
 
151 152
 // QueryParamsNameToCSV 命名参数查询并返回 CSV 字节数据
152 153
 // params 可以是 map[string]interface{} 或结构体
153
-func (f *DBFactory) QueryParamsNameToCSV(sql string, writerHeader bool, params map[string]interface{}) ([]byte, error) {
154
+func (f *DBFactory) QueryParamsNameToCSV(sql string, writerHeader bool, params map[string]interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
154 155
 
155
-	return functions.QueryParamsNameToCSV(f.db, sql, writerHeader, params)
156
+	return functions.QueryParamsNameToCSV(f.db, sql, writerHeader, params, reqCtx)
156 157
 }
157 158
 
158 159
 // ExecuteDDL 快捷执行DDL语句

+ 2
- 2
factory/doris/doris_factory.go Bestand weergeven

@@ -33,9 +33,9 @@ var (
33 33
 func GetDorisFactory(httpFactory *http.HTTPFactory) (*DorisFactory, error) {
34 34
 	var initErr error
35 35
 	instanceOnce.Do(func() {
36
-		cfg := config.GetConfig()
36
+		cfg, err := config.GetConfig()
37 37
 
38
-		if err := config.GetInitError(); err != nil {
38
+		if err != nil {
39 39
 			initErr = fmt.Errorf("failed to load config: %v", err)
40 40
 			return
41 41
 		}

+ 2
- 2
factory/http/http_factory.go Bestand weergeven

@@ -31,9 +31,9 @@ var (
31 31
 // GetHTTPFactory 获取HTTP工厂单例实例
32 32
 func GetHTTPFactory() (*HTTPFactory, error) {
33 33
 	httpFactoryOnce.Do(func() {
34
-		cfg := config.GetConfig()
34
+		cfg, err := config.GetConfig()
35 35
 
36
-		if err := config.GetInitError(); err != nil {
36
+		if err != nil {
37 37
 			httpFactoryInitErr = fmt.Errorf("failed to load config: %v", err)
38 38
 			return
39 39
 		}

+ 2
- 2
factory/rabbitmq/rabbitmq_factory.go Bestand weergeven

@@ -20,9 +20,9 @@ type RabbitMQFactory struct {
20 20
 
21 21
 // NewRabbitMQFactory 创建RabbitMQ工厂
22 22
 func NewRabbitMQFactory() (*RabbitMQFactory, error) {
23
-	cfg := config.GetConfig()
23
+	cfg, err := config.GetConfig()
24 24
 
25
-	if err := config.GetInitError(); err != nil {
25
+	if err != nil {
26 26
 		return nil, fmt.Errorf("failed to load config: %v", err)
27 27
 	}
28 28
 

+ 2
- 2
factory/redis/redis_factory.go Bestand weergeven

@@ -18,9 +18,9 @@ type RedisFactory struct {
18 18
 
19 19
 // NewRedisFactory 创建Redis客户端工厂
20 20
 func NewRedisFactory() (*RedisFactory, error) {
21
-	cfg := config.GetConfig()
21
+	cfg, err := config.GetConfig()
22 22
 
23
-	if err := config.GetInitError(); err != nil {
23
+	if err != nil {
24 24
 		return nil, fmt.Errorf("failed to load config: %v", err)
25 25
 	}
26 26
 

+ 4
- 3
functions/query_csv.go Bestand weergeven

@@ -6,11 +6,12 @@ import (
6 6
 	"fmt"
7 7
 	"strings"
8 8
 
9
+	"git.x2erp.com/qdy/go-base/ctx"
9 10
 	"github.com/jmoiron/sqlx"
10 11
 )
11 12
 
12 13
 // QueryToCSV 无参数查询并返回 CSV 字节数据
13
-func QueryToCSV(db *sqlx.DB, sql string, writerHeader bool) ([]byte, error) {
14
+func QueryToCSV(db *sqlx.DB, sql string, writerHeader bool, reqCtx *ctx.RequestContext) ([]byte, error) {
14 15
 	if sql == "" {
15 16
 		return nil, fmt.Errorf("SQL query cannot be empty")
16 17
 	}
@@ -24,7 +25,7 @@ func QueryToCSV(db *sqlx.DB, sql string, writerHeader bool) ([]byte, error) {
24 25
 }
25 26
 
26 27
 // QueryParamsToCSV 位置参数查询并返回 CSV 字节数据
27
-func QueryPositionalToCSV(db *sqlx.DB, sql string, writerHeader bool, params []interface{}) ([]byte, error) {
28
+func QueryPositionalToCSV(db *sqlx.DB, sql string, writerHeader bool, params []interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
28 29
 	if sql == "" {
29 30
 		return nil, fmt.Errorf("SQL query cannot be empty")
30 31
 	}
@@ -39,7 +40,7 @@ func QueryPositionalToCSV(db *sqlx.DB, sql string, writerHeader bool, params []i
39 40
 
40 41
 // QueryParamsNameToCSV 命名参数查询并返回 CSV 字节数据
41 42
 // params 可以是 map[string]interface{} 或结构体
42
-func QueryParamsNameToCSV(db *sqlx.DB, sql string, writerHeader bool, params map[string]interface{}) ([]byte, error) {
43
+func QueryParamsNameToCSV(db *sqlx.DB, sql string, writerHeader bool, params map[string]interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
43 44
 	if sql == "" {
44 45
 		return nil, fmt.Errorf("SQL query cannot be empty")
45 46
 	}

+ 16
- 14
functions/query_json.go Bestand weergeven

@@ -6,29 +6,30 @@ import (
6 6
 	"fmt"
7 7
 	"time"
8 8
 
9
+	"git.x2erp.com/qdy/go-base/ctx"
9 10
 	"git.x2erp.com/qdy/go-base/types"
10 11
 	"github.com/jmoiron/sqlx"
11 12
 )
12 13
 
13 14
 // QueryParamsNameToJSON 执行带命名参数的查询
14
-func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{}) *types.QueryResult {
15
+func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
15 16
 	startTime := time.Now()
16 17
 
17 18
 	if sql == "" {
18
-		return createErrorResult("SQL query cannot be empty", startTime)
19
+		return createErrorResult("SQL query cannot be empty", startTime, reqCtx)
19 20
 	}
20 21
 
21 22
 	// 处理命名参数
22 23
 	query, args, err := sqlx.Named(sql, params)
23 24
 	if err != nil {
24
-		return createErrorResult(fmt.Sprintf("Failed to process named parameters: %v", err), startTime)
25
+		return createErrorResult(fmt.Sprintf("Failed to process named parameters: %v", err), startTime, reqCtx)
25 26
 	}
26 27
 
27 28
 	// 执行查询
28 29
 	rows, err := db.Query(sqlx.Rebind(sqlx.DOLLAR, query), args...)
29 30
 	//rows, err := sqlx.NamedQuery(db, sql, params)
30 31
 	if err != nil {
31
-		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime)
32
+		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime, reqCtx)
32 33
 	}
33 34
 	defer rows.Close()
34 35
 
@@ -37,19 +38,19 @@ func QueryParamsNameToJSON(db *sqlx.DB, sql string, params map[string]interface{
37 38
 }
38 39
 
39 40
 // QueryPositionalToJSON 执行带位置参数的查询
40
-func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}) *types.QueryResult {
41
+func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}, reqCtx *ctx.RequestContext) *types.QueryResult {
41 42
 	startTime := time.Now()
42 43
 
43 44
 	//fmt.Printf("positionalParams: %s", params)
44 45
 	//fmt.Printf("sql: %s", sql)
45 46
 	if sql == "" {
46
-		return createErrorResult("SQL query cannot be empty", startTime)
47
+		return createErrorResult("SQL query cannot be empty", startTime, reqCtx)
47 48
 	}
48 49
 
49 50
 	// 执行查询
50 51
 	rows, err := db.Query(sql, params...)
51 52
 	if err != nil {
52
-		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime)
53
+		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime, reqCtx)
53 54
 	}
54 55
 	defer rows.Close()
55 56
 
@@ -58,17 +59,17 @@ func QueryPositionalToJSON(db *sqlx.DB, sql string, params []interface{}) *types
58 59
 }
59 60
 
60 61
 // QueryToJSON 执行无参数的查询
61
-func QueryToJSON(db *sqlx.DB, sql string) *types.QueryResult {
62
+func QueryToJSON(db *sqlx.DB, sql string, reqCtx *ctx.RequestContext) *types.QueryResult {
62 63
 	startTime := time.Now()
63 64
 
64 65
 	if sql == "" {
65
-		return createErrorResult("SQL query cannot be empty", startTime)
66
+		return createErrorResult("SQL query cannot be empty", startTime, reqCtx)
66 67
 	}
67 68
 
68 69
 	// 执行查询
69 70
 	rows, err := db.Query(sql)
70 71
 	if err != nil {
71
-		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime)
72
+		return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime, reqCtx)
72 73
 	}
73 74
 	defer rows.Close()
74 75
 
@@ -146,10 +147,11 @@ func processQueryResult(rows *sql.Rows, startTime time.Time) *types.QueryResult
146 147
 }
147 148
 
148 149
 // createErrorResult 创建错误结果的辅助函数
149
-func createErrorResult(errorMsg string, startTime time.Time) *types.QueryResult {
150
+func createErrorResult(errorMsg string, startTime time.Time, reqCtx *ctx.RequestContext) *types.QueryResult {
150 151
 	return &types.QueryResult{
151
-		Success: false,
152
-		Error:   errorMsg,
153
-		Time:    time.Since(startTime).String(),
152
+		Success:  false,
153
+		Error:    errorMsg,
154
+		Time:     time.Since(startTime).String(),
155
+		Metadata: reqCtx,
154 156
 	}
155 157
 }

+ 2
- 1
functions/testConnection.go Bestand weergeven

@@ -2,6 +2,7 @@ package functions
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"log"
5 6
 
6 7
 	"github.com/jmoiron/sqlx"
7 8
 )
@@ -28,6 +29,6 @@ func TestConnection(db *sqlx.DB, dbType string) error {
28 29
 		return fmt.Errorf("unexpected test result: %d", result)
29 30
 	}
30 31
 
31
-	fmt.Println("test Connection  database is success.")
32
+	log.Println("test Connection  database is success.")
32 33
 	return nil
33 34
 }

+ 4
- 2
myhandle/query_handler_bytes.go Bestand weergeven

@@ -3,6 +3,8 @@ package myhandle
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"net/http"
6
+
7
+	"git.x2erp.com/qdy/go-base/ctx"
6 8
 )
7 9
 
8 10
 // 最简单的 queryHandler,只处理 []byte 返回
@@ -10,7 +12,7 @@ func QueryHandlerBytes[T any, F any](
10 12
 	w http.ResponseWriter,
11 13
 	r *http.Request,
12 14
 	factory F,
13
-	handlerFunc func(F, T) []byte,
15
+	handlerFunc func(F, T, *ctx.RequestContext) []byte,
14 16
 ) {
15 17
 	// 解析请求参数
16 18
 	var req T
@@ -24,7 +26,7 @@ func QueryHandlerBytes[T any, F any](
24 26
 	}
25 27
 
26 28
 	// 调用业务逻辑函数
27
-	csvData := handlerFunc(factory, req)
29
+	csvData := handlerFunc(factory, req, ctx.GetContext(r))
28 30
 
29 31
 	// 直接返回 CSV 数据(包含错误信息时也会被正确处理)
30 32
 	w.Header().Set("Content-Type", "text/csv")

+ 3
- 2
myhandle/query_handler_json.go Bestand weergeven

@@ -5,6 +5,7 @@ import (
5 5
 	"net/http"
6 6
 	"time"
7 7
 
8
+	"git.x2erp.com/qdy/go-base/ctx"
8 9
 	"git.x2erp.com/qdy/go-base/types"
9 10
 )
10 11
 
@@ -13,7 +14,7 @@ func QueryHandlerJson[T any, F any](
13 14
 	w http.ResponseWriter,
14 15
 	r *http.Request,
15 16
 	factory F,
16
-	handlerFunc func(F, T) *types.QueryResult,
17
+	handlerFunc func(F, T, *ctx.RequestContext) *types.QueryResult,
17 18
 ) {
18 19
 	// 解析请求参数
19 20
 	var req T
@@ -29,7 +30,7 @@ func QueryHandlerJson[T any, F any](
29 30
 	}
30 31
 
31 32
 	// 执行业务逻辑
32
-	result := handlerFunc(factory, req)
33
+	result := handlerFunc(factory, req, ctx.GetContext(r))
33 34
 
34 35
 	// 设置头,写入结果
35 36
 	w.Header().Set("Content-Type", "application/json")

+ 7
- 1
test.go Bestand weergeven

@@ -11,7 +11,13 @@ import (
11 11
 func main() {
12 12
 
13 13
 	// 显示当前使用的数据库配置
14
-	config := config.GetConfig()
14
+	config, err := config.GetConfig()
15
+	if err != nil {
16
+		log.Fatalf("failed to load config: %v", err)
17
+
18
+		return
19
+	}
20
+
15 21
 	dbConfig := config.GetDatabase() // 通过接口方法获取数据库配置
16 22
 	fmt.Printf("Using database type: %s\n", dbConfig.Type)
17 23
 	fmt.Printf("Database host: %s:%d\n", dbConfig.Host, dbConfig.Port)

Laden…
Annuleren
Opslaan