Try to add logger

This commit is contained in:
Jinzhu 2013-11-11 09:05:14 +08:00
parent 874856a592
commit 9e762a9a30
5 changed files with 39 additions and 7 deletions

View File

@ -668,9 +668,10 @@ db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).Fi
``` ```
## TODO ## TODO
* Transaction
* Logger
* Join, Having, Group
* Index, Unique, Valiations * Index, Unique, Valiations
* SQL Log
* SQL Query with goroutines
# Author # Author

View File

@ -32,6 +32,7 @@ func (s *Chain) err(err error) error {
if err != nil { if err != nil {
s.Errors = append(s.Errors, err) s.Errors = append(s.Errors, err)
s.Error = err s.Error = err
warn(err)
} }
return err return err
} }

31
logger.go Normal file
View File

@ -0,0 +1,31 @@
package gorm
import "fmt"
var logger interface{}
type Logger interface {
Print(v ...interface{})
}
func Print(level string, v ...interface{}) {
args := []interface{}{level}
if l, ok := logger.(Logger); ok {
l.Print(append(args, v...))
} else {
fmt.Println("logger haven't been set,", append(args, v...))
}
}
func warn(v ...interface{}) {
Print("warn", v...)
}
func info(v ...interface{}) {
Print("info", v...)
}
func debug(v ...interface{}) {
Print("debug", v...)
}

View File

@ -19,6 +19,10 @@ func (s *DB) SetPool(n int) {
s.db.SetMaxIdleConns(n) s.db.SetMaxIdleConns(n)
} }
func (s *DB) SetLogger(l interface{}) {
logger = l
}
func (s *DB) SingularTable(result bool) { func (s *DB) SingularTable(result bool) {
singularTableName = result singularTableName = result
} }

View File

@ -63,8 +63,3 @@ func getInterfaceAsString(value interface{}) (str string, err error) {
} }
return return
} }
func debug(value interface{}) {
fmt.Printf("***************\n")
fmt.Printf("%+v\n\n", value)
}