From f1a67f5124323f808a6c8286e811ec44d6bfec27 Mon Sep 17 00:00:00 2001 From: Bruno Carlin Date: Wed, 7 May 2025 17:07:08 +0200 Subject: [PATCH] feat: The default formatter now prints timestamps with microseconds --- CHANGELOG.md | 2 ++ logging.go | 2 +- logging_test.go | 24 +++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 828bfcd..cf8c21b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- The default formatter now prints timestamps with microseconds + ## v0.4.1 (2022-06-03) - Ensure all backends implement the interface `BACKEND`. diff --git a/logging.go b/logging.go index f89bf0f..890ea9a 100644 --- a/logging.go +++ b/logging.go @@ -103,7 +103,7 @@ func GetLogger(name string) *Logger { func defaultFormatter(r *Record) string { return fmt.Sprintf("%s [%-8s] %s: %s\n", - r.Timestamp.Format("2006/01/02 15:04:05"), r.Level.Name(), r.Logger, + r.Timestamp.Format("2006/01/02 15:04:05.999999"), r.Level.Name(), r.Logger, strings.TrimSpace(r.Message)) } diff --git a/logging_test.go b/logging_test.go index ce9c6d2..99cf3e4 100644 --- a/logging_test.go +++ b/logging_test.go @@ -1,6 +1,12 @@ package logging -import "testing" +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) func Test_LevelByName(t *testing.T) { t.Parallel() @@ -16,3 +22,19 @@ func Test_LevelByName(t *testing.T) { } } } + +func Test_defaultFormatter(t *testing.T) { + t.Parallel() + + r := &Record{ + Timestamp: time.Date(2000, 0o1, 0o2, 0o3, 0o4, 0o5, 123456789, time.Local), + Level: Info, + Logger: "my logger", + Message: "my log line", + } + + got := defaultFormatter(r) + want := fmt.Sprintf("2000/01/02 03:04:05.123456 [INFO ] my logger: my log line\n") + + assert.Equal(t, want, got) +}