change logger of DB from glog.Logger to interface Logger

This commit is contained in:
John Guo 2021-08-03 20:34:26 +08:00
parent 114cdb2351
commit 28cb0bef25
7 changed files with 51 additions and 20 deletions

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)

View File

@ -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)
}
}
}
}