forked from mirror/gorm
Fix format log, close #752
This commit is contained in:
parent
cd766809c6
commit
8b291cc32b
27
logger.go
27
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:]...)
|
||||
|
|
Loading…
Reference in New Issue