Skip to content

Commit 1943e7d

Browse files
yuluo-yxLinuxSuRen
andauthored
feat: add logging support (#389)
* feat: add logging support --------- Signed-off-by: yuluo-yx <yuluo08290126@gmail.com> Signed-off-by: YuLuo <yuluo08290126@gmail.com> Signed-off-by: Rick <1450685+LinuxSuRen@users.noreply.github.com> Co-authored-by: Rick <1450685+LinuxSuRen@users.noreply.github.com>
1 parent ea911ae commit 1943e7d

37 files changed

+1243
-221
lines changed

.github/workflows/build.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v4
1515
with:
16-
go-version: 1.20.x
16+
go-version: 1.22
1717
- name: Unit Test
1818
run: |
1919
make test-all-backend
@@ -40,7 +40,7 @@ jobs:
4040
- name: Set up Go
4141
uses: actions/setup-go@v4
4242
with:
43-
go-version: 1.20.x
43+
go-version: 1.22
4444
- name: API Test
4545
env:
4646
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -70,7 +70,7 @@ jobs:
7070
- name: Set up Go
7171
uses: actions/setup-go@v4
7272
with:
73-
go-version: 1.20.x
73+
go-version: 1.22
7474
- name: Run GoReleaser
7575
uses: goreleaser/goreleaser-action@v4
7676
env:
@@ -91,7 +91,7 @@ jobs:
9191
- name: Set up Go
9292
uses: actions/setup-go@v4
9393
with:
94-
go-version: 1.20.x
94+
go-version: 1.22
9595
- name: Set output
9696
id: vars
9797
run: echo "tag=$(git describe --tags)" >> $GITHUB_OUTPUT
@@ -126,7 +126,7 @@ jobs:
126126
- name: Set up Go
127127
uses: actions/setup-go@v4
128128
with:
129-
go-version: 1.20.x
129+
go-version: 1.22
130130
- name: Use Node.js
131131
uses: actions/setup-node@v3
132132
with:

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Set up Go
2525
uses: actions/setup-go@v4
2626
with:
27-
go-version: 1.20.x
27+
go-version: 1.22.2
2828
- name: Use Node.js
2929
uses: actions/setup-node@v3
3030
with:
@@ -46,7 +46,7 @@ jobs:
4646
- name: Set up Go
4747
uses: actions/setup-go@v4
4848
with:
49-
go-version: 1.20.x
49+
go-version: 1.22.2
5050
- name: Unit Test
5151
run: |
5252
make test-all-backend

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ COPY console/atest-ui .
55
RUN npm install --ignore-scripts --registry=https://registry.npmmirror.com
66
RUN npm run build-only
77

8-
FROM docker.io/golang:1.20 AS builder
8+
FROM docker.io/golang:1.22.2 AS builder
99

1010
ARG VERSION
1111
ARG GOPROXY

cmd/run.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"log"
2524
"net/http"
2625
"os"
2726
"os/exec"
@@ -32,10 +31,12 @@ import (
3231

3332
"github.com/linuxsuren/api-testing/pkg/apispec"
3433
"github.com/linuxsuren/api-testing/pkg/limit"
34+
"github.com/linuxsuren/api-testing/pkg/logging"
3535
"github.com/linuxsuren/api-testing/pkg/runner"
3636
"github.com/linuxsuren/api-testing/pkg/runner/monitor"
3737
"github.com/linuxsuren/api-testing/pkg/testing"
3838
"github.com/linuxsuren/api-testing/pkg/util"
39+
3940
fakeruntime "github.com/linuxsuren/go-fake-runtime"
4041
"github.com/spf13/cobra"
4142
"github.com/spf13/pflag"
@@ -71,6 +72,10 @@ type runOption struct {
7172
loader testing.Loader
7273
}
7374

75+
var (
76+
runLogger = logging.DefaultLogger(logging.LogLevelInfo).WithName("run")
77+
)
78+
7479
func newDefaultRunOption() *runOption {
7580
return &runOption{
7681
reporter: runner.NewMemoryTestReporter(nil, ""),
@@ -198,7 +203,7 @@ func (o *runOption) startMonitor() (err error) {
198203
execer := fakeruntime.NewDefaultExecerWithContext(o.context)
199204
go func(socketURL, plugin string) {
200205
if err = execer.RunCommandWithIO(plugin, "", os.Stdout, os.Stderr, nil, "server", "--socket", socketURL); err != nil {
201-
log.Printf("failed to start %s, error: %v", socketURL, err)
206+
runLogger.Info("failed to start", "socketURL", socketURL, " error", err.Error())
202207
}
203208
}(sockFile, monitorBin)
204209

@@ -287,7 +292,7 @@ func (o *runOption) runSuiteWithDuration(loader testing.Loader) (err error) {
287292
defer sem.Release(1)
288293
defer wait.Done()
289294
defer func() {
290-
log.Println("routing end with", time.Since(now))
295+
runLogger.Info("routing end with", "time", time.Since(now))
291296
}()
292297

293298
dataContext := getDefaultContext()

cmd/server.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"context"
2323
"errors"
2424
"fmt"
25-
"log"
2625
"net"
2726
"net/http"
2827
"os"
@@ -37,13 +36,15 @@ import (
3736
pprof "net/http/pprof"
3837

3938
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
39+
"github.com/linuxsuren/api-testing/pkg/logging"
4040
"github.com/linuxsuren/api-testing/pkg/oauth"
4141
template "github.com/linuxsuren/api-testing/pkg/render"
4242
"github.com/linuxsuren/api-testing/pkg/server"
4343
"github.com/linuxsuren/api-testing/pkg/testing"
4444
"github.com/linuxsuren/api-testing/pkg/testing/remote"
4545
"github.com/linuxsuren/api-testing/pkg/util"
4646
fakeruntime "github.com/linuxsuren/go-fake-runtime"
47+
4748
"github.com/prometheus/client_golang/prometheus"
4849
"github.com/prometheus/client_golang/prometheus/collectors"
4950
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -55,6 +56,10 @@ import (
5556
"google.golang.org/grpc/reflection"
5657
)
5758

59+
var (
60+
serverLogger = logging.DefaultLogger(logging.LogLevelInfo).WithName("server")
61+
)
62+
5863
func createServerCmd(execer fakeruntime.Execer, httpServer server.HTTPServer) (c *cobra.Command) {
5964
opt := &serverOption{
6065
httpServer: httpServer,
@@ -228,15 +233,15 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
228233
reflection.Register(gRPCServer)
229234
}
230235
server.RegisterRunnerServer(s, remoteServer)
231-
log.Printf("gRPC server listening at %v", lis.Addr())
236+
serverLogger.Info("gRPC server listening at", "addr", lis.Addr())
232237
s.Serve(lis)
233238
}()
234239

235240
go func() {
236241
<-clean
237-
log.Println("stopping the extensions")
242+
serverLogger.Info("stopping the extensions")
238243
storeExtMgr.StopAll()
239-
log.Println("stopping the server")
244+
serverLogger.Info("stopping the server")
240245
_ = lis.Close()
241246
_ = o.httpServer.Shutdown(ctx)
242247
}()
@@ -280,8 +285,8 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
280285

281286
debugHandler(mux, remoteServer)
282287
o.httpServer.WithHandler(mux)
283-
log.Printf("HTTP server listening at %v", httplis.Addr())
284-
log.Printf("Server is running.")
288+
serverLogger.Info("HTTP server listening at", "addr", httplis.Addr())
289+
serverLogger.Info("Server is running.")
285290
err = o.httpServer.Serve(httplis)
286291
err = util.IgnoreErrServerClosed(err)
287292
}
@@ -354,7 +359,7 @@ func debugHandler(mux *runtime.ServeMux, remoteServer server.RunnerServer) {
354359
sub := pathParams["sub"]
355360
extName := r.URL.Query().Get("name")
356361
if extName != "" && remoteServer != nil {
357-
log.Println("get pprof of extension:", extName)
362+
serverLogger.Info("get pprof of extension", "name", extName)
358363

359364
ctx := metadata.NewIncomingContext(r.Context(), metadata.New(map[string]string{
360365
server.HeaderKeyStoreName: extName,

go.mod

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/linuxsuren/api-testing
22

3-
go 1.20
3+
go 1.22.2
44

55
require (
66
github.com/Masterminds/sprig/v3 v3.2.3
@@ -10,6 +10,10 @@ require (
1010
github.com/expr-lang/expr v1.15.6
1111
github.com/flopp/go-findfont v0.1.0
1212
github.com/ghodss/yaml v1.0.0
13+
github.com/go-logr/logr v1.4.1
14+
github.com/go-logr/zapr v1.3.0
15+
github.com/gorilla/mux v1.8.1
16+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0
1317
github.com/gorilla/mux v1.8.1
1418
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
1519
github.com/h2non/gock v1.2.0
@@ -18,62 +22,64 @@ require (
1822
github.com/linuxsuren/go-fake-runtime v0.0.4
1923
github.com/linuxsuren/go-service v0.0.0-20231225060426-efabcd3a5161
2024
github.com/linuxsuren/unstructured v0.0.1
21-
github.com/prometheus/client_golang v1.16.0
25+
github.com/prometheus/client_golang v1.19.0
2226
github.com/signintech/gopdf v0.18.0
23-
github.com/spf13/cobra v1.6.1
27+
github.com/spf13/cobra v1.8.0
2428
github.com/spf13/pflag v1.0.5
25-
github.com/stretchr/testify v1.8.4
29+
github.com/stretchr/testify v1.9.0
2630
github.com/tidwall/gjson v1.14.4
2731
github.com/xeipuuv/gojsonschema v1.2.0
28-
golang.org/x/oauth2 v0.14.0
29-
golang.org/x/sync v0.3.0
30-
google.golang.org/grpc v1.57.0
31-
google.golang.org/protobuf v1.31.0
32+
go.uber.org/zap v1.27.0
33+
golang.org/x/oauth2 v0.18.0
34+
golang.org/x/sync v0.6.0
35+
google.golang.org/grpc v1.62.1
36+
google.golang.org/protobuf v1.33.0
3237
gopkg.in/yaml.v3 v3.0.1
3338
)
3439

3540
require (
3641
github.com/Masterminds/goutils v1.1.1 // indirect
37-
github.com/Masterminds/semver/v3 v3.2.0 // indirect
42+
github.com/Masterminds/semver/v3 v3.2.1 // indirect
3843
github.com/beorn7/perks v1.0.1 // indirect
3944
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4045
github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect
4146
github.com/cucumber/messages-go/v16 v16.0.1 // indirect
4247
github.com/davecgh/go-spew v1.1.1 // indirect
4348
github.com/gofrs/uuid v4.2.0+incompatible // indirect
49+
github.com/golang/protobuf v1.5.4 // indirect
50+
github.com/google/uuid v1.6.0 // indirect
4451
github.com/golang/protobuf v1.5.3 // indirect
4552
github.com/google/uuid v1.3.0 // indirect
4653
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
4754
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
4855
github.com/hashicorp/go-memdb v1.3.2 // indirect
4956
github.com/hashicorp/golang-lru v0.5.4 // indirect
50-
github.com/huandu/xstrings v1.3.3 // indirect
57+
github.com/huandu/xstrings v1.4.0 // indirect
5158
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
52-
github.com/imdario/mergo v0.3.11 // indirect
53-
github.com/inconshreveable/mousetrap v1.0.1 // indirect
54-
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
55-
github.com/mitchellh/copystructure v1.0.0 // indirect
56-
github.com/mitchellh/reflectwalk v1.0.0 // indirect
59+
github.com/imdario/mergo v0.3.16 // indirect
60+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
61+
github.com/mitchellh/copystructure v1.2.0 // indirect
62+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5763
github.com/phpdave11/gofpdi v1.0.14-0.20211212211723-1f10f9844311 // indirect
5864
github.com/pkg/errors v0.9.1 // indirect
5965
github.com/pmezard/go-difflib v1.0.0 // indirect
60-
github.com/prometheus/client_model v0.3.0 // indirect
61-
github.com/prometheus/common v0.42.0 // indirect
62-
github.com/prometheus/procfs v0.10.1 // indirect
63-
github.com/rogpeppe/go-internal v1.10.0 // indirect
66+
github.com/prometheus/client_model v0.6.0 // indirect
67+
github.com/prometheus/common v0.50.0 // indirect
68+
github.com/prometheus/procfs v0.12.0 // indirect
6469
github.com/sergi/go-diff v1.2.0 // indirect
65-
github.com/shopspring/decimal v1.2.0 // indirect
70+
github.com/shopspring/decimal v1.3.1 // indirect
6671
github.com/spf13/cast v1.5.0 // indirect
6772
github.com/tidwall/match v1.1.1 // indirect
6873
github.com/tidwall/pretty v1.2.1 // indirect
69-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
74+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
7075
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
71-
golang.org/x/crypto v0.17.0 // indirect
72-
golang.org/x/net v0.18.0 // indirect
73-
golang.org/x/sys v0.15.0 // indirect
76+
go.uber.org/multierr v1.11.0 // indirect
77+
golang.org/x/crypto v0.21.0 // indirect
78+
golang.org/x/net v0.22.0 // indirect
79+
golang.org/x/sys v0.18.0 // indirect
7480
golang.org/x/text v0.14.0 // indirect
75-
google.golang.org/appengine v1.6.7 // indirect
76-
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
77-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
81+
google.golang.org/appengine v1.6.8 // indirect
82+
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
83+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
7884
gopkg.in/yaml.v2 v2.4.0 // indirect
7985
)

0 commit comments

Comments
 (0)