diff --git a/gorm_test.go b/gorm_test.go index e446d94e..5d2265da 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -86,6 +86,7 @@ func init() { // GRANT ALL ON gorm.* TO 'gorm'@'localhost'; // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") // db, err = Open("sqlite3", "/tmp/gorm.db") + db.LogMode(true) if err != nil { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) diff --git a/logger.go b/logger.go index 05ddcec6..fb344997 100644 --- a/logger.go +++ b/logger.go @@ -1,31 +1,41 @@ package gorm -import "fmt" +import ( + "fmt" + "log" + "os" +) var logger interface{} +var logger_disabled bool 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...)) + if logger_disabled { + return } + + var has_valid_logger bool + if logger, has_valid_logger = logger.(Logger); !has_valid_logger { + fmt.Println("logger haven't been set, using os.Stdout") + logger = log.New(os.Stdout, "", 0) + } + + args := []interface{}{level} + logger.(Logger).Print(append(args, v...)) } func warn(v ...interface{}) { - Print("warn", v...) + go Print("warn", v...) } func info(v ...interface{}) { - Print("info", v...) + go Print("info", v...) } func debug(v ...interface{}) { - Print("debug", v...) + go Print("debug", v...) } diff --git a/main.go b/main.go index f4ea0e40..bbea1265 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,10 @@ func (s *DB) SetLogger(l interface{}) { logger = l } +func (s *DB) LogMode(b bool) { + logger_disabled = !b +} + func (s *DB) SingularTable(result bool) { singularTableName = result }