Refact logger

This commit is contained in:
Jinzhu 2014-01-04 17:36:58 +08:00
parent 66bfcec72e
commit 30e1b07f7e
3 changed files with 48 additions and 53 deletions

View File

@ -105,6 +105,7 @@ func init() {
// db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)}) // db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)})
// db.SetLogger(log.New(os.Stdout, "\r\n", 0)) // db.SetLogger(log.New(os.Stdout, "\r\n", 0))
db.LogMode(true)
db.LogMode(false) db.LogMode(false)
if err != nil { if err != nil {

View File

@ -19,21 +19,26 @@ type Logger struct {
//var defaultLogger = 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)} var defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
// Format log
var sqlRegexp = regexp.MustCompile(`(\$\d+)|\?`)
func (logger Logger) Print(v ...interface{}) { func (logger Logger) Print(v ...interface{}) {
if len(v) > 1 { if len(v) > 1 {
level := v[0] level := v[0]
tim := "\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m" currentTime := "\n\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m"
source := fmt.Sprintf("\033[35m(%v)\033[0m", v[1])
messages := []interface{}{source, currentTime}
if level == "sql" { if level == "sql" {
dur := v[1] // duration
sql := fmt.Sprintf(regexp.MustCompile(`(\$\d+)|\?`).ReplaceAllString(v[2].(string), "'%v'"), v[3].([]interface{})...) messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(v[2].(time.Duration).Nanoseconds()/1e4)/100.0))
dur = fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(dur.(time.Duration).Nanoseconds()/1e4)/100.0) // sql
logger.Println(tim, dur, sql) messages = append(messages, fmt.Sprintf(sqlRegexp.ReplaceAllString(v[3].(string), "'%v'"), v[4].([]interface{})...))
} else { } else {
messages := []interface{}{"\033[31m"} messages = append(messages, "\033[31;1m")
messages = append(messages, v...) messages = append(messages, v[2:]...)
messages = append(messages, "\033[0m") messages = append(messages, "\033[0m")
logger.Println(messages...)
} }
logger.Println(messages...)
} }
} }

View File

@ -23,9 +23,8 @@ func (s *DB) clone() *DB {
} }
func (s *DB) new() *DB { func (s *DB) new() *DB {
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error, search: &search{}} s.search = nil
db.search.db = &db return s.clone()
return &db
} }
func (s *DB) do(data interface{}) *Do { func (s *DB) do(data interface{}) *Do {
@ -35,7 +34,28 @@ func (s *DB) do(data interface{}) *Do {
return &do return &do
} }
func (s *DB) fileWithLineNum() string { func (s *DB) err(err error) error {
if err != nil {
if s.logMode == 0 {
if err != RecordNotFound {
go s.print(fileWithLineNum(), err)
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
return nil
}
}
} else {
s.log(err)
}
s.Error = err
}
return err
}
func (s *DB) hasError() bool {
return s.Error != nil
}
func fileWithLineNum() string {
for i := 5; i < 15; i++ { for i := 5; i < 15; i++ {
_, file, line, ok := runtime.Caller(i) _, file, line, ok := runtime.Caller(i)
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) { if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
@ -45,49 +65,18 @@ func (s *DB) fileWithLineNum() string {
return "" return ""
} }
func (s *DB) err(err error) error { func (s *DB) print(v ...interface{}) {
if err == nil { go s.parent.logger.(logger).Print(v...)
return nil }
func (s *DB) log(v ...interface{}) {
if s.logMode == 2 {
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
} }
if s.logMode == 0 {
if err != RecordNotFound {
go fmt.Println(s.fileWithLineNum(), err)
error_str := err.Error()
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(error_str) {
return nil
}
}
} else {
s.warn(err)
}
s.Error = err
return err
}
func (s *DB) hasError() bool {
return s.Error != nil
}
func (s *DB) print(level string, v ...interface{}) {
if s.logMode == 2 || level == "debug" {
s.parent.logger.(logger).Print(append([]interface{}{level}, v...)...)
}
}
func (s *DB) warn(v ...interface{}) {
go s.print("warn", v...)
}
func (s *DB) info(v ...interface{}) {
go s.print("info", v...)
} }
func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { func (s *DB) slog(sql string, t time.Time, vars ...interface{}) {
go s.print("sql", time.Now().Sub(t), sql, vars) if s.logMode == 2 {
} s.print("sql", fileWithLineNum(), time.Now().Sub(t), sql, vars)
}
func (s *DB) debug(v ...interface{}) {
go s.print("debug", v...)
} }