Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

add logger of session #1283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,6 +90,8 @@ func (session *Session) Init() {

session.afterProcessors = make([]executedProcessor, 0)

session.logger = session.engine.logger

session.lastSQL = ""
session.lastSQLArgs = []interface{}{}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -649,7 +653,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
// !<winxxp>! 增加支持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() {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion session_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
38 changes: 19 additions & 19 deletions session_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions session_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions session_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -457,15 +457,15 @@ 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)
}
}

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
}
Expand Down
12 changes: 6 additions & 6 deletions session_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)))
Expand Down
Loading