Skip to content

Commit

Permalink
✅ test: add new example test case for issues#100
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 15, 2023
1 parent 1fac5f7 commit c125af8
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions _example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/syyongx/llog v0.0.0-20200222114215-e8f9f86ac0a3
go.uber.org/zap v1.24.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions _example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
139 changes: 139 additions & 0 deletions _example/issue100/issue100_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"fmt"
"testing"
"time"

"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

type Obj struct {
a int
b int64
c string
d bool
}

var (
str1 = "str1"
str2 = "str222222222222"
int1 = 1
int2 = 2
obj = Obj{1, 2, "3", true}
)

func TestZapSugar(t *testing.T) {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "./zap-sugar.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
w,
zap.InfoLevel,
)
logger := zap.New(core)

sugar := logger.Sugar()
sugar.Info("message is msg")

count := 100000
start := time.Now().UnixNano()
for n := count; n > 0; n-- {
sugar.Info("message is msg")
}
end := time.Now().UnixNano()
fmt.Printf("\n zap sugar no format\n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)

start = time.Now().UnixNano()
for n := count; n > 0; n-- {
sugar.Infof("message is %d %d %s %s %#v", int1, int2, str1, str2, obj)
}
end = time.Now().UnixNano()
fmt.Printf("\n zap sugar format\n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)
sugar.Sync()
}

func TestZapLog(t *testing.T) {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "./zap.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})

core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
w,
zap.InfoLevel,
)
logger := zap.New(core)

count := 100000
start := time.Now().UnixNano()
for n := count; n > 0; n-- {
logger.Info("message is msg")
}
end := time.Now().UnixNano()
fmt.Printf("\n zap no format\n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)

start = time.Now().UnixNano()
for n := count; n > 0; n-- {
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.Int("int1", int1),
zap.Int("int2", int2),
zap.String("str", str1),
zap.String("str2", str2),
zap.Any("backoff", obj),
)
}
end = time.Now().UnixNano()
fmt.Printf("\n zap format\n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)
logger.Sync()
}

func TestSlog(t *testing.T) {
h1, err := handler.NewEmptyConfig().With(
handler.WithLogfile("./slog-info.log"), // 路径
handler.WithRotateTime(handler.EveryHour), // 日志分割间隔
handler.WithLogLevels(slog.AllLevels), // 日志level
handler.WithBuffSize(4*1024*1024), // buffer大小
handler.WithCompress(true), // 是否压缩旧日志 zip
handler.WithBackupNum(24*3), // 保留旧日志数量
handler.WithBuffMode(handler.BuffModeBite),
// handler.WithRenameFunc(), //RenameFunc build filename for rotate file
).CreateHandler()
if err != nil {
fmt.Printf("Create slog handler err: %#v", err)
return
}

f := slog.AsTextFormatter(h1.Formatter())
myTplt := "[{{datetime}}] [{{level}}] [{{caller}}] {{message}}\n"
f.SetTemplate(myTplt)
logs := slog.NewWithHandlers(h1)

count := 100000
start := time.Now().UnixNano()
for i := 0; i < count; i++ {
logs.Info("message is msg")
}
end := time.Now().UnixNano()
fmt.Printf("\n slog no format \n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)

start = time.Now().UnixNano()
for n := count; n > 0; n-- {
logs.Infof("message is %d %d %s %s %#v", int1, int2, str1, str2, obj)
}
end = time.Now().UnixNano()
fmt.Printf("\n slog format \n total cost %d ns\n avg cost %d ns \n count %d \n", end-start, (end-start)/int64(count), count)
logs.MustFlush()
}

0 comments on commit c125af8

Please sign in to comment.