Skip to content

Commit

Permalink
logMode codes more readable (go-gorm#2216)
Browse files Browse the repository at this point in the history
  • Loading branch information
sairoutine authored and jinzhu committed Dec 13, 2018
1 parent 472c70c commit 5ad6f62
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DB struct {
// single db
db SQLCommon
blockGlobalUpdate bool
logMode int
logMode logModeValue
logger logger
search *search
values sync.Map
Expand All @@ -31,6 +31,14 @@ type DB struct {
singularTable bool
}

type logModeValue int

const (
defaultLogMode logModeValue = iota
noLogMode
detailedLogMode
)

// Open initialize a new db connection, need to import driver first, e.g:
//
// import _ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -141,9 +149,9 @@ func (s *DB) SetLogger(log logger) {
// LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs
func (s *DB) LogMode(enable bool) *DB {
if enable {
s.logMode = 2
s.logMode = detailedLogMode
} else {
s.logMode = 1
s.logMode = noLogMode
}
return s
}
Expand Down Expand Up @@ -716,7 +724,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
func (s *DB) AddError(err error) error {
if err != nil {
if err != ErrRecordNotFound {
if s.logMode == 0 {
if s.logMode == defaultLogMode {
go s.print(fileWithLineNum(), err)
} else {
s.log(err)
Expand Down Expand Up @@ -780,13 +788,13 @@ func (s *DB) print(v ...interface{}) {
}

func (s *DB) log(v ...interface{}) {
if s != nil && s.logMode == 2 {
if s != nil && s.logMode == detailedLogMode {
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
}
}

func (s *DB) slog(sql string, t time.Time, vars ...interface{}) {
if s.logMode == 2 {
if s.logMode == detailedLogMode {
s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, s.RowsAffected)
}
}

0 comments on commit 5ad6f62

Please sign in to comment.