From 902119d617bdbbeaf20944d11b346e266299a490 Mon Sep 17 00:00:00 2001 From: "huhouhuam@outlook.com" Date: Sat, 10 Feb 2024 21:43:47 +0800 Subject: [PATCH] upgrade golang version to 1.21 --- .github/workflows/ci.yml | 66 +++++++++++++++++++++++++++++++++++++ .golangci.yaml | 16 ++++----- Makefile | 71 ++++++++++++++++++++++++++++++++++++++++ context.go | 1 + distribution/logger.go | 4 ++- encoder.go | 1 + example/go.mod | 2 +- go.mod | 18 +++++----- go.sum | 12 +++---- klog/logger.go | 1 + log.go | 11 +++++-- logrus/hook.go | 1 + logrus/logger.go | 5 +-- options.go | 11 ++++--- options_test.go | 1 + types.go | 1 + 16 files changed, 187 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c4ee170 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,66 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: ci + +on: [push, pull_request] +env: + GO_VERSION: '1.21' + +jobs: + # Check if there is any dirty change for go mod tidy + go-mod: + runs-on: ubuntu-latest + steps: + - name: Setup Go + uses: actions/setup-go@v3 + with: + # https://github.com/actions/setup-go#supported-version-syntax + # ex: + # - 1.18beta1 -> 1.18.0-beta.1 + # - 1.18rc1 -> 1.18.0-rc.1 + go-version: ${{ env.GO_VERSION }} + - name: Checkout code + uses: actions/checkout@v4 + - name: Check go mod + run: | + go mod tidy + git diff --exit-code go.mod + git diff --exit-code go.sum + test: + runs-on: ubuntu-latest + needs: go-mod + steps: + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + - name: Checkout code + uses: actions/checkout@v4 + - name: Run Unit tests. + run: make test + format: + runs-on: ubuntu-latest + needs: test + steps: + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + - name: Checkout code + uses: actions/checkout@v4 + - name: format Go Code + run: make format + lint: + runs-on: ubuntu-latest + needs: format + steps: + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + - name: Checkout code + uses: actions/checkout@v4 + - name: Lint Go Code + run: | + make lint \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml index 24d1c44..239d939 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -229,8 +229,8 @@ linters: disable-all: true enable: - bodyclose - - deadcode - - depguard +# - deadcode +# - depguard - dogsled - dupl - errcheck @@ -241,33 +241,33 @@ linters: - gocyclo - gofmt - goimports - - golint +# - revive - goprintffuncname - gosec - gosimple - govet - ineffassign - - interfacer +# - interfacer - lll - misspell - nakedret - nolintlint - rowserrcheck - - scopelint + - exportloopref - staticcheck - - structcheck +# - structcheck - stylecheck - typecheck - unconvert - unparam - unused - - varcheck +# - varcheck - whitespace - asciicheck - gocognit - godot - godox - - maligned +# - maligned - nestif - prealloc - gomodguard diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b09b0b --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +GO := go +ROOT_PACKAGE := github.com/seacraft/log +ifeq ($(origin ROOT_DIR),undefined) +ROOT_DIR := $(shell pwd) +endif + +# Linux command settings +FIND := find . ! -path './third_party/*' ! -path './vendor/*' +XARGS := xargs --no-run-if-empty + +all: test format lint + +## test: Test the package. +.PHONY: test +test: + @echo "===========> Testing packages" + @$(GO) test $(ROOT_PACKAGE)/... + +.PHONY: goimports.verify +goimports.verify: +ifeq (,$(shell which goimports 2>/dev/null)) + @echo "===========> Installing goimports" + @$(GO) install golang.org/x/tools/cmd/goimports@v0.17.0 +endif + +.PHONY: golines.verify +golines.verify: +ifeq (,$(shell which golines 2>/dev/null)) + @echo "===========> Installing golines" + @$(GO) install github.com/segmentio/golines@v0.12.2 +endif + +## format: Format the package with `gofmt` +.PHONY: format +format: golines.verify goimports.verify + @echo "===========> Formating codes" + @$(FIND) -type f -name '*.go' | $(XARGS) gofmt -s -w + @$(FIND) -type f -name '*.go' | $(XARGS) goimports -w -local $(ROOT_PACKAGE) + @$(FIND) -type f -name '*.go' | $(XARGS) golines -w --max-len=120 --reformat-tags --shorten-comments --ignore-generated . + +.PHONY: lint.verify +lint.verify: +ifeq (,$(shell which golangci-lint 2>/dev/null)) + @echo "===========> Installing golangci lint" + @$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.56.1 +endif + +## lint: Check syntax and styling of go sources. +.PHONY: lint +lint: lint.verify + @echo "===========> Run golangci to lint source codes" + @golangci-lint run $(ROOT_DIR)/... + +.PHONY: updates.verify +updates.verify: +ifeq (,$(shell which go-mod-outdated 2>/dev/null)) + @echo "===========> Installing go-mod-outdated" + @$(GO) install github.com/psampaz/go-mod-outdated@v0.9.0 +endif + +## check-updates: Check outdated dependencies of the go projects. +.PHONY: check-updates +check-updates: updates.verify + @$(GO) list -u -m -json all | go-mod-outdated -update -direct + +## help: Show this help info. +.PHONY: help +help: Makefile + @echo -e "\nUsage: make ...\n\nTargets:" + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' + @echo "$$USAGE_OPTIONS" \ No newline at end of file diff --git a/context.go b/context.go index 9dee62b..536d6f6 100644 --- a/context.go +++ b/context.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package log import ( diff --git a/distribution/logger.go b/distribution/logger.go index 888a3df..2d5f0a4 100644 --- a/distribution/logger.go +++ b/distribution/logger.go @@ -13,15 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + // Package distribution implements a logger which compatible to logrus/std log/prometheus. package distribution import ( "fmt" - logruslogger "github.com/seacraft/log/logrus" "github.com/sirupsen/logrus" "go.uber.org/zap" + + logruslogger "github.com/seacraft/log/logrus" ) // Logger is a logger which compatible to logrus/std log/prometheus. diff --git a/encoder.go b/encoder.go index 5380bb9..ee59eb9 100644 --- a/encoder.go +++ b/encoder.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package log import ( diff --git a/example/go.mod b/example/go.mod index 1da4b36..216dc57 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,6 +1,6 @@ module github.com/seacraft/log/example -go 1.20 +go 1.21 require ( github.com/seacraft/log v1.0.0 diff --git a/go.mod b/go.mod index 8d43fc0..fbc1e68 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,19 @@ module github.com/seacraft/log -go 1.20 +go 1.21 + +require ( + github.com/sirupsen/logrus v1.9.3 + github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.8.4 + go.uber.org/zap v1.26.0 + k8s.io/klog v1.0.0 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sirupsen/logrus v1.9.3 // indirect - github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/objx v0.5.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect - go.uber.org/multierr v1.10.0 // indirect - go.uber.org/zap v1.26.0 // indirect + go.uber.org/multierr v1.11.0 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/klog v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 3362bbf..7bb6637 100644 --- a/go.sum +++ b/go.sum @@ -9,20 +9,18 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= -go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +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.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= diff --git a/klog/logger.go b/klog/logger.go index 5be5f30..ec4e11d 100644 --- a/klog/logger.go +++ b/klog/logger.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + // Package klog init klog logger. klog is used by kubernetes, this can compatible the kubernetes packages. package klog diff --git a/log.go b/log.go index 5211b07..d6f237e 100644 --- a/log.go +++ b/log.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + // Package log is a log package used by TKE team. package log @@ -164,7 +165,10 @@ func handleFields(l *zap.Logger, args []interface{}, additional ...zap.Field) [] keyStr, isString := key.(string) if !isString { // if the key isn't a string, DPanic and stop logging - l.DPanic("non-string key argument passed to logging, ignoring all later arguments", zap.Any("invalid key", key)) + l.DPanic( + "non-string key argument passed to logging, ignoring all later arguments", + zap.Any("invalid key", key), + ) break } @@ -180,8 +184,7 @@ var ( options *Options ) -// nolint: gochecknoinits // need to init a default logger -func init() { +func init() { //nolint: gochecknoinits // need to init a default logger Init(NewOptions()) } @@ -438,10 +441,12 @@ func Fatalw(msg string, keysAndValues ...interface{}) { logger.zapLogger.Sugar().Fatalw(msg, keysAndValues...) } +// GetOptions get Logger options. func GetOptions() *Options { return options } +// GetLogger get Logger instance. func GetLogger() *zapLogger { return logger } diff --git a/logrus/hook.go b/logrus/hook.go index d2fed25..cd09cb2 100644 --- a/logrus/hook.go +++ b/logrus/hook.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package logrus import ( diff --git a/logrus/logger.go b/logrus/logger.go index a70014d..f2a3d29 100644 --- a/logrus/logger.go +++ b/logrus/logger.go @@ -13,11 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + // Package logrus adds a hook to the logrus logger hooks. package logrus import ( - "io/ioutil" + "io" "github.com/sirupsen/logrus" "go.uber.org/zap" @@ -26,7 +27,7 @@ import ( // NewLogger create a logrus logger, add hook to it and return it. func NewLogger(zapLogger *zap.Logger) *logrus.Logger { logger := logrus.New() - logger.SetOutput(ioutil.Discard) + logger.SetOutput(io.Discard) logger.AddHook(newHook(zapLogger)) return logger } diff --git a/options.go b/options.go index ba1946e..9241ef5 100644 --- a/options.go +++ b/options.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package log import ( @@ -38,11 +39,11 @@ const ( // Options contains configuration items related to log. type Options struct { - Level string `json:"level" mapstructure:"level"` - Format string `json:"format" mapstructure:"format"` - EnableColor bool `json:"enable-color" mapstructure:"enable-color"` - EnableCaller bool `json:"enable-caller" mapstructure:"enable-caller"` - OutputPaths []string `json:"output-paths" mapstructure:"output-paths"` + Level string `json:"level" mapstructure:"level"` + Format string `json:"format" mapstructure:"format"` + EnableColor bool `json:"enable-color" mapstructure:"enable-color"` + EnableCaller bool `json:"enable-caller" mapstructure:"enable-caller"` + OutputPaths []string `json:"output-paths" mapstructure:"output-paths"` ErrorOutputPaths []string `json:"error-output-paths" mapstructure:"error-output-paths"` } diff --git a/options_test.go b/options_test.go index 6f6d3dc..7b2d692 100644 --- a/options_test.go +++ b/options_test.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package log_test import ( diff --git a/types.go b/types.go index bf92375..c1a3259 100644 --- a/types.go +++ b/types.go @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package log import (