mirror of https://github.com/go-gorm/gorm.git
Refactor Logger
This commit is contained in:
parent
2c025b25fe
commit
97949fdbc1
73
logger.go
73
logger.go
|
@ -17,41 +17,38 @@ var (
|
||||||
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
||||||
)
|
)
|
||||||
|
|
||||||
type logger interface {
|
func isPrintable(s string) bool {
|
||||||
Print(v ...interface{})
|
for _, r := range s {
|
||||||
|
if !unicode.IsPrint(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogWriter log writer interface
|
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
|
||||||
type LogWriter interface {
|
|
||||||
Println(v ...interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logger default logger
|
|
||||||
type Logger struct {
|
|
||||||
LogWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print format & print log
|
|
||||||
func (logger Logger) Print(values ...interface{}) {
|
|
||||||
if len(values) > 1 {
|
if len(values) > 1 {
|
||||||
level := values[0]
|
var (
|
||||||
currentTime := "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m"
|
sql string
|
||||||
source := fmt.Sprintf("\033[35m(%v)\033[0m", values[1])
|
formattedValues []string
|
||||||
messages := []interface{}{source, currentTime}
|
level = values[0]
|
||||||
|
currentTime = "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m"
|
||||||
|
source = fmt.Sprintf("\033[35m(%v)\033[0m", values[1])
|
||||||
|
)
|
||||||
|
|
||||||
|
messages = []interface{}{source, currentTime}
|
||||||
|
|
||||||
if level == "sql" {
|
if level == "sql" {
|
||||||
// duration
|
// duration
|
||||||
messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
|
messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
|
||||||
// sql
|
// sql
|
||||||
var sql string
|
|
||||||
var formattedValues []string
|
|
||||||
|
|
||||||
for _, value := range values[4].([]interface{}) {
|
for _, value := range values[4].([]interface{}) {
|
||||||
indirectValue := reflect.Indirect(reflect.ValueOf(value))
|
indirectValue := reflect.Indirect(reflect.ValueOf(value))
|
||||||
if indirectValue.IsValid() {
|
if indirectValue.IsValid() {
|
||||||
value = indirectValue.Interface()
|
value = indirectValue.Interface()
|
||||||
if t, ok := value.(time.Time); ok {
|
if t, ok := value.(time.Time); ok {
|
||||||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format(time.RFC3339)))
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
|
||||||
} else if b, ok := value.([]byte); ok {
|
} else if b, ok := value.([]byte); ok {
|
||||||
if str := string(b); isPrintable(str) {
|
if str := string(b); isPrintable(str) {
|
||||||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
|
||||||
|
@ -68,7 +65,7 @@ func (logger Logger) Print(values ...interface{}) {
|
||||||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
|
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
|
formattedValues = append(formattedValues, "NULL")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +74,10 @@ func (logger Logger) Print(values ...interface{}) {
|
||||||
sql = values[3].(string)
|
sql = values[3].(string)
|
||||||
for index, value := range formattedValues {
|
for index, value := range formattedValues {
|
||||||
placeholder := fmt.Sprintf(`\$%d`, index+1)
|
placeholder := fmt.Sprintf(`\$%d`, index+1)
|
||||||
subre := regexp.MustCompile(placeholder)
|
sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value)
|
||||||
sql = subre.ReplaceAllString(sql, value)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var formattedValuesLength = len(formattedValues)
|
formattedValuesLength := len(formattedValues)
|
||||||
for index, value := range sqlRegexp.Split(values[3].(string), -1) {
|
for index, value := range sqlRegexp.Split(values[3].(string), -1) {
|
||||||
sql += value
|
sql += value
|
||||||
if index < formattedValuesLength {
|
if index < formattedValuesLength {
|
||||||
|
@ -96,15 +92,26 @@ func (logger Logger) Print(values ...interface{}) {
|
||||||
messages = append(messages, values[2:]...)
|
messages = append(messages, values[2:]...)
|
||||||
messages = append(messages, "\033[0m")
|
messages = append(messages, "\033[0m")
|
||||||
}
|
}
|
||||||
logger.Println(messages...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPrintable(s string) bool {
|
type logger interface {
|
||||||
for _, r := range s {
|
Print(v ...interface{})
|
||||||
if !unicode.IsPrint(r) {
|
}
|
||||||
return false
|
|
||||||
}
|
// LogWriter log writer interface
|
||||||
}
|
type LogWriter interface {
|
||||||
return true
|
Println(v ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger default logger
|
||||||
|
type Logger struct {
|
||||||
|
LogWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print format & print log
|
||||||
|
func (logger Logger) Print(values ...interface{}) {
|
||||||
|
logger.Println(LogFormatter(values...)...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue