Repository: zeromicro/go-zero
Pull request #2416 (2022-09-19 09:45:18)

cleanup: deprecated field and func


Patch:
diff --git a/core/logx/config.go b/core/logx/config.go
index eb79f4ee..dd96b6eb 100644
--- a/core/logx/config.go
+++ b/core/logx/config.go
@@ -7,7 +7,7 @@ type LogConf struct {
 	Encoding            string `json:",default=json,options=[json,plain]"`
 	TimeFormat          string `json:",optional"`
 	Path                string `json:",default=logs"`
-	Level               string `json:",default=info,options=[info,error,severe]"`
+	Level               string `json:",default=info,options=[debug,info,error,severe]"`
 	Compress            bool   `json:",optional"`
 	KeepDays            int    `json:",optional"`
 	StackCooldownMillis int    `json:",default=100"`
diff --git a/core/logx/logger.go b/core/logx/logger.go
index ca690766..ddb4ada7 100644
--- a/core/logx/logger.go
+++ b/core/logx/logger.go
@@ -7,6 +7,14 @@ import (
 
 // A Logger represents a logger.
 type Logger interface {
+	// Debug logs a message at info level.
+	Debug(...interface{})
+	// Debugf logs a message at info level.
+	Debugf(string, ...interface{})
+	// Debugv logs a message at info level.
+	Debugv(interface{})
+	// Debugw logs a message at info level.
+	Debugw(string, ...LogField)
 	// Error logs a message at error level.
 	Error(...interface{})
 	// Errorf logs a message at error level.
diff --git a/core/logx/logs.go b/core/logx/logs.go
index 28fd5c06..621b1ee3 100644
--- a/core/logx/logs.go
+++ b/core/logx/logs.go
@@ -64,6 +64,26 @@ func Close() error {
 	return nil
 }
 
+// Debug writes v into access log.
+func Debug(v ...interface{}) {
+	writeDebug(fmt.Sprint(v...))
+}
+
+// Debugf writes v with format into access log.
+func Debugf(format string, v ...interface{}) {
+	writeDebug(fmt.Sprintf(format, v...))
+}
+
+// Debugv writes v into access log with json content.
+func Debugv(v interface{}) {
+	writeDebug(v)
+}
+
+// Debugw writes msg along with fields into access log.
+func Debugw(msg string, fields ...LogField) {
+	writeDebug(msg, fields...)
+}
+
 // Disable disables the logging.
 func Disable() {
 	atomic.StoreUint32(&disableLog, 1)
@@ -352,6 +372,8 @@ func handleOptions(opts []LogOption) {
 
 func setupLogLevel(c LogConf) {
 	switch c.Level {
+	case levelDebug:
+		SetLevel(DebugLevel)
 	case levelInfo:
 		SetLevel(InfoLevel)
 	case levelError:
@@ -392,6 +414,12 @@ func shallLogStat() bool {
 	return atomic.LoadUint32(&disableStat) == 0
 }
 
+func writeDebug(val interface{}, fields ...LogField) {
+	if shallLog(DebugLevel) {
+		getWriter().Debug(val, addCaller(fields...)...)
+	}
+}
+
 func writeError(val interface{}, fields ...LogField) {
 	if shallLog(ErrorLevel) {
 		getWriter().Error(val, addCaller(fields...)...)
diff --git a/core/logx/richlogger.go b/core/logx/richlogger.go
index de52a80e..f73fddf9 100644
--- a/core/logx/richlogger.go
+++ b/core/logx/richlogger.go
@@ -40,6 +40,22 @@ type richLogger struct {
 	fields     []LogField
 }
 
+func (l *richLogger) Debug(v ...interface{}) {
+	l.debug(fmt.Sprint(v...))
+}
+
+func (l *richLogger) Debugf(format string, v ...interface{}) {
+	l.debug(fmt.Sprintf(format, v...))
+}
+
+func (l *richLogger) Debugv(v interface{}) {
+	l.debug(v)
+}
+
+func (l *richLogger) Debugw(msg string, fields ...LogField) {
+	l.debug(msg, fields...)
+}
+
 func (l *richLogger) Error(v ...interface{}) {
 	l.err(fmt.Sprint(v...))
 }
@@ -135,6 +151,12 @@ func (l *richLogger) buildFields(fields ...LogField) []LogField {
 	return fields
 }
 
+func (l *richLogger) debug(v interface{}, fields ...LogField) {
+	if shallLog(DebugLevel) {
+		getWriter().Debug(v, l.buildFields(fields...)...)
+	}
+}
+
 func (l *richLogger) err(v interface{}, fields ...LogField) {
 	if shallLog(ErrorLevel) {
 		getWriter().Error(v, l.buildFields(fields...)...)
diff --git a/core/logx/vars.go b/core/logx/vars.go
index 26c4fd84..b95c3215 100644
--- a/core/logx/vars.go
+++ b/core/logx/vars.go
@@ -3,8 +3,10 @@ package logx
 import "errors"
 
 const (
-	// InfoLevel logs everything
-	InfoLevel uint32 = iota
+	// DebugLevel logs everything
+	DebugLevel uint32 = iota
+	// InfoLevel does not include debugs
+	InfoLevel
 	// ErrorLevel includes errors, slows, stacks
 	ErrorLevel
 	// SevereLevel only log severe messages
@@ -37,6 +39,7 @@ const (
 	levelFatal  = "fatal"
 	levelSlow   = "slow"
 	levelStat   = "stat"
+	levelDebug  = "debug"
 
 	backupFileDelimiter = "-"
 	flags               = 0x0
diff --git a/core/logx/writer.go b/core/logx/writer.go
index 5f372566..16e96976 100644
--- a/core/logx/writer.go
+++ b/core/logx/writer.go
@@ -18,6 +18,7 @@ type (
 	Writer interface {
 		Alert(v interface{})
 		Close() error
+		Debug(v interface{}, fields ...LogField)
 		Error(v interface{}, fields ...LogField)
 		Info(v interface{}, fields ...LogField)
 		Severe(v interface{})
@@ -194,6 +195,10 @@ func (w *concreteWriter) Close() error {
 	return w.statLog.Close()
 }
 
+func (w *concreteWriter) Debug(v interface{}, fields ...LogField) {
+	output(w.infoLog, levelDebug, v, fields...)
+}
+
 func (w *concreteWriter) Error(v interface{}, fields ...LogField) {
 	output(w.errorLog, levelError, v, fields...)
 }
@@ -227,6 +232,9 @@ func (n nopWriter) Close() error {
 	return nil
 }
 
+func (n nopWriter) Debug(_ interface{}, _ ...LogField) {
+}
+
 func (n nopWriter) Error(_ interface{}, _ ...LogField) {
 }
 
diff --git a/core/stores/redis/hook.go b/core/stores/redis/hook.go
index 5229bd27..7994fdd7 100644
--- a/core/stores/redis/hook.go
+++ b/core/stores/redis/hook.go
@@ -2,10 +2,13 @@ package redis
 
 import (
 	"context"
+	"io"
+	"net"
 	"strings"
 	"time"
 
 	red "github.com/go-redis/redis/v8"
+	"github.com/zeromicro/go-zero/core/breaker"
 	"github.com/zeromicro/go-zero/core/errorx"
 	"github.com/zeromicro/go-zero/core/logx"
 	"github.com/zeromicro/go-zero/core/mapping"
@@ -116,24 +119,17 @@ func formatError(err error) string {
 		return ""
 	}
 
-	es := err.Error()
-	switch {
-	case strings.HasPrefix(es, "read"):
-		return "read timeout"
-	case strings.HasPrefix(es, "dial"):
-		if strings.Contains(es, "connection refused") {
-			return "connection refused"
-		}
-		return "dial timeout"
-	case strings.HasPrefix(es, "write"):
-		return "write timeout"
-	case strings.Contains(es, "EOF"):
+	opErr, ok := err.(*net.OpError)
+	if ok && opErr.Timeout() {
+		return "timeout"
+	}
+
+	switch err {
+	case io.EOF:
 		return "eof"
-	case strings.Contains(es, "reset"):
-		return "reset"
-	case strings.Contains(es, "broken"):
-		return "broken pipe"
-	case strings.Contains(es, "breaker"):
+	case context.DeadlineExceeded:
+		return "context deadline"
+	case breaker.ErrServiceUnavailable:
 		return "breaker"
 	default:
 		return "unexpected error"
diff --git a/tools/goctl/model/sql/README.MD b/tools/goctl/model/sql/README.MD
index 574f7dc6..772dda04 100644
--- a/tools/goctl/model/sql/README.MD
+++ b/tools/goctl/model/sql/README.MD
@@ -35,16 +35,16 @@ goctl model 为go-zero下的工具模块中的组件之一，目前支持识别m
     	"fmt"
     	"strings"
     	"time"
-    
+        
+        "github.com/zeromicro/go-zero/core/stores/builder"
     	"github.com/zeromicro/go-zero/core/stores/cache"
     	"github.com/zeromicro/go-zero/core/stores/sqlc"
     	"github.com/zeromicro/go-zero/core/stores/sqlx"
     	"github.com/zeromicro/go-zero/core/stringx"
-    	"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
     )
     
     var (
-    	userFieldNames          = builderx.FieldNames(&User{})
+    	userFieldNames          = builder.FieldNames(&User{})
     	userRows                = strings.Join(userFieldNames, ",")
     	userRowsExpectAutoSet   = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
     	userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"


Test patch:
diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go
index 3be37648..70ec4c7f 100644
--- a/core/logx/logs_test.go
+++ b/core/logx/logs_test.go
@@ -35,6 +35,12 @@ func (mw *mockWriter) Alert(v interface{}) {
 	output(&mw.builder, levelAlert, v)
 }
 
+func (mw *mockWriter) Debug(v interface{}, fields ...LogField) {
+	mw.lock.Lock()
+	defer mw.lock.Unlock()
+	output(&mw.builder, levelDebug, v, fields...)
+}
+
 func (mw *mockWriter) Error(v interface{}, fields ...LogField) {
 	mw.lock.Lock()
 	defer mw.lock.Unlock()
@@ -212,6 +218,46 @@ func TestStructedLogAlert(t *testing.T) {
 	})
 }
 
+func TestStructedLogDebug(t *testing.T) {
+	w := new(mockWriter)
+	old := writer.Swap(w)
+	defer writer.Store(old)
+
+	doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
+		Debug(v...)
+	})
+}
+
+func TestStructedLogDebugf(t *testing.T) {
+	w := new(mockWriter)
+	old := writer.Swap(w)
+	defer writer.Store(old)
+
+	doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
+		Debugf(fmt.Sprint(v...))
+	})
+}
+
+func TestStructedLogDebugv(t *testing.T) {
+	w := new(mockWriter)
+	old := writer.Swap(w)
+	defer writer.Store(old)
+
+	doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
+		Debugv(fmt.Sprint(v...))
+	})
+}
+
+func TestStructedLogDebugw(t *testing.T) {
+	w := new(mockWriter)
+	old := writer.Swap(w)
+	defer writer.Store(old)
+
+	doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
+		Debugw(fmt.Sprint(v...), Field("foo", time.Second))
+	})
+}
+
 func TestStructedLogError(t *testing.T) {
 	w := new(mockWriter)
 	old := writer.Swap(w)
diff --git a/core/logx/richlogger_test.go b/core/logx/richlogger_test.go
index 08777752..7cf373a5 100644
--- a/core/logx/richlogger_test.go
+++ b/core/logx/richlogger_test.go
@@ -37,6 +37,41 @@ func TestTraceLog(t *testing.T) {
 	validate(t, w.String(), true, true)
 }
 
+func TestTraceDebug(t *testing.T) {
+	w := new(mockWriter)
+	old := writer.Swap(w)
+	writer.lock.RLock()
+	defer func() {
+		writer.lock.RUnlock()
+		writer.Store(old)
+	}()
+
+	otp := otel.GetTracerProvider()
+	tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
+	otel.SetTracerProvider(tp)
+	defer otel.SetTracerProvider(otp)
+
+	ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
+	defer span.End()
+
+	l := WithContext(ctx)
+	SetLevel(DebugLevel)
+	l.WithDuration(time.Second).Debug(testlog)
+	assert.True(t, strings.Contains(w.String(), traceKey))
+	assert.True(t, strings.Contains(w.String(), spanKey))
+	w.Reset()
+	l.WithDuration(time.Second).Debugf(testlog)
+	validate(t, w.String(), true, true)
+	w.Reset()
+	l.WithDuration(time.Second).Debugv(testlog)
+	validate(t, w.String(), true, true)
+	w.Reset()
+	l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
+	validate(t, w.String(), true, true)
+	assert.True(t, strings.Contains(w.String(), "foo"), w.String())
+	assert.True(t, strings.Contains(w.String(), "bar"), w.String())
+}
+
 func TestTraceError(t *testing.T) {
 	w := new(mockWriter)
 	old := writer.Swap(w)
diff --git a/tools/goctl/model/sql/gen/gen_test.go b/tools/goctl/model/sql/gen/gen_test.go
index e73ecc6e..136bfab4 100644
--- a/tools/goctl/model/sql/gen/gen_test.go
+++ b/tools/goctl/model/sql/gen/gen_test.go
@@ -13,11 +13,11 @@ import (
 
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
+
 	"github.com/zeromicro/go-zero/core/logx"
+	"github.com/zeromicro/go-zero/core/stores/builder"
 	"github.com/zeromicro/go-zero/core/stringx"
-
 	"github.com/zeromicro/go-zero/tools/goctl/config"
-	"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
 	"github.com/zeromicro/go-zero/tools/goctl/model/sql/parser"
 	"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
 )
@@ -158,7 +158,7 @@ func TestFields(t *testing.T) {
 		UpdateTime sql.NullTime    `db:"update_time"`
 	}
 	var (
-		studentFieldNames          = builderx.RawFieldNames(&Student{})
+		studentFieldNames          = builder.RawFieldNames(&Student{})
 		studentRows                = strings.Join(studentFieldNames, ",")
 		studentRowsExpectAutoSet   = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
 		studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
diff --git a/tools/goctl/model/sql/test/model/studentmodel.go b/tools/goctl/model/sql/test/model/studentmodel.go
index 9a79105c..00903f2c 100755
--- a/tools/goctl/model/sql/test/model/studentmodel.go
+++ b/tools/goctl/model/sql/test/model/studentmodel.go
@@ -6,15 +6,15 @@ import (
 	"strings"
 	"time"
 
+	"github.com/zeromicro/go-zero/core/stores/builder"
 	"github.com/zeromicro/go-zero/core/stores/cache"
 	"github.com/zeromicro/go-zero/core/stores/sqlc"
 	"github.com/zeromicro/go-zero/core/stores/sqlx"
 	"github.com/zeromicro/go-zero/core/stringx"
-	"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
 )
 
 var (
-	studentFieldNames          = builderx.RawFieldNames(&Student{})
+	studentFieldNames          = builder.RawFieldNames(&Student{})
 	studentRows                = strings.Join(studentFieldNames, ",")
 	studentRowsExpectAutoSet   = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
 	studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
diff --git a/tools/goctl/model/sql/test/model/usermodel.go b/tools/goctl/model/sql/test/model/usermodel.go
index 7076b1e2..322861d8 100755
--- a/tools/goctl/model/sql/test/model/usermodel.go
+++ b/tools/goctl/model/sql/test/model/usermodel.go
@@ -6,14 +6,14 @@ import (
 	"strings"
 	"time"
 
+	"github.com/zeromicro/go-zero/core/stores/builder"
 	"github.com/zeromicro/go-zero/core/stores/sqlc"
 	"github.com/zeromicro/go-zero/core/stores/sqlx"
 	"github.com/zeromicro/go-zero/core/stringx"
-	"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
 )
 
 var (
-	userFieldNames          = builderx.RawFieldNames(&User{})
+	userFieldNames          = builder.RawFieldNames(&User{})
 	userRows                = strings.Join(userFieldNames, ",")
 	userRowsExpectAutoSet   = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
 	userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"