From 4b68a1d055f48981529dd7d4bbb2eb31382cdd81 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Fri, 12 Apr 2024 17:13:57 +0800 Subject: [PATCH] fix: fix ci and test --- .github/workflows/ci.yml | 8 +- .github/workflows/pr_review_dog.yml | 6 +- go.mod | 2 +- go.sum | 5 ++ log_test.go | 120 ++++++++++++++++++---------- 5 files changed, 91 insertions(+), 50 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b1ff44..39c15d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,10 +3,10 @@ name: Tests on: push: paths-ignore: - - '**.md' + - "**.md" pull_request: paths-ignore: - - '**.md' + - "**.md" env: GO111MODULE: on @@ -18,7 +18,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go-version: ["1.18.x", "1.19.x"] + go-version: ["1.21.x", "1.22.x"] os: [ubuntu-latest, macos-latest, windows-latest] steps: @@ -62,4 +62,4 @@ jobs: with: files: ./coverage flags: unittests - verbose: true \ No newline at end of file + verbose: true diff --git a/.github/workflows/pr_review_dog.yml b/.github/workflows/pr_review_dog.yml index dd134a3..19e1903 100644 --- a/.github/workflows/pr_review_dog.yml +++ b/.github/workflows/pr_review_dog.yml @@ -3,7 +3,7 @@ name: Reviewdog on: pull_request: paths-ignore: - - '**.md' + - "**.md" jobs: golangci-lint: @@ -14,7 +14,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.18.x' + go-version: "1.22.x" - name: Check out code into the Go module directory uses: actions/checkout@v3.5.3 @@ -22,4 +22,4 @@ jobs: - name: golangci-lint uses: reviewdog/action-golangci-lint@v2 with: - golangci_lint_flags: "-E goimports -E gocritic -E misspell" \ No newline at end of file + golangci_lint_flags: "-E goimports -E gocritic -E misspell" diff --git a/go.mod b/go.mod index 13bc782..99737f1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/things-go/log -go 1.18 +go 1.21 require ( github.com/natefinch/lumberjack v2.0.0+incompatible diff --git a/go.sum b/go.sum index 599b06e..335b1c6 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,15 @@ github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= 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/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM= github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= @@ -16,3 +20,4 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log_test.go b/log_test.go index f5552e0..755bbe9 100644 --- a/log_test.go +++ b/log_test.go @@ -1,68 +1,104 @@ -package log +package log_test import ( "context" "testing" - "go.uber.org/zap" + "github.com/things-go/log" ) -func TestNew(t *testing.T) { - l, lv := New(WithConfig(Config{Level: "debug", Format: "json"})) - ReplaceGlobals(NewLoggerWith(l, lv)) - SetDefaultValuer(Caller(3), func(ctx context.Context) Field { return zap.String("field_fn_key1", "field_fn_value1") }) +func init() { + l, lv := log.New(log.WithConfig(log.Config{Level: "debug", Format: "json"})) + log.ReplaceGlobals(log.NewLoggerWith(l, lv)) + log.SetDefaultValuer( + log.Caller(3), + func(ctx context.Context) log.Field { + return log.String("deft_key1", "deft_val1") + }, + ) +} - Debug("Debug") - Info("Info") - Warn("Warn") - Info("info") - Error("Error") - DPanic("DPanic") +func Test_LoggerNormal(t *testing.T) { + log.Debug("Debug") + log.Info("Info") + log.Warn("Warn") + log.Info("info") + log.Error("Error") + log.DPanic("DPanic") +} - Debugf("Debugf: %s", "debug") - Infof("Infof: %s", "info") - Warnf("Warnf: %s", "warn") - Infof("Infof: %s", "info") - Errorf("Errorf: %s", "error") - DPanicf("DPanicf: %s", "dPanic") +func Test_LoggerFormater(t *testing.T) { + log.Debugf("Debugf: %s", "debug") + log.Infof("Infof: %s", "info") + log.Warnf("Warnf: %s", "warn") + log.Infof("Infof: %s", "info") + log.Errorf("Errorf: %s", "error") + log.DPanicf("DPanicf: %s", "dPanic") +} - Debugw("Debugw", "Debugw", "w") - Infow("Infow", "Infow", "w") - Warnw("Warnw", "Warnw", "w") - Infow("Infow", "Infow", "w") - Errorw("Errorw", "Errorw", "w") - DPanicw("DPanicw", "DPanicw", "w") +func Test_LoggerKeyValue(t *testing.T) { + log.Debugw("Debugw", "Debugw", "w") + log.Infow("Infow", "Infow", "w") + log.Warnw("Warnw", "Warnw", "w") + log.Infow("Infow", "Infow", "w") + log.Errorw("Errorw", "Errorw", "w") + log.DPanicw("DPanicw", "DPanicw", "w") +} - shouPanic(t, func() { - Panic("Panic") +func TestPanic(t *testing.T) { + shouldPanic(t, func() { + log.Panic("Panic") }) - shouPanic(t, func() { - Panicf("Panicf: %s", "panic") + shouldPanic(t, func() { + log.Panicf("Panicf: %s", "panic") }) - shouPanic(t, func() { - Panicw("Panicw: %s", "panic", "w") + shouldPanic(t, func() { + log.Panicw("Panicw: %s", "panic", "w") }) +} + +func Test_LoggerWith(t *testing.T) { + log.With( + log.String("string", "bb"), + log.Int16("int16", 100), + ). + Debug("debug with") +} - With(zap.String("aa", "bb")).Debug("debug with") +func Test_LoggerNamed(t *testing.T) { + log.Named("another").Debug("debug named") +} +func Test_Logger_ZapLogger(t *testing.T) { + log.Logger().Debug("desugar") +} - Named("another").Debug("debug named") +func Test_LoggerNamespace(t *testing.T) { + log.Logger().With(log.Namespace("aaaa")).With(log.String("xx", "yy"), log.String("aa", "bb")).Debug("with namespace") + + _ = log.Sync() +} - Logger().Debug("desugar") +type ctxKey struct{} - WithContext(context.Background()). - WithValuer(func(ctx context.Context) Field { return zap.String("field_fn_key2", "field_fn_value2") }). +func Test_Logger_WithContext(t *testing.T) { + ctx := context.WithValue(context.Background(), ctxKey{}, "ctx_value") + ctxValuer := func(ctx context.Context) log.Field { + s, ok := ctx.Value(ctxKey{}).(string) + if !ok { + return log.Skip() + } + return log.String("ctx_key", s) + } + log.WithContext(ctx). + WithValuer(ctxValuer). Debug("with context") - WithContext(context.Background()). - WithValuer(func(ctx context.Context) Field { return zap.String("field_fn_key3", "field_fn_value3") }). + log.WithContext(ctx). + WithValuer(ctxValuer). Debug("with field fn") - - Logger().With(zap.Namespace("aaaa")).With(zap.String("xx", "yy")).Debug("----<>") - - _ = Sync() } -func shouPanic(t *testing.T, f func()) { +func shouldPanic(t *testing.T, f func()) { defer func() { e := recover() if e == nil {