Skip to content

Commit

Permalink
feat: use tint to colorize logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunzhuo committed Sep 22, 2023
1 parent b0055e3 commit 878fee1
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"reflect"
"runtime"
"time"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/yomorun/yomo/pkg/frame-codec/y3codec"
"github.com/yomorun/yomo/pkg/id"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

// Client is the abstraction of a YoMo-Client. a YoMo-Client can be
Expand Down
2 changes: 1 addition & 1 deletion core/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"crypto/tls"
"log/slog"
"time"

"github.com/quic-go/quic-go"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/yomorun/yomo/core/ylog"
pkgtls "github.com/yomorun/yomo/pkg/tls"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

// ClientOption YoMo client options
Expand Down
2 changes: 1 addition & 1 deletion core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package core
import (
"context"
"errors"
"log/slog"
"sync"
"time"

"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/metadata"
"github.com/yomorun/yomo/core/router"
"golang.org/x/exp/slog"
)

var ctxPool sync.Pool
Expand Down
2 changes: 1 addition & 1 deletion core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"os"
"reflect"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/metadata"
"github.com/yomorun/yomo/core/router"
"golang.org/x/exp/slog"

// authentication implements, Currently, only token authentication is implemented
_ "github.com/yomorun/yomo/pkg/auth"
Expand Down
2 changes: 1 addition & 1 deletion core/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package core

import (
"crypto/tls"
"log/slog"
"time"

"github.com/quic-go/quic-go"
"github.com/yomorun/yomo/core/auth"
"github.com/yomorun/yomo/core/ylog"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

// DefalutQuicConfig be used when `quicConfig` is nil.
Expand Down
2 changes: 1 addition & 1 deletion core/ylog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ package ylog
import (
"io"
"log"
"log/slog"
"os"
"strings"

"github.com/caarlos0/env/v6"
"golang.org/x/exp/slog"
"gopkg.in/natefinch/lumberjack.v2"
)

Expand Down
2 changes: 1 addition & 1 deletion core/ylog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package ylog

import (
"io"
"log/slog"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/slog"
)

func TestLogger(t *testing.T) {
Expand Down
32 changes: 16 additions & 16 deletions core/ylog/slog_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"bytes"
"context"
"io"
"log/slog"
"os"
"strings"
"sync"

"golang.org/x/exp/slog"
"github.com/lmittmann/tint"
)

// handler supports splitting log stream to common log stream and error log stream.
Expand Down Expand Up @@ -113,25 +114,24 @@ func (h *handler) WithGroup(name string) slog.Handler {
}

func bufferedSlogHandler(buf io.Writer, format string, level slog.Level, verbose, disableTime bool) slog.Handler {
opt := &slog.HandlerOptions{
AddSource: verbose,
Level: level,
}
if disableTime {
opt.ReplaceAttr = func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
return a
replaceAttr := func(groups []string, a slog.Attr) slog.Attr {
if disableTime && a.Key == "time" && len(groups) == 0 {
return slog.Attr{}
}
return a
}

var h slog.Handler
if strings.ToLower(format) == "json" {
h = slog.NewJSONHandler(buf, opt)
} else {
h = slog.NewTextHandler(buf, opt)
return slog.NewJSONHandler(buf, &slog.HandlerOptions{
AddSource: verbose,
Level: level,
ReplaceAttr: replaceAttr,
})
}

return h
return tint.NewHandler(buf, &tint.Options{
AddSource: verbose,
Level: level,
ReplaceAttr: replaceAttr,
})
}
2 changes: 1 addition & 1 deletion example/0-basic/source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"encoding/json"
"log/slog"
"math/rand"
"os"
"time"

"github.com/yomorun/yomo"
"golang.org/x/exp/slog"
)

// custom logger
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/yomorun/yomo

go 1.20
go 1.21

toolchain go1.21.1

require (
github.com/briandowns/spinner v1.22.0
Expand All @@ -9,6 +11,7 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3
github.com/fatih/color v1.15.0
github.com/joho/godotenv v1.4.0
github.com/lmittmann/tint v1.0.2
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/quic-go/quic-go v0.38.1
github.com/reactivex/rxgo/v2 v2.5.0
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -111,6 +112,7 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand All @@ -123,6 +125,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -160,9 +163,13 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/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.2 h1:9XZ+JvEzjvd3VNVugYqo3j+dl0NRju8k9FquAusJExM=
github.com/lmittmann/tint v1.0.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/matoous/go-nanoid v1.5.0/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
Expand All @@ -178,6 +185,7 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -193,6 +201,7 @@ github.com/reactivex/rxgo/v2 v2.5.0 h1:FhPgHwX9vKdNQB2gq9EPt+EKk9QrrzoeztGbEEnZa
github.com/reactivex/rxgo/v2 v2.5.0/go.mod h1:bs4fVZxcb5ZckLIOeIeVH942yunJLWDABWGbrHAW+qU=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/second-state/WasmEdge-go v0.13.0 h1:lCirXbSeqqvLLI67e330+F65EhkbvtAi7/ib913+sMs=
github.com/second-state/WasmEdge-go v0.13.0/go.mod h1:HyBf9hVj1sRAjklsjc1Yvs9b5RcmthPG9z99dY78TKg=
Expand Down Expand Up @@ -363,6 +372,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -565,6 +575,7 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package yomo

import (
"crypto/tls"
"log/slog"

"github.com/quic-go/quic-go"
"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/frame"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

type (
Expand Down
2 changes: 1 addition & 1 deletion sfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yomo

import (
"context"
"log/slog"

"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/frame"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/yomorun/yomo/pkg/id"
"github.com/yomorun/yomo/pkg/trace"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

// StreamFunction defines serverless streaming functions.
Expand Down
2 changes: 1 addition & 1 deletion source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package yomo

import (
"context"
"log/slog"

"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/metadata"
"github.com/yomorun/yomo/pkg/id"
"github.com/yomorun/yomo/pkg/trace"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
)

// Source is responsible for sending data to yomo.
Expand Down
2 changes: 1 addition & 1 deletion zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package yomo
import (
"context"
"fmt"
"log/slog"

"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/router"
"github.com/yomorun/yomo/pkg/config"
"golang.org/x/exp/slog"
)

// Zipper is the orchestrator of yomo. There are two types of zipper:
Expand Down

0 comments on commit 878fee1

Please sign in to comment.