Skip to content

Commit d7296a6

Browse files
committed
log ts time format
1 parent 0733070 commit d7296a6

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎utils/log.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ var levelMap = map[string]zapcore.Level{
6767
"fatal": zapcore.FatalLevel,
6868
}
6969

70+
const logTimeLayout = "2006-01-02 15:04:05"
71+
7072
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
71-
enc.AppendString(t.UTC().Format(time.RFC3339Nano))
73+
enc.AppendString(t.In(time.Local).Format(logTimeLayout))
7274
}
7375

7476
// optional helpers for structured fields (used in some modules)

‎utils/log_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package utils
22

33
import (
4+
"encoding/json"
45
"moto/config"
56
"os"
67
"path/filepath"
78
"strings"
89
"testing"
10+
"time"
911

1012
"go.uber.org/zap"
13+
"go.uber.org/zap/zapcore"
1114
)
1215

1316
func TestLoggerHasSafeDefault(t *testing.T) {
@@ -60,3 +63,34 @@ func TestConfigureAllowsStdoutOnly(t *testing.T) {
6063
}
6164
Logger.Warn("stdout-only")
6265
}
66+
67+
func TestTimeEncoderUsesLocalTimeWithSecondPrecision(t *testing.T) {
68+
previousLocation := time.Local
69+
time.Local = time.FixedZone("UTC+8", 8*60*60)
70+
t.Cleanup(func() { time.Local = previousLocation })
71+
72+
encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
73+
TimeKey: "ts",
74+
MessageKey: "msg",
75+
EncodeTime: TimeEncoder,
76+
})
77+
entry := zapcore.Entry{
78+
Time: time.Date(2026, time.August, 27, 9, 3, 6, 987654321, time.UTC),
79+
Message: "time-format-test",
80+
}
81+
buffer, err := encoder.EncodeEntry(entry, nil)
82+
if err != nil {
83+
t.Fatalf("EncodeEntry() error = %v", err)
84+
}
85+
defer buffer.Free()
86+
87+
var decoded struct {
88+
Timestamp string `json:"ts"`
89+
}
90+
if err := json.Unmarshal(buffer.Bytes(), &decoded); err != nil {
91+
t.Fatalf("decode log entry: %v", err)
92+
}
93+
if decoded.Timestamp != "2026-08-27 17:03:06" {
94+
t.Fatalf("timestamp = %q, want %q", decoded.Timestamp, "2026-08-27 17:03:06")
95+
}
96+
}

0 commit comments

Comments
 (0)