From 7794e589500885a290bbfbcee0d6630c2aec6002 Mon Sep 17 00:00:00 2001 From: reign_dev Date: Tue, 30 Apr 2019 15:45:33 +0800 Subject: [PATCH 1/2] add logger of session --- session.go | 29 ++++++++++++++++++++++++----- session_context.go | 2 +- session_convert.go | 38 +++++++++++++++++++------------------- session_delete.go | 4 ++-- session_find.go | 16 ++++++++-------- session_get.go | 12 ++++++------ session_insert.go | 14 +++++++------- session_raw.go | 16 ++++++++-------- session_schema.go | 2 +- session_update.go | 22 +++++++++++----------- 10 files changed, 87 insertions(+), 68 deletions(-) diff --git a/session.go b/session.go index fdb58c58b..b4836d565 100644 --- a/session.go +++ b/session.go @@ -35,6 +35,8 @@ type Session struct { isCommitedOrRollbacked bool isAutoClose bool + logger core.ILogger + // Automatically reset the statement after operations that execute a SQL // query such as Count(), Find(), Get(), ... autoResetStatement bool @@ -88,6 +90,8 @@ func (session *Session) Init() { session.afterProcessors = make([]executedProcessor, 0) + session.logger = session.logger + session.lastSQL = "" session.lastSQLArgs = []interface{}{} @@ -424,7 +428,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b fieldValue, err := session.getField(dataStruct, key, table, idx) if err != nil { if !strings.Contains(err.Error(), "is not valid") { - session.engine.logger.Warn(err) + session.logger.Warn(err) } continue } @@ -609,7 +613,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b z, _ := t.Zone() // set new location if database don't save timezone or give an incorrect timezone if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbTZ.String() { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location - session.engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location()) + session.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location()) t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), dbTZ) } @@ -627,7 +631,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b hasAssigned = true t, err := session.byte2Time(col, d) if err != nil { - session.engine.logger.Error("byte2Time error:", err.Error()) + session.logger.Error("byte2Time error:", err.Error()) hasAssigned = false } else { fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) @@ -636,7 +640,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b hasAssigned = true t, err := session.str2Time(col, d) if err != nil { - session.engine.logger.Error("byte2Time error:", err.Error()) + session.logger.Error("byte2Time error:", err.Error()) hasAssigned = false } else { fieldValue.Set(reflect.ValueOf(t).Convert(fieldType)) @@ -649,7 +653,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b // !! 增加支持sql.Scanner接口的结构,如sql.NullString hasAssigned = true if err := nulVal.Scan(vv.Interface()); err != nil { - session.engine.logger.Error("sql.Sanner error:", err.Error()) + session.logger.Error("sql.Sanner error:", err.Error()) hasAssigned = false } } else if col.SQLType.IsJson() { @@ -864,3 +868,18 @@ func (session *Session) incrVersionFieldValue(fieldValue *reflect.Value) { fieldValue.SetUint(fieldValue.Uint() + 1) } } + +// SetLogger set the new logger +func (session *Session) SetLogger(logger core.ILogger) { + session.logger = logger +} + +// Logger return the logger interface +func (session *Session) Logger() core.ILogger { + return session.logger +} + +// SetLogLevel sets the logger level +func (session *Session) SetLogLevel(level core.LogLevel) { + session.logger.SetLevel(level) +} diff --git a/session_context.go b/session_context.go index 915f05685..10ac64387 100644 --- a/session_context.go +++ b/session_context.go @@ -18,6 +18,6 @@ func (session *Session) PingContext(ctx context.Context) error { defer session.Close() } - session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName()) + session.logger.Infof("PING DATABASE %v", session.engine.DriverName()) return session.DB().PingContext(ctx) } diff --git a/session_convert.go b/session_convert.go index 6b6fbdeaf..90671308a 100644 --- a/session_convert.go +++ b/session_convert.go @@ -33,27 +33,27 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti sd, err := strconv.ParseInt(sdata, 10, 64) if err == nil { x = time.Unix(sd, 0) - //session.engine.logger.Debugf("time(0) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(0) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } else { - //session.engine.logger.Debugf("time(0) err key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(0) err key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } } else if len(sdata) > 19 && strings.Contains(sdata, "-") { x, err = time.ParseInLocation(time.RFC3339Nano, sdata, parseLoc) - session.engine.logger.Debugf("time(1) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + session.logger.Debugf("time(1) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) if err != nil { x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, parseLoc) - //session.engine.logger.Debugf("time(2) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(2) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } if err != nil { x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, parseLoc) - //session.engine.logger.Debugf("time(3) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(3) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } } else if len(sdata) == 19 && strings.Contains(sdata, "-") { x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, parseLoc) - //session.engine.logger.Debugf("time(4) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(4) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' { x, err = time.ParseInLocation("2006-01-02", sdata, parseLoc) - //session.engine.logger.Debugf("time(5) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(5) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } else if col.SQLType.Name == core.Time { if strings.Contains(sdata, " ") { ssd := strings.Split(sdata, " ") @@ -67,7 +67,7 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti st := fmt.Sprintf("2006-01-02 %v", sdata) x, err = time.ParseInLocation("2006-01-02 15:04:05", st, parseLoc) - //session.engine.logger.Debugf("time(6) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) + //session.logger.Debugf("time(6) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata) } else { outErr = fmt.Errorf("unsupported time format %v", sdata) return @@ -104,7 +104,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, if len(data) > 0 { err := DefaultJSONHandler.Unmarshal(data, x.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return err } fieldValue.Set(x.Elem()) @@ -118,7 +118,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, if len(data) > 0 { err := DefaultJSONHandler.Unmarshal(data, x.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return err } fieldValue.Set(x.Elem()) @@ -131,7 +131,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, if len(data) > 0 { err := DefaultJSONHandler.Unmarshal(data, x.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return err } fieldValue.Set(x.Elem()) @@ -260,7 +260,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, if len(data) > 0 { err := DefaultJSONHandler.Unmarshal(data, &x) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return err } fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType)) @@ -271,7 +271,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, if len(data) > 0 { err := DefaultJSONHandler.Unmarshal(data, &x) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return err } fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType)) @@ -563,7 +563,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val if fieldValue.IsNil() { return nil, nil } else if !fieldValue.IsValid() { - session.engine.logger.Warn("the field[", col.FieldName, "] is invalid") + session.logger.Warn("the field[", col.FieldName, "] is invalid") return nil, nil } else { // !nashtsai! deference pointer type to instance type @@ -605,14 +605,14 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val if col.SQLType.IsText() { bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return 0, err } return string(bytes), nil } else if col.SQLType.IsBlob() { bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return 0, err } return bytes, nil @@ -621,7 +621,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val case reflect.Complex64, reflect.Complex128: bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return 0, err } return string(bytes), nil @@ -633,7 +633,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val if col.SQLType.IsText() { bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return 0, err } return string(bytes), nil @@ -646,7 +646,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val } else { bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) return 0, err } } diff --git a/session_delete.go b/session_delete.go index 26782f692..4ad5dc190 100644 --- a/session_delete.go +++ b/session_delete.go @@ -61,14 +61,14 @@ func (session *Session) cacheDelete(table *core.Table, tableName, sqlStr string, } for _, id := range ids { - session.engine.logger.Debug("[cacheDelete] delete cache obj:", tableName, id) + session.logger.Debug("[cacheDelete] delete cache obj:", tableName, id) sid, err := id.ToString() if err != nil { return err } cacher.DelBean(tableName, sid) } - session.engine.logger.Debug("[cacheDelete] clear cache table:", tableName) + session.logger.Debug("[cacheDelete] clear cache table:", tableName) cacher.ClearIds(tableName) return nil } diff --git a/session_find.go b/session_find.go index 48ee32098..0aef183d1 100644 --- a/session_find.go +++ b/session_find.go @@ -188,7 +188,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) return err } err = nil // !nashtsai! reset err to nil for ErrCacheFailed - session.engine.logger.Warn("Cache Find Failed") + session.logger.Warn("Cache Find Failed") } } @@ -354,7 +354,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in for rows.Next() { i++ if i > 500 { - session.engine.logger.Debug("[cacheFind] ids length > 500, no cache") + session.logger.Debug("[cacheFind] ids length > 500, no cache") return ErrCacheFailed } var res = make([]string, len(table.PrimaryKeys)) @@ -373,13 +373,13 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in ids = append(ids, pk) } - session.engine.logger.Debug("[cacheFind] cache sql:", ids, tableName, sqlStr, newsql, args) + session.logger.Debug("[cacheFind] cache sql:", ids, tableName, sqlStr, newsql, args) err = core.PutCacheSql(cacher, ids, tableName, newsql, args) if err != nil { return err } } else { - session.engine.logger.Debug("[cacheFind] cache hit sql:", tableName, sqlStr, newsql, args) + session.logger.Debug("[cacheFind] cache hit sql:", tableName, sqlStr, newsql, args) } sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr)) @@ -398,7 +398,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in ides = append(ides, id) ididxes[sid] = idx } else { - session.engine.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean) + session.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean) pk := session.engine.IdOf(bean) xid, err := pk.ToString() @@ -407,7 +407,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in } if sid != xid { - session.engine.logger.Error("[cacheFind] error cache", xid, sid, bean) + session.logger.Error("[cacheFind] error cache", xid, sid, bean) return ErrCacheFailed } temps[idx] = bean @@ -457,7 +457,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in bean := rv.Interface() temps[ididxes[sid]] = bean - session.engine.logger.Debug("[cacheFind] cache bean:", tableName, id, bean, temps) + session.logger.Debug("[cacheFind] cache bean:", tableName, id, bean, temps) cacher.PutBean(tableName, sid, bean) } } @@ -465,7 +465,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in for j := 0; j < len(temps); j++ { bean := temps[j] if bean == nil { - session.engine.logger.Warn("[cacheFind] cache no hit:", tableName, ids[j], temps) + session.logger.Warn("[cacheFind] cache no hit:", tableName, ids[j], temps) // return errors.New("cache error") // !nashtsai! no need to return error, but continue instead continue } diff --git a/session_get.go b/session_get.go index 5ecf2f377..0b7e8caa8 100644 --- a/session_get.go +++ b/session_get.go @@ -166,7 +166,7 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf tableName := session.statement.TableName() cacher := session.engine.getCacher(tableName) - session.engine.logger.Debug("[cacheGet] find sql:", newsql, args) + session.logger.Debug("[cacheGet] find sql:", newsql, args) table := session.statement.RefTable ids, err := core.GetCacheSql(cacher, tableName, newsql, args) if err != nil { @@ -202,19 +202,19 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf } ids = []core.PK{pk} - session.engine.logger.Debug("[cacheGet] cache ids:", newsql, ids) + session.logger.Debug("[cacheGet] cache ids:", newsql, ids) err = core.PutCacheSql(cacher, ids, tableName, newsql, args) if err != nil { return false, err } } else { - session.engine.logger.Debug("[cacheGet] cache hit sql:", newsql, ids) + session.logger.Debug("[cacheGet] cache hit sql:", newsql, ids) } if len(ids) > 0 { structValue := reflect.Indirect(reflect.ValueOf(bean)) id := ids[0] - session.engine.logger.Debug("[cacheGet] get bean:", tableName, id) + session.logger.Debug("[cacheGet] get bean:", tableName, id) sid, err := id.ToString() if err != nil { return false, err @@ -227,10 +227,10 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf return has, err } - session.engine.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean) + session.logger.Debug("[cacheGet] cache bean:", tableName, id, cacheBean) cacher.PutBean(tableName, sid, cacheBean) } else { - session.engine.logger.Debug("[cacheGet] cache hit bean:", tableName, id, cacheBean) + session.logger.Debug("[cacheGet] cache hit bean:", tableName, id, cacheBean) has = true } structValue.Set(reflect.Indirect(reflect.ValueOf(cacheBean))) diff --git a/session_insert.go b/session_insert.go index aa2a432b3..89bad3137 100644 --- a/session_insert.go +++ b/session_insert.go @@ -440,7 +440,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { if table.Version != "" && session.statement.checkVersion { verValue, err := table.VersionColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } else if verValue.IsValid() && verValue.CanSet() { session.incrVersionFieldValue(verValue) } @@ -458,7 +458,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { aiValue, err := table.AutoIncrColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() { @@ -481,7 +481,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { if table.Version != "" && session.statement.checkVersion { verValue, err := table.VersionColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } else if verValue.IsValid() && verValue.CanSet() { session.incrVersionFieldValue(verValue) } @@ -499,7 +499,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { aiValue, err := table.AutoIncrColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() { @@ -522,7 +522,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { if table.Version != "" && session.statement.checkVersion { verValue, err := table.VersionColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } else if verValue.IsValid() && verValue.CanSet() { session.incrVersionFieldValue(verValue) } @@ -540,7 +540,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { aiValue, err := table.AutoIncrColumn().ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() { @@ -572,7 +572,7 @@ func (session *Session) cacheInsert(table string) error { if cacher == nil { return nil } - session.engine.logger.Debug("[cache] clear sql:", table) + session.logger.Debug("[cache] clear sql:", table) cacher.ClearIds(table) return nil } diff --git a/session_raw.go b/session_raw.go index c25563656..42427a996 100644 --- a/session_raw.go +++ b/session_raw.go @@ -33,16 +33,16 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row defer func() { execDuration := time.Since(b4ExecTime) if len(args) > 0 { - session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration) + session.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration) } else { - session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration) + session.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration) } }() } else { if len(args) > 0 { - session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args) + session.logger.Infof("[SQL] %v %#v", sqlStr, args) } else { - session.engine.logger.Infof("[SQL] %v", sqlStr) + session.logger.Infof("[SQL] %v", sqlStr) } } } @@ -160,16 +160,16 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er defer func() { execDuration := time.Since(b4ExecTime) if len(args) > 0 { - session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration) + session.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration) } else { - session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration) + session.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration) } }() } else { if len(args) > 0 { - session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args) + session.logger.Infof("[SQL] %v %#v", sqlStr, args) } else { - session.engine.logger.Infof("[SQL] %v", sqlStr) + session.logger.Infof("[SQL] %v", sqlStr) } } } diff --git a/session_schema.go b/session_schema.go index 7629906f5..507b1b06e 100644 --- a/session_schema.go +++ b/session_schema.go @@ -18,7 +18,7 @@ func (session *Session) Ping() error { defer session.Close() } - session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName()) + session.logger.Infof("PING DATABASE %v", session.engine.DriverName()) return session.DB().PingContext(session.ctx) } diff --git a/session_update.go b/session_update.go index 1c20556c4..7ca3f7e68 100644 --- a/session_update.go +++ b/session_update.go @@ -28,7 +28,7 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, for _, filter := range session.engine.dialect.Filters() { newsql = filter.Do(newsql, session.engine.dialect, table) } - session.engine.logger.Debug("[cacheUpdate] new sql", oldhead, newsql) + session.logger.Debug("[cacheUpdate] new sql", oldhead, newsql) var nStart int if len(args) > 0 { @@ -41,7 +41,7 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, } cacher := session.engine.getCacher(tableName) - session.engine.logger.Debug("[cacheUpdate] get cache sql", newsql, args[nStart:]) + session.logger.Debug("[cacheUpdate] get cache sql", newsql, args[nStart:]) ids, err := core.GetCacheSql(cacher, tableName, newsql, args[nStart:]) if err != nil { rows, err := session.NoCache().queryRows(newsql, args[nStart:]...) @@ -74,7 +74,7 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, ids = append(ids, pk) } - session.engine.logger.Debug("[cacheUpdate] find updated id", ids) + session.logger.Debug("[cacheUpdate] find updated id", ids) } /*else { session.engine.LogDebug("[xorm:cacheUpdate] del cached sql:", tableName, newsql, args) cacher.DelIds(tableName, genSqlKey(newsql, args)) @@ -105,16 +105,16 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, } else if strings.Contains(colName, session.engine.QuoteStr()) { colName = strings.TrimSpace(strings.Replace(colName, session.engine.QuoteStr(), "", -1)) } else { - session.engine.logger.Debug("[cacheUpdate] cannot find column", tableName, colName) + session.logger.Debug("[cacheUpdate] cannot find column", tableName, colName) return ErrCacheFailed } if col := table.GetColumn(colName); col != nil { fieldValue, err := col.ValueOf(bean) if err != nil { - session.engine.logger.Error(err) + session.logger.Error(err) } else { - session.engine.logger.Debug("[cacheUpdate] set bean field", bean, colName, fieldValue.Interface()) + session.logger.Debug("[cacheUpdate] set bean field", bean, colName, fieldValue.Interface()) if col.IsVersion && session.statement.checkVersion { session.incrVersionFieldValue(fieldValue) } else { @@ -122,16 +122,16 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string, } } } else { - session.engine.logger.Errorf("[cacheUpdate] ERROR: column %v is not table %v's", + session.logger.Errorf("[cacheUpdate] ERROR: column %v is not table %v's", colName, table.Name) } } - session.engine.logger.Debug("[cacheUpdate] update cache", tableName, id, bean) + session.logger.Debug("[cacheUpdate] update cache", tableName, id, bean) cacher.PutBean(tableName, sid, bean) } } - session.engine.logger.Debug("[cacheUpdate] clear cached table sql:", tableName) + session.logger.Debug("[cacheUpdate] clear cached table sql:", tableName) cacher.ClearIds(tableName) return nil } @@ -383,7 +383,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if cacher := session.engine.getCacher(tableName); cacher != nil && session.statement.UseCache { //session.cacheUpdate(table, tableName, sqlStr, args...) - session.engine.logger.Debug("[cacheUpdate] clear table ", tableName) + session.logger.Debug("[cacheUpdate] clear table ", tableName) cacher.ClearIds(tableName) cacher.ClearBeans(tableName) } @@ -394,7 +394,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 closure(bean) } if processor, ok := interface{}(bean).(AfterUpdateProcessor); ok { - session.engine.logger.Debug("[event]", tableName, " has after update processor") + session.logger.Debug("[event]", tableName, " has after update processor") processor.AfterUpdate() } } else { From 6e1a08b0de7186de7307389b3b3c118bd6d0bb99 Mon Sep 17 00:00:00 2001 From: reign_dev Date: Tue, 30 Apr 2019 15:52:51 +0800 Subject: [PATCH 2/2] add logger of session --- session.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session.go b/session.go index b4836d565..707382d80 100644 --- a/session.go +++ b/session.go @@ -90,7 +90,7 @@ func (session *Session) Init() { session.afterProcessors = make([]executedProcessor, 0) - session.logger = session.logger + session.logger = session.engine.logger session.lastSQL = "" session.lastSQLArgs = []interface{}{}