From 9e762a9a301b2cf65af6c16339316d28f6c71a11 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 11 Nov 2013 09:05:14 +0800 Subject: [PATCH] Try to add logger --- README.md | 5 +++-- chain.go | 1 + logger.go | 31 +++++++++++++++++++++++++++++++ main.go | 4 ++++ utils.go | 5 ----- 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 logger.go diff --git a/README.md b/README.md index ed53f5af..4496f152 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/chain.go b/chain.go index 01064467..620f9d40 100644 --- a/chain.go +++ b/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 } diff --git a/logger.go b/logger.go new file mode 100644 index 00000000..05ddcec6 --- /dev/null +++ b/logger.go @@ -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...) +} diff --git a/main.go b/main.go index 1f4ff90f..f4ea0e40 100644 --- a/main.go +++ b/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 } diff --git a/utils.go b/utils.go index b4b7e4c2..7627e459 100644 --- a/utils.go +++ b/utils.go @@ -63,8 +63,3 @@ func getInterfaceAsString(value interface{}) (str string, err error) { } return } - -func debug(value interface{}) { - fmt.Printf("***************\n") - fmt.Printf("%+v\n\n", value) -}