forked from mirror/gorm
Try to add logger
This commit is contained in:
parent
874856a592
commit
9e762a9a30
|
@ -668,9 +668,10 @@ db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).Fi
|
|||
```
|
||||
|
||||
## TODO
|
||||
* Transaction
|
||||
* Logger
|
||||
* Join, Having, Group
|
||||
* Index, Unique, Valiations
|
||||
* SQL Log
|
||||
* SQL Query with goroutines
|
||||
|
||||
# Author
|
||||
|
||||
|
|
1
chain.go
1
chain.go
|
@ -32,6 +32,7 @@ func (s *Chain) err(err error) error {
|
|||
if err != nil {
|
||||
s.Errors = append(s.Errors, err)
|
||||
s.Error = err
|
||||
warn(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -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...)
|
||||
}
|
4
main.go
4
main.go
|
@ -19,6 +19,10 @@ func (s *DB) SetPool(n int) {
|
|||
s.db.SetMaxIdleConns(n)
|
||||
}
|
||||
|
||||
func (s *DB) SetLogger(l interface{}) {
|
||||
logger = l
|
||||
}
|
||||
|
||||
func (s *DB) SingularTable(result bool) {
|
||||
singularTableName = result
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue