diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index dd5232d08..e6b88dfd5 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -152,8 +152,8 @@ type DB interface { GetGroup() string // See Core.GetGroup. SetDryRun(enabled bool) // See Core.SetDryRun. GetDryRun() bool // See Core.GetDryRun. - SetLogger(logger *glog.Logger) // See Core.SetLogger. - GetLogger() *glog.Logger // See Core.GetLogger. + SetLogger(logger Logger) // See Core.SetLogger. + GetLogger() Logger // See Core.GetLogger. GetConfig() *ConfigNode // See Core.GetConfig. SetMaxIdleConnCount(n int) // See Core.SetMaxIdleConnCount. SetMaxOpenConnCount(n int) // See Core.SetMaxOpenConnCount. @@ -179,7 +179,7 @@ type Core struct { debug *gtype.Bool // Enable debug mode for the database, which can be changed in runtime. cache *gcache.Cache // Cache manager, SQL result cache only. schema *gtype.String // Custom schema for this object. - logger *glog.Logger // Logger. + logger Logger // Logger for logging functionality. config *ConfigNode // Current config node. } @@ -200,6 +200,12 @@ type Link interface { IsTransaction() bool } +// Logger is the logging interface for DB. +type Logger interface { + Error(ctx context.Context, s string) + Debug(ctx context.Context, s string) +} + // Sql is the sql recording struct. type Sql struct { Sql string // SQL string(may contain reserved char '?'). @@ -270,9 +276,6 @@ const ( ) var ( - // ErrNoRows is alias of sql.ErrNoRows. - ErrNoRows = sql.ErrNoRows - // instances is the management map for instances. instances = gmap.NewStrAnyMap(true) @@ -344,7 +347,7 @@ func New(group ...string) (db DB, err error) { debug: gtype.NewBool(), cache: gcache.New(), schema: gtype.NewString(), - logger: glog.New(), + logger: LoggerImp{glog.New()}, config: node, } if v, ok := driverMap[node.Type]; ok { diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 3b0cbe7f5..abbb101f8 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -659,9 +659,9 @@ func (c *Core) writeSqlToLogger(ctx context.Context, sql *Sql) { s := fmt.Sprintf("[%3d ms] [%s] %s%s", sql.End-sql.Start, sql.Group, transactionIdStr, sql.Format) if sql.Error != nil { s += "\nError: " + sql.Error.Error() - c.logger.Ctx(ctx).Error(s) + c.logger.Error(ctx, s) } else { - c.logger.Ctx(ctx).Debug(s) + c.logger.Debug(ctx, s) } } diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 0982d6130..3f5fb818e 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -12,8 +12,6 @@ import ( "time" "github.com/gogf/gf/os/gcache" - - "github.com/gogf/gf/os/glog" ) // Config is the configuration management object. @@ -135,12 +133,12 @@ func IsConfigured() bool { } // SetLogger sets the logger for orm. -func (c *Core) SetLogger(logger *glog.Logger) { +func (c *Core) SetLogger(logger Logger) { c.logger = logger } // GetLogger returns the logger of the orm. -func (c *Core) GetLogger() *glog.Logger { +func (c *Core) GetLogger() Logger { return c.logger } diff --git a/database/gdb/gdb_core_logger.go b/database/gdb/gdb_core_logger.go new file mode 100644 index 000000000..e9b5a3fb8 --- /dev/null +++ b/database/gdb/gdb_core_logger.go @@ -0,0 +1,27 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb + +import ( + "context" + "github.com/gogf/gf/os/glog" +) + +// LoggerImp is the default implementation of interface Logger for DB. +type LoggerImp struct { + *glog.Logger +} + +// Error implements function Error for interface Logger. +func (l LoggerImp) Error(ctx context.Context, s string) { + l.Ctx(ctx).Error(s) +} + +// Debug implements function Debug for interface Logger. +func (l LoggerImp) Debug(ctx context.Context, s string) { + l.Ctx(ctx).Debug(s) +} diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index 0919432d5..fab36478e 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -8,6 +8,7 @@ package gdb import ( "bytes" + "database/sql" "fmt" "reflect" "regexp" @@ -805,9 +806,9 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i } // formatError customizes and returns the SQL error. -func formatError(err error, sql string, args ...interface{}) error { - if err != nil && err != ErrNoRows { - return gerror.NewCodef(gerror.CodeDbOperationError, "%s, %s\n", err.Error(), FormatSqlWithArgs(sql, args)) +func formatError(err error, s string, args ...interface{}) error { + if err != nil && err != sql.ErrNoRows { + return gerror.NewCodef(gerror.CodeDbOperationError, "%s, %s\n", err.Error(), FormatSqlWithArgs(s, args)) } return err } diff --git a/database/gdb/gdb_z_mysql_ctx_test.go b/database/gdb/gdb_z_mysql_ctx_test.go index 0d3cf753a..9143bab77 100644 --- a/database/gdb/gdb_z_mysql_ctx_test.go +++ b/database/gdb/gdb_z_mysql_ctx_test.go @@ -30,7 +30,7 @@ func Test_Ctx(t *testing.T) { } func Test_Ctx_Query(t *testing.T) { - db.GetLogger().SetCtxKeys("SpanId", "TraceId") + db.GetLogger().(gdb.LoggerImp).SetCtxKeys("SpanId", "TraceId") gtest.C(t, func(t *gtest.T) { db.SetDebug(true) defer db.SetDebug(false) @@ -48,7 +48,7 @@ func Test_Ctx_Query(t *testing.T) { func Test_Ctx_Model(t *testing.T) { table := createInitTable() defer dropTable(table) - db.GetLogger().SetCtxKeys("SpanId", "TraceId") + db.GetLogger().(gdb.LoggerImp).SetCtxKeys("SpanId", "TraceId") gtest.C(t, func(t *gtest.T) { db.SetDebug(true) defer db.SetDebug(false) diff --git a/frame/gins/gins_database.go b/frame/gins/gins_database.go index ec854722e..ebd65b982 100644 --- a/frame/gins/gins_database.go +++ b/frame/gins/gins_database.go @@ -132,8 +132,10 @@ func Database(name ...string) gdb.DB { loggerConfigMap = Config().GetMap(configNodeKey) } if len(loggerConfigMap) > 0 { - if err := db.GetLogger().SetConfigWithMap(loggerConfigMap); err != nil { - panic(err) + if logger, ok := db.GetLogger().(gdb.LoggerImp); ok { + if err := logger.SetConfigWithMap(loggerConfigMap); err != nil { + panic(err) + } } } }