From 8b291cc32b3c184f438e8531425af929a8630108 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 4 Mar 2016 08:59:33 +0800 Subject: [PATCH] Fix format log, close #752 --- logger.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/logger.go b/logger.go index fcd0b57c..96c417cc 100644 --- a/logger.go +++ b/logger.go @@ -38,29 +38,40 @@ func (logger Logger) Print(values ...interface{}) { // duration messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0)) // sql - var formatedValues []interface{} + var sql string + var formattedValues []string + for _, value := range values[4].([]interface{}) { indirectValue := reflect.Indirect(reflect.ValueOf(value)) if indirectValue.IsValid() { value = indirectValue.Interface() if t, ok := value.(time.Time); ok { - formatedValues = append(formatedValues, fmt.Sprintf("'%v'", t.Format(time.RFC3339))) + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format(time.RFC3339))) } else if b, ok := value.([]byte); ok { - formatedValues = append(formatedValues, fmt.Sprintf("'%v'", string(b))) + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", string(b))) } else if r, ok := value.(driver.Valuer); ok { if value, err := r.Value(); err == nil && value != nil { - formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value)) + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } else { - formatedValues = append(formatedValues, "NULL") + formattedValues = append(formattedValues, "NULL") } } else { - formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value)) + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } } else { - formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value)) + formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } } - messages = append(messages, fmt.Sprintf(sqlRegexp.ReplaceAllString(values[3].(string), "%v"), formatedValues...)) + + var formattedValuesLength = len(formattedValues) + for index, value := range sqlRegexp.Split(values[3].(string), -1) { + sql += value + if index < formattedValuesLength { + sql += formattedValues[index] + } + } + + messages = append(messages, sql) } else { messages = append(messages, "\033[31;1m") messages = append(messages, values[2:]...)