gorm/logger.go

47 lines
847 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 07:11:49 +04:00
)
2013-11-11 05:05:14 +04:00
var logger interface{}
2013-11-11 07:11:49 +04:00
var logger_disabled bool
2013-11-11 05:05:14 +04:00
type Logger interface {
Print(v ...interface{})
}
2013-11-11 07:53:56 +04:00
func print(level string, v ...interface{}) {
2013-11-11 09:16:08 +04:00
if logger_disabled && level != "debug" {
2013-11-11 07:11:49 +04:00
return
}
2013-11-11 05:05:14 +04:00
2013-11-11 07:11:49 +04:00
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)
2013-11-11 05:05:14 +04:00
}
2013-11-11 07:11:49 +04:00
args := []interface{}{level}
logger.(Logger).Print(append(args, v...))
2013-11-11 05:05:14 +04:00
}
func warn(v ...interface{}) {
2013-11-11 07:53:56 +04:00
go print("warn", v...)
2013-11-11 05:05:14 +04:00
}
func info(v ...interface{}) {
2013-11-11 07:53:56 +04:00
go print("info", v...)
}
func slog(sql string, vars ...interface{}) {
go print("sql", fmt.Sprintf(regexp.MustCompile(`\$\d|\?`).ReplaceAllString(sql, "'%v'"), vars...))
2013-11-11 05:05:14 +04:00
}
func debug(v ...interface{}) {
2013-11-11 07:53:56 +04:00
go print("debug", v...)
2013-11-11 05:05:14 +04:00
}