refactor logger
This commit is contained in:
parent
4e559eee5d
commit
4bbe5d039f
|
@ -48,7 +48,7 @@ func (s *Stmt) ExecMapContext(ctx context.Context, mp interface{}) (sql.Result,
|
|||
for k, i := range s.names {
|
||||
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
||||
}
|
||||
return s.Stmt.ExecContext(ctx, args...)
|
||||
return s.ExecContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error) {
|
||||
|
@ -65,13 +65,17 @@ func (s *Stmt) ExecStructContext(ctx context.Context, st interface{}) (sql.Resul
|
|||
for k, i := range s.names {
|
||||
args[i] = vv.Elem().FieldByName(k).Interface()
|
||||
}
|
||||
return s.Stmt.ExecContext(ctx, args...)
|
||||
return s.ExecContext(ctx, args...)
|
||||
}
|
||||
|
||||
func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error) {
|
||||
return s.ExecStructContext(context.Background(), st)
|
||||
}
|
||||
|
||||
func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
||||
return s.Stmt.ExecContext(ctx, args)
|
||||
}
|
||||
|
||||
func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) {
|
||||
rows, err := s.Stmt.QueryContext(ctx, args...)
|
||||
if err != nil {
|
||||
|
|
30
engine.go
30
engine.go
|
@ -34,8 +34,8 @@ type Engine struct {
|
|||
db *core.DB
|
||||
dialect dialects.Dialect
|
||||
|
||||
showSQL bool
|
||||
showExecTime bool
|
||||
//showSQL bool
|
||||
//showExecTime bool
|
||||
|
||||
logger log.ContextLogger
|
||||
TZLocation *time.Location // The timezone of the application
|
||||
|
@ -67,21 +67,16 @@ func (engine *Engine) BufferSize(size int) *Session {
|
|||
// ShowSQL show SQL statement or not on logger if log level is great than INFO
|
||||
func (engine *Engine) ShowSQL(show ...bool) {
|
||||
engine.logger.ShowSQL(show...)
|
||||
if len(show) == 0 {
|
||||
engine.showSQL = true
|
||||
} else {
|
||||
engine.showSQL = show[0]
|
||||
}
|
||||
}
|
||||
|
||||
// ShowExecTime show SQL statement and execute time or not on logger if log level is great than INFO
|
||||
func (engine *Engine) ShowExecTime(show ...bool) {
|
||||
/*func (engine *Engine) ShowExecTime(show ...bool) {
|
||||
if len(show) == 0 {
|
||||
engine.showExecTime = true
|
||||
} else {
|
||||
engine.showExecTime = show[0]
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// Logger return the logger interface
|
||||
func (engine *Engine) Logger() log.ContextLogger {
|
||||
|
@ -89,11 +84,18 @@ func (engine *Engine) Logger() log.ContextLogger {
|
|||
}
|
||||
|
||||
// SetLogger set the new logger
|
||||
func (engine *Engine) SetLogger(logger log.ContextLogger) {
|
||||
engine.logger = logger
|
||||
engine.showSQL = logger.IsShowSQL()
|
||||
engine.dialect.SetLogger(logger)
|
||||
engine.db.Logger = logger
|
||||
func (engine *Engine) SetLogger(logger interface{}) {
|
||||
var realLogger log.ContextLogger
|
||||
switch t := logger.(type) {
|
||||
case log.Logger:
|
||||
realLogger = log.NewLoggerAdapter(t)
|
||||
case log.ContextLogger:
|
||||
realLogger = t
|
||||
}
|
||||
engine.logger = realLogger
|
||||
//engine.showSQL = realLogger.IsShowSQL()
|
||||
engine.dialect.SetLogger(realLogger)
|
||||
engine.db.Logger = realLogger
|
||||
}
|
||||
|
||||
// SetLogLevel sets the logger level
|
||||
|
|
|
@ -135,7 +135,7 @@ func (eg *EngineGroup) SetDefaultCacher(cacher caches.Cacher) {
|
|||
}
|
||||
|
||||
// SetLogger set the new logger
|
||||
func (eg *EngineGroup) SetLogger(logger log.ContextLogger) {
|
||||
func (eg *EngineGroup) SetLogger(logger interface{}) {
|
||||
eg.Engine.SetLogger(logger)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetLogger(logger)
|
||||
|
@ -189,12 +189,12 @@ func (eg *EngineGroup) SetTableMapper(mapper names.Mapper) {
|
|||
}
|
||||
|
||||
// ShowExecTime show SQL statement and execute time or not on logger if log level is great than INFO
|
||||
func (eg *EngineGroup) ShowExecTime(show ...bool) {
|
||||
/*func (eg *EngineGroup) ShowExecTime(show ...bool) {
|
||||
eg.Engine.ShowExecTime(show...)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].ShowExecTime(show...)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// ShowSQL show SQL statement or not on logger if log level is great than INFO
|
||||
func (eg *EngineGroup) ShowSQL(show ...bool) {
|
||||
|
|
|
@ -98,7 +98,7 @@ type EngineInterface interface {
|
|||
SetConnMaxLifetime(time.Duration)
|
||||
SetColumnMapper(names.Mapper)
|
||||
SetDefaultCacher(caches.Cacher)
|
||||
SetLogger(logger log.ContextLogger)
|
||||
SetLogger(logger interface{})
|
||||
SetLogLevel(log.LogLevel)
|
||||
SetMapper(names.Mapper)
|
||||
SetMaxOpenConns(int)
|
||||
|
@ -107,7 +107,7 @@ type EngineInterface interface {
|
|||
SetTableMapper(names.Mapper)
|
||||
SetTZDatabase(tz *time.Location)
|
||||
SetTZLocation(tz *time.Location)
|
||||
ShowExecTime(...bool)
|
||||
//ShowExecTime(...bool)
|
||||
ShowSQL(show ...bool)
|
||||
Sync(...interface{}) error
|
||||
Sync2(...interface{}) error
|
||||
|
|
13
session.go
13
session.go
|
@ -82,7 +82,7 @@ func (session *Session) Init() {
|
|||
session.engine.DatabaseTZ,
|
||||
)
|
||||
|
||||
session.showSQL = session.engine.showSQL
|
||||
//session.showSQL = session.engine.showSQL
|
||||
session.isAutoCommit = true
|
||||
session.isCommitedOrRollbacked = false
|
||||
session.isAutoClose = false
|
||||
|
@ -865,17 +865,6 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
|
|||
func (session *Session) saveLastSQL(sql string, args ...interface{}) {
|
||||
session.lastSQL = sql
|
||||
session.lastSQLArgs = args
|
||||
session.logSQL(sql, args...)
|
||||
}
|
||||
|
||||
func (session *Session) logSQL(sqlStr string, sqlArgs ...interface{}) {
|
||||
if session.showSQL && !session.engine.showExecTime {
|
||||
if len(sqlArgs) > 0 {
|
||||
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, sqlArgs)
|
||||
} else {
|
||||
session.engine.logger.Infof("[SQL] %v", sqlStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LastSQL returns last query information
|
||||
|
|
|
@ -7,7 +7,6 @@ package xorm
|
|||
import (
|
||||
"database/sql"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm/core"
|
||||
"xorm.io/xorm/internal/statements"
|
||||
|
@ -27,27 +26,8 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
|
|||
|
||||
session.queryPreprocess(&sqlStr, args...)
|
||||
|
||||
if session.showSQL {
|
||||
session.lastSQL = sqlStr
|
||||
session.lastSQLArgs = args
|
||||
if session.engine.showExecTime {
|
||||
b4ExecTime := time.Now()
|
||||
defer func() {
|
||||
execDuration := time.Since(b4ExecTime)
|
||||
if len(args) > 0 {
|
||||
session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
|
||||
} else {
|
||||
session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
if len(args) > 0 {
|
||||
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
|
||||
} else {
|
||||
session.engine.logger.Infof("[SQL] %v", sqlStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
session.lastSQL = sqlStr
|
||||
session.lastSQLArgs = args
|
||||
|
||||
if session.isAutoCommit {
|
||||
var db *core.DB
|
||||
|
@ -156,25 +136,8 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
|
|||
|
||||
session.queryPreprocess(&sqlStr, args...)
|
||||
|
||||
if session.engine.showSQL {
|
||||
if session.engine.showExecTime {
|
||||
b4ExecTime := time.Now()
|
||||
defer func() {
|
||||
execDuration := time.Since(b4ExecTime)
|
||||
if len(args) > 0 {
|
||||
session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
|
||||
} else {
|
||||
session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
if len(args) > 0 {
|
||||
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
|
||||
} else {
|
||||
session.engine.logger.Infof("[SQL] %v", sqlStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
session.lastSQL = sqlStr
|
||||
session.lastSQLArgs = args
|
||||
|
||||
if !session.isAutoCommit {
|
||||
return session.tx.ExecContext(session.ctx, sqlStr, args...)
|
||||
|
|
Loading…
Reference in New Issue