Fix format log, close #752

This commit is contained in:
Jinzhu 2016-03-04 08:59:33 +08:00
parent cd766809c6
commit 8b291cc32b
1 changed files with 19 additions and 8 deletions

View File

@ -38,29 +38,40 @@ func (logger Logger) Print(values ...interface{}) {
// 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 formatedValues []interface{} 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 {
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 { } 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 { } else if r, ok := value.(driver.Valuer); ok {
if value, err := r.Value(); err == nil && value != nil { if value, err := r.Value(); err == nil && value != nil {
formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value)) formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
} else { } else {
formatedValues = append(formatedValues, "NULL") formattedValues = append(formattedValues, "NULL")
} }
} else { } else {
formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value)) formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
} }
} else { } 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 { } else {
messages = append(messages, "\033[31;1m") messages = append(messages, "\033[31;1m")
messages = append(messages, values[2:]...) messages = append(messages, values[2:]...)