From ba01300653050fe8673888f49b2521014ad25346 Mon Sep 17 00:00:00 2001 From: wangyuehong Date: Sun, 6 Aug 2023 00:01:22 +0900 Subject: [PATCH] customize time format in sql log --- logger/sql.go | 14 +++++++++----- logger/sql_test.go | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/logger/sql.go b/logger/sql.go index 1521c1fd..099e1353 100644 --- a/logger/sql.go +++ b/logger/sql.go @@ -13,10 +13,14 @@ import ( "gorm.io/gorm/utils" ) +var ( + // TimeParamFormat defines the format of the time parameter in ExpalianSQL. + TimeParamFormat = "2006-01-02 15:04:05.999" +) + const ( - tmFmtWithMS = "2006-01-02 15:04:05.999" - tmFmtZero = "0000-00-00 00:00:00" - nullStr = "NULL" + tmFmtZero = "0000-00-00 00:00:00" + nullStr = "NULL" ) func isPrintable(s string) bool { @@ -47,14 +51,14 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a if v.IsZero() { vars[idx] = escaper + tmFmtZero + escaper } else { - vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper + vars[idx] = escaper + v.Format(TimeParamFormat) + escaper } case *time.Time: if v != nil { if v.IsZero() { vars[idx] = escaper + tmFmtZero + escaper } else { - vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper + vars[idx] = escaper + v.Format(TimeParamFormat) + escaper } } else { vars[idx] = nullStr diff --git a/logger/sql_test.go b/logger/sql_test.go index e4a72748..b3a86696 100644 --- a/logger/sql_test.go +++ b/logger/sql_test.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "testing" + "time" "github.com/jinzhu/now" "gorm.io/gorm/logger" @@ -101,7 +102,6 @@ func TestExplainSQL(t *testing.T) { Vars: []interface{}{"jinzhu", 1, 0.1753607109, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es}, Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 0.1753607109, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)), }, - } for idx, r := range results { @@ -109,4 +109,20 @@ func TestExplainSQL(t *testing.T) { t.Errorf("Explain SQL #%v expects %v, but got %v", idx, r.Result, result) } } + + t.Run("customize time format", func(t *testing.T) { + orignalFormat := logger.TimeParamFormat + t.Cleanup(func() { logger.TimeParamFormat = orignalFormat }) + + logger.TimeParamFormat = time.RFC3339Nano + + tnano := now.MustParse("2020-02-23T11:10:10.123456789+08:00") + var zt time.Time + sql := "create table users (name, create_at, update_at, delete_at, init_at) values (?, ?, ?, ?, ?)" + vars := []interface{}{"jinzhu", tnano, &tnano, zt, &zt} + expected := `create table users (name, create_at, update_at, delete_at, init_at) values ("jinzhu", "2020-02-23T11:10:10.123456789+08:00", "2020-02-23T11:10:10.123456789+08:00", "0000-00-00 00:00:00", "0000-00-00 00:00:00")` + if result := logger.ExplainSQL(sql, nil, `"`, vars...); result != expected { + t.Errorf("Explain SQL expects %v, but got %v", expected, result) + } + }) }