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
|
|
|
|
2014-01-03 15:23:41 +04:00
|
|
|
type logger interface {
|
2013-11-11 05:05:14 +04:00
|
|
|
Print(v ...interface{})
|
|
|
|
}
|
|
|
|
|
2014-01-03 15:23:41 +04:00
|
|
|
type Logger struct {
|
2013-11-11 13:16:15 +04:00
|
|
|
*log.Logger
|
|
|
|
}
|
|
|
|
|
2014-01-04 11:32:44 +04:00
|
|
|
var defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
2013-11-19 04:50:07 +04:00
|
|
|
|
2014-01-04 13:36:58 +04:00
|
|
|
// Format log
|
|
|
|
var sqlRegexp = regexp.MustCompile(`(\$\d+)|\?`)
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func (logger Logger) Print(values ...interface{}) {
|
|
|
|
if len(values) > 1 {
|
|
|
|
level := values[0]
|
2014-08-23 12:00:04 +04:00
|
|
|
currentTime := "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m"
|
2015-02-17 17:55:14 +03:00
|
|
|
source := fmt.Sprintf("\033[35m(%v)\033[0m", values[1])
|
2014-01-04 13:36:58 +04:00
|
|
|
messages := []interface{}{source, currentTime}
|
2013-11-11 13:16:15 +04:00
|
|
|
|
|
|
|
if level == "sql" {
|
2014-01-04 13:36:58 +04:00
|
|
|
// duration
|
2015-02-17 17:55:14 +03:00
|
|
|
messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
|
2014-01-04 13:36:58 +04:00
|
|
|
// sql
|
2015-02-17 17:55:14 +03:00
|
|
|
messages = append(messages, fmt.Sprintf(sqlRegexp.ReplaceAllString(values[3].(string), "'%v'"), values[4].([]interface{})...))
|
2013-11-11 13:16:15 +04:00
|
|
|
} else {
|
2014-01-04 13:36:58 +04:00
|
|
|
messages = append(messages, "\033[31;1m")
|
2015-02-17 17:55:14 +03:00
|
|
|
messages = append(messages, values[2:]...)
|
2013-11-11 13:16:15 +04:00
|
|
|
messages = append(messages, "\033[0m")
|
|
|
|
}
|
2014-01-04 13:36:58 +04:00
|
|
|
logger.Println(messages...)
|
2013-11-11 13:16:15 +04:00
|
|
|
}
|
|
|
|
}
|