Skip to content

Commit

Permalink
[patch] goccy/go-json (#76)
Browse files Browse the repository at this point in the history
* try to use goccy/go-json

Signed-off-by: kpango <[email protected]>

* add comment for JSONFormat

Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango authored Feb 13, 2021
1 parent be39555 commit c262dbb
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 32 deletions.
Empty file modified .circleci/config.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .whitesource
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified Makefile
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
18 changes: 14 additions & 4 deletions example/main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,15 @@ func main() {
glg.Printf("%s : %s", "printf", "formatted")
glg.CustomLog(customTag, "custom logging")
glg.CustomLog(customErrTag, "custom error logging")
glg.Info("kpango's glg support json logging")
glg.Get().EnableJSON()
glg.Warn("kpango's glg", "support", "json", "logging")
glg.Info("hello", struct {
glg.Info("kpango's glg support json logging")
glg.Get().EnableJSON()
err := glg.Warn("kpango's glg", "support", "json", "logging")
if err != nil {
glg.Get().DisableJSON()
glg.Error(err)
glg.Get().EnableJSON()
}
err = glg.Info("hello", struct {
Name string
Age int
Gender string
Expand All @@ -150,6 +155,11 @@ func main() {
Age: 28,
Gender: "male",
}, 2020)
if err != nil {
glg.Get().DisableJSON()
glg.Error(err)
glg.Get().EnableJSON()
}

go func() {
time.Sleep(time.Second * 5)
Expand Down
11 changes: 6 additions & 5 deletions glg.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"time"
"unsafe"

jsoniter "github.com/json-iterator/go"
json "github.com/goccy/go-json"
"github.com/kpango/fastime"
)

Expand All @@ -50,8 +50,9 @@ type Glg struct {
enableJSON bool
}

type jsonFormat struct {
Date time.Time
// JSONFormat is json object structure for logging
type JSONFormat struct {
Date string
Level string
Detail interface{}
}
Expand Down Expand Up @@ -704,8 +705,8 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
} else {
detail = val
}
return jsoniter.NewEncoder(w).Encode(jsonFormat{
Date: fastime.Now(),
return json.NewEncoder(w).Encode(JSONFormat{
Date: string(fastime.FormattedNow()),
Level: level.String(),
Detail: detail,
})
Expand Down
32 changes: 29 additions & 3 deletions glg_bench_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ SOFTWARE.`
)

type JSONMessage struct {
Message string
Number int
Float float64
Message string `json:"message,omitempty"`
Number int `json:"number,omitempty"`
Float float64 `json:"float,omitempty"`
}

type MockWriter struct {
Expand Down Expand Up @@ -192,6 +192,32 @@ func BenchmarkGlgJSON(b *testing.B) {
}

func BenchmarkZapJSON(b *testing.B) {
cfg := zap.NewProductionConfig()
logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(cfg.EncoderConfig), zapcore.AddSync(&MockWriter{}), cfg.Level))
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("", zap.String("message", testJSON.Message),
zap.Int("number", testJSON.Number),
zap.Float64("float", testJSON.Float))
logger.Info("", zap.String("message", testJSON.Message),
zap.Int("number", testJSON.Number),
zap.Float64("float", testJSON.Float))
logger.Info("",zap.String("message", testJSON.Message),
zap.Int("number", testJSON.Number),
zap.Float64("float", testJSON.Float))
logger.Info("", zap.String("message", testJSON.Message),
zap.Int("number", testJSON.Number),
zap.Float64("float", testJSON.Float))
logger.Info("", zap.String("message", testJSON.Message),
zap.Int("number", testJSON.Number),
zap.Float64("float", testJSON.Float))
}
})
}

func BenchmarkZapSugarJSON(b *testing.B) {
cfg := zap.NewProductionConfig()
logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(cfg.EncoderConfig), zapcore.AddSync(&MockWriter{}), cfg.Level)).Sugar()
b.ReportAllocs()
Expand Down
2 changes: 1 addition & 1 deletion glg_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3909,7 +3909,7 @@ func TestGlg_EnableJSON(t *testing.T) {
if err != nil {
t.Error(err)
}
var dec jsonFormat
var dec JSONFormat
err = json.NewDecoder(d).Decode(&dec)
if err != nil {
t.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/kpango/glg

go 1.13
go 1.15

require (
github.com/json-iterator/go v1.1.10
github.com/goccy/go-json v0.4.4
github.com/kpango/fastime v1.0.16
go.uber.org/zap v1.16.0
)
22 changes: 5 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goccy/go-json v0.4.4 h1:j8SeR5WhAtKKA+IAn78Rszv/WgZMhb3RqM3W8uXgjXk=
github.com/goccy/go-json v0.4.4/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kpango/fastime v1.0.16 h1:1prFG/3pTjzcDeCTxt98VB4IvjxcySLs0ldCEhZg0R8=
github.com/kpango/fastime v1.0.16/go.mod h1:lVqUTcXmQnk1wriyvq5DElbRSRDC0XtqbXQRdz0Eo+g=
Expand All @@ -17,10 +14,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -30,18 +23,13 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.3.0 h1:sFPn2GLc3poCkfrpIXGhBD2X0CMIo4Q/zSULXrj/+uc=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.14.0 h1:/pduUoebOeeJzTDFuoMgC6nRkiasr1sBCIEorly7m4o=
go.uber.org/zap v1.14.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
Binary file modified images/bench.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified images/logo.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/sample.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified levelmap.go
100644 → 100755
Empty file.
Empty file modified loggers.go
100644 → 100755
Empty file.
Empty file modified renovate.json
100644 → 100755
Empty file.

0 comments on commit c262dbb

Please sign in to comment.