mirror of https://github.com/go-gorm/gorm.git
Improve Logger
This commit is contained in:
parent
84395b435b
commit
4125526ef6
|
@ -821,6 +821,9 @@ db.LogMode(true)
|
|||
// Refer to gorm's default logger for how to format messages: https://github.com/jinzhu/gorm/blob/master/logger.go#files
|
||||
db.SetLogger(log.New(os.Stdout, "\r\n", 0))
|
||||
|
||||
// If you want to use gorm's default log format, then you could just do it like this
|
||||
db.SetLogger(gorm.Logger{revel.TRACE})
|
||||
|
||||
// Disable logging
|
||||
db.LogMode(false)
|
||||
|
||||
|
|
|
@ -102,8 +102,11 @@ func init() {
|
|||
fmt.Println("testing postgres...")
|
||||
db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||
}
|
||||
|
||||
// db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)})
|
||||
// db.SetLogger(log.New(os.Stdout, "\r\n", 0))
|
||||
db.LogMode(false)
|
||||
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
|
||||
}
|
||||
|
|
|
@ -8,18 +8,18 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
type logger interface {
|
||||
Print(v ...interface{})
|
||||
}
|
||||
|
||||
type defaultLogger struct {
|
||||
type Logger struct {
|
||||
*log.Logger
|
||||
}
|
||||
|
||||
//var default_logger = log.New(os.Stdout, "\r\n", 0)
|
||||
var default_logger = defaultLogger{log.New(os.Stdout, "\r\n", 0)}
|
||||
var default_logger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
||||
|
||||
func (logger defaultLogger) Print(v ...interface{}) {
|
||||
func (logger Logger) Print(v ...interface{}) {
|
||||
if len(v) > 1 {
|
||||
level := v[0]
|
||||
tim := "\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m"
|
||||
|
|
4
main.go
4
main.go
|
@ -15,7 +15,7 @@ type DB struct {
|
|||
dialect dialect.Dialect
|
||||
tagIdentifier string
|
||||
singularTable bool
|
||||
logger Logger
|
||||
logger logger
|
||||
logMode int
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ func (s *DB) SetTagIdentifier(str string) {
|
|||
s.parent.tagIdentifier = str
|
||||
}
|
||||
|
||||
func (s *DB) SetLogger(l Logger) {
|
||||
func (s *DB) SetLogger(l logger) {
|
||||
s.parent.logger = l
|
||||
}
|
||||
|
||||
|
|
|
@ -72,12 +72,12 @@ 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 {
|
||||
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(args, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue