Skip to content

Commit

Permalink
feat(log): 馃寛 tinted console logs (#851)
Browse files Browse the repository at this point in the history
# Description

Support tinted (colorized) logs, If the user has not set the `YOMO_LOG_FORMAT` environment variable, the logger should be tinted (colorized) by default.
  • Loading branch information
woorui committed Jul 3, 2024
1 parent 292a148 commit 8e08836
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/ylog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/ylog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions core/ylog/slog_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 8e08836

Please sign in to comment.