diff --git a/core/ylog/logger.go b/core/ylog/logger.go index e44bb997a..c4805bd11 100644 --- a/core/ylog/logger.go +++ b/core/ylog/logger.go @@ -64,7 +64,7 @@ type Config struct { // Format supports text and json, // The default is text. - Format string `env:"YOMO_LOG_FORMAT" envDefault:"text"` + Format string `env:"YOMO_LOG_FORMAT" envDefault:""` // MaxSize is the maximum size in megabytes of the log file before it gets rotated. // It defaults to 100 megabytes. diff --git a/core/ylog/logger_test.go b/core/ylog/logger_test.go index 6b43fb3fc..605a01b57 100644 --- a/core/ylog/logger_test.go +++ b/core/ylog/logger_test.go @@ -37,13 +37,13 @@ func TestLogger(t *testing.T) { assert.NoError(t, err) assert.FileExists(t, output) - assert.Equal(t, "level=INFO msg=\"some info\" hello=yomo\nlevel=WARN msg=\"some waring\" hello=yomo\n", string(log)) + assert.Equal(t, "\x1b[92mINF\x1b[0m some info \x1b[2mhello=\x1b[0myomo\n\x1b[93mWRN\x1b[0m some waring \x1b[2mhello=\x1b[0myomo\n", string(log)) errlog, err := os.ReadFile(errOutput) assert.NoError(t, err) assert.FileExists(t, errOutput) - assert.Equal(t, "level=ERROR msg=error err=EOF hello=yomo\n", string(errlog)) + assert.Equal(t, "\x1b[91mERR\x1b[0m error \x1b[2merr=\x1b[0mEOF \x1b[2mhello=\x1b[0myomo\n", string(errlog)) os.Remove(output) os.Remove(errOutput) diff --git a/core/ylog/slog_handler.go b/core/ylog/slog_handler.go index 193fbf80f..f8cf3e653 100644 --- a/core/ylog/slog_handler.go +++ b/core/ylog/slog_handler.go @@ -9,6 +9,9 @@ import ( "os" "strings" "sync" + "time" + + "github.com/lmittmann/tint" ) // handler supports splitting log stream to common log stream and error log stream. @@ -126,10 +129,19 @@ func bufferedSlogHandler(buf io.Writer, format string, level slog.Level, verbose } var h slog.Handler - if strings.ToLower(format) == "json" { + format = strings.ToLower(format) + switch format { + case "json": h = slog.NewJSONHandler(buf, opt) - } else { + case "text": h = slog.NewTextHandler(buf, opt) + default: + h = tint.NewHandler(buf, &tint.Options{ + AddSource: verbose, + Level: level, + ReplaceAttr: opt.ReplaceAttr, + TimeFormat: time.Kitchen, + }) } return h diff --git a/go.mod b/go.mod index 6628d3f95..56e85714e 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/invopop/jsonschema v0.12.0 github.com/joho/godotenv v1.5.1 + github.com/lmittmann/tint v1.0.4 github.com/matoous/go-nanoid/v2 v2.1.0 github.com/quic-go/quic-go v0.44.0 github.com/reactivex/rxgo/v2 v2.5.0 diff --git a/go.sum b/go.sum index 872d6927c..752aacf8f 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,8 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc= +github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= github.com/lufia/plan9stats v0.0.0-20240513124658-fba389f38bae h1:dIZY4ULFcto4tAFlj1FYZl8ztUZ13bdq+PLY+NOfbyI= github.com/lufia/plan9stats v0.0.0-20240513124658-fba389f38bae/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=