forked from mirror/gorm
Support Logger
This commit is contained in:
parent
9e762a9a30
commit
9bc11d6637
|
@ -86,6 +86,7 @@ func init() {
|
||||||
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
|
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
|
||||||
// db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
|
// db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
|
||||||
// db, err = Open("sqlite3", "/tmp/gorm.db")
|
// db, err = Open("sqlite3", "/tmp/gorm.db")
|
||||||
|
db.LogMode(true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
|
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
|
||||||
|
|
30
logger.go
30
logger.go
|
@ -1,31 +1,41 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
var logger interface{}
|
var logger interface{}
|
||||||
|
var logger_disabled bool
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Print(v ...interface{})
|
Print(v ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Print(level string, v ...interface{}) {
|
func Print(level string, v ...interface{}) {
|
||||||
args := []interface{}{level}
|
if logger_disabled {
|
||||||
|
return
|
||||||
if l, ok := logger.(Logger); ok {
|
|
||||||
l.Print(append(args, v...))
|
|
||||||
} else {
|
|
||||||
fmt.Println("logger haven't been set,", append(args, v...))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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{}) {
|
func warn(v ...interface{}) {
|
||||||
Print("warn", v...)
|
go Print("warn", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func info(v ...interface{}) {
|
func info(v ...interface{}) {
|
||||||
Print("info", v...)
|
go Print("info", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func debug(v ...interface{}) {
|
func debug(v ...interface{}) {
|
||||||
Print("debug", v...)
|
go Print("debug", v...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue