Initalize default logger when initalize DB

This commit is contained in:
Jinzhu 2014-01-04 15:32:44 +08:00
parent ccae2f0345
commit 66bfcec72e
3 changed files with 4 additions and 8 deletions

View File

@ -16,8 +16,8 @@ type Logger struct {
*log.Logger
}
//var default_logger = log.New(os.Stdout, "\r\n", 0)
var default_logger = Logger{log.New(os.Stdout, "\r\n", 0)}
//var defaultLogger = log.New(os.Stdout, "\r\n", 0)
var defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
func (logger Logger) Print(v ...interface{}) {
if len(v) > 1 {

View File

@ -23,6 +23,7 @@ func Open(driver, source string) (db DB, err error) {
db.db, err = sql.Open(driver, source)
db.dialect = dialect.New(driver)
db.tagIdentifier = "sql"
db.logger = defaultLogger
db.parent = &db
return
}

View File

@ -72,12 +72,7 @@ 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 {
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([]interface{}{level}, v...)...)
}
}