diff --git a/README.md b/README.md index 1273a9fc..ef71f645 100644 --- a/README.md +++ b/README.md @@ -821,6 +821,9 @@ db.LogMode(true) // Refer to gorm's default logger for how to format messages: https://github.com/jinzhu/gorm/blob/master/logger.go#files db.SetLogger(log.New(os.Stdout, "\r\n", 0)) +// If you want to use gorm's default log format, then you could just do it like this +db.SetLogger(gorm.Logger{revel.TRACE}) + // Disable logging db.LogMode(false) diff --git a/gorm_test.go b/gorm_test.go index 6a1ec247..609fb230 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -102,8 +102,11 @@ func init() { fmt.Println("testing postgres...") db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable") } + + // db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)}) // db.SetLogger(log.New(os.Stdout, "\r\n", 0)) db.LogMode(false) + 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 3decaf56..789bf088 100644 --- a/logger.go +++ b/logger.go @@ -8,18 +8,18 @@ import ( "time" ) -type Logger interface { +type logger interface { Print(v ...interface{}) } -type defaultLogger struct { +type Logger struct { *log.Logger } //var default_logger = log.New(os.Stdout, "\r\n", 0) -var default_logger = defaultLogger{log.New(os.Stdout, "\r\n", 0)} +var default_logger = Logger{log.New(os.Stdout, "\r\n", 0)} -func (logger defaultLogger) Print(v ...interface{}) { +func (logger Logger) Print(v ...interface{}) { if len(v) > 1 { level := v[0] tim := "\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m" diff --git a/main.go b/main.go index ae00ce9b..b850b015 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ type DB struct { dialect dialect.Dialect tagIdentifier string singularTable bool - logger Logger + logger logger logMode int } @@ -35,7 +35,7 @@ func (s *DB) SetTagIdentifier(str string) { s.parent.tagIdentifier = str } -func (s *DB) SetLogger(l Logger) { +func (s *DB) SetLogger(l logger) { s.parent.logger = l } diff --git a/private.go b/private.go index 4ab3ef8c..c9dd2463 100644 --- a/private.go +++ b/private.go @@ -72,12 +72,12 @@ func (s *DB) hasError() bool { func (s *DB) print(level string, v ...interface{}) { if s.logMode == 2 || level == "debug" { - if _, ok := s.parent.logger.(Logger); !ok { + if _, ok := s.parent.logger.(logger); !ok { fmt.Println("logger haven't been set, using os.Stdout") s.parent.logger = default_logger } args := []interface{}{level} - s.parent.logger.(Logger).Print(append(args, v...)...) + s.parent.logger.(logger).Print(append(args, v...)...) } }