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