Skip to content

Commit

Permalink
fixed issue of unwanted creation of transaction bug and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Dec 16, 2022
1 parent 6f41368 commit fe5ea37
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,32 @@ var txlog = func(lvl logging.Level, anon interface{}, s string, args ...interfac
} else {
s = fmt.Sprintf("%s - %s", lvl, s)
}
} else {
s = fmt.Sprintf(s, args...)
s = fmt.Sprintf("%s - %s", lvl, s)
}

connID := ""
txID := 0
switch typed := anon.(type) {
case *Connection:
connID = typed.ID
if typed.TX != nil {
txID = typed.TX.ID
}
case *Tx:
txID = typed.ID
case store:
tx, err := typed.Transaction()
if err == nil {
txID = tx.ID
connID := ""
txID := 0
extra := ""
switch typed := anon.(type) {
case *Connection:
connID = typed.ID
if typed.TX != nil {
txID = typed.TX.ID
}

extra = printStats(&typed.Store)
case *Tx:
txID = typed.ID
case store:
if t, ok := typed.(*Tx); ok {
txID = t.ID
}

extra = printStats(&typed)
}
}

if connID != "" || txID != 0 {
s = fmt.Sprintf("%s (conn=%v, tx=%v)", s, connID, txID)
s = fmt.Sprintf("%s (conn=%v, tx=%v%v)", s, connID, txID, extra)
} else {
s = fmt.Sprintf(s, args...)
s = fmt.Sprintf("%s - %s", lvl, s)
}

if Color {
Expand All @@ -82,3 +84,14 @@ var txlog = func(lvl logging.Level, anon interface{}, s string, args ...interfac

defaultStdLogger.Println(s)
}

// printStats returns a string represent connection pool information from
// the given store.
func printStats(s *store) string {
if db, ok := (*s).(*dB); ok {
s := db.Stats()
return fmt.Sprintf(", maxconn: %d, openconn: %d, in-use: %d, idle: %d", s.MaxOpenConnections, s.OpenConnections, s.InUse, s.Idle)
}

return ""
}

0 comments on commit fe5ea37

Please sign in to comment.