gorm/logger.go

37 lines
792 B
Go
Raw Normal View History

2013-11-11 05:05:14 +04:00
package gorm
2013-11-11 07:11:49 +04:00
import (
"fmt"
"log"
"os"
2013-11-11 07:53:56 +04:00
"regexp"
2013-11-11 11:48:31 +04:00
"time"
2013-11-11 07:11:49 +04:00
)
2013-11-11 05:05:14 +04:00
type Logger interface {
Print(v ...interface{})
}
2013-11-11 11:48:31 +04:00
func (s *Chain) print(level string, v ...interface{}) {
if s.d.log_mode || s.debug_mode || level == "debug" {
if _, ok := s.d.logger.(Logger); !ok {
fmt.Println("logger haven't been set, using os.Stdout")
s.d.logger = log.New(os.Stdout, "", 0)
}
args := []interface{}{level}
s.d.logger.(Logger).Print(append(args, v...))
2013-11-11 05:05:14 +04:00
}
}
2013-11-11 11:48:31 +04:00
func (s *Chain) warn(v ...interface{}) {
go s.print("warn", v...)
2013-11-11 07:53:56 +04:00
}
2013-11-11 11:48:31 +04:00
func (s *Chain) slog(sql string, t time.Time, vars ...interface{}) {
go s.print("sql", time.Now().Sub(t), fmt.Sprintf(regexp.MustCompile(`\$\d|\?`).ReplaceAllString(sql, "'%v'"), vars...))
2013-11-11 05:05:14 +04:00
}
2013-11-11 11:48:31 +04:00
func (s *Chain) debug(v ...interface{}) {
go s.print("debug", v...)
2013-11-11 05:05:14 +04:00
}