Skip to content

Commit

Permalink
upgrade golang version to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
huhouhua committed Feb 10, 2024
1 parent 6ba75d4 commit 902119d
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 35 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
# - deadcode
# - depguard
- dogsled
- dupl
- errcheck
Expand All @@ -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
Expand Down
71 changes: 71 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 <TARGETS> ...\n\nTargets:"
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo "$$USAGE_OPTIONS"
1 change: 1 addition & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package log

import (
Expand Down
4 changes: 3 additions & 1 deletion distribution/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package log

import (
Expand Down
2 changes: 1 addition & 1 deletion example/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/seacraft/log/example

go 1.20
go 1.21

require (
github.com/seacraft/log v1.0.0
Expand Down
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 5 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions klog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand All @@ -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())
}

Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions logrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package logrus

import (
Expand Down
5 changes: 3 additions & 2 deletions logrus/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
11 changes: 6 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package log

import (
Expand All @@ -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"`
}

Expand Down
Loading

0 comments on commit 902119d

Please sign in to comment.