Support Logger

This commit is contained in:
Jinzhu 2013-11-11 11:11:49 +08:00
parent 9e762a9a30
commit 9bc11d6637
3 changed files with 25 additions and 10 deletions

View File

@ -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))

View File

@ -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...)
} }

View File

@ -23,6 +23,10 @@ func (s *DB) SetLogger(l interface{}) {
logger = l logger = l
} }
func (s *DB) LogMode(b bool) {
logger_disabled = !b
}
func (s *DB) SingularTable(result bool) { func (s *DB) SingularTable(result bool) {
singularTableName = result singularTableName = result
} }