Skip to content

Commit cb97807

Browse files
authored
docs:optimize logger caller (#85)
1 parent 0fe3c64 commit cb97807

File tree

9 files changed

+63
-24
lines changed

9 files changed

+63
-24
lines changed

Diff for: async_call/client/main.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ package main
1717

1818
import (
1919
"context"
20-
"github.com/cloudwego/kitex-examples/kitex_gen/api"
21-
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
22-
"github.com/cloudwego/kitex/client"
2320
"log"
2421
"strings"
2522
"time"
23+
24+
"github.com/cloudwego/kitex-examples/kitex_gen/api"
25+
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
26+
"github.com/cloudwego/kitex/client"
2627
)
2728

2829
func NewFuture(f func() (interface{}, error)) func() (interface{}, error) {
@@ -41,9 +42,8 @@ func NewFuture(f func() (interface{}, error)) func() (interface{}, error) {
4142
}
4243

4344
func sequentialCall(client echo.Client) {
44-
4545
for i := 1; i < 5; i++ {
46-
var req = &api.Request{Message: "my request"}
46+
req := &api.Request{Message: "my request"}
4747
resp, err := client.Echo(context.Background(), req)
4848
if err != nil {
4949
log.Fatal(err)
@@ -55,7 +55,7 @@ func sequentialCall(client echo.Client) {
5555
func asyncParallelCall(client echo.Client) {
5656
var futures []func() (interface{}, error)
5757
for i := 0; i < 5; i++ {
58-
var req = &api.Request{Message: "my request"}
58+
req := &api.Request{Message: "my request"}
5959

6060
futures = append(futures, NewFuture(func() (interface{}, error) {
6161
return client.Echo(context.Background(), req)
@@ -88,5 +88,4 @@ func main() {
8888
t1 := time.Now()
8989
asyncParallelCall(client)
9090
log.Println("cast time: " + time.Since(t1).String())
91-
9291
}

Diff for: async_call/server/main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package main
1717

1818
import (
1919
"context"
20+
"log"
21+
"time"
22+
2023
"github.com/cloudwego/kitex-examples/kitex_gen/api"
2124
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
2225
"github.com/cloudwego/kitex/pkg/klog"
23-
"log"
24-
"time"
2526
)
2627

2728
var _ api.Echo = &EchoImpl{}
@@ -32,7 +33,7 @@ type EchoImpl struct{}
3233
// Echo implements the Echo interface.
3334
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
3435
klog.Info("echo called")
35-
//computing...
36+
// computing...
3637
time.Sleep(time.Duration(1) * time.Second)
3738
return &api.Response{Message: req.Message}, nil
3839
}

Diff for: discovery/p2p/resolver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ func (p *P2PResolver) Name() string {
5151
// Diff computes the difference between two results.
5252
// When `next` is cacheable, the Change should be cacheable, too. And the `Result` field's CacheKey in
5353
// the return value should be set with the given cacheKey.
54-
func (p *P2PResolver) Diff(cacheKey string, prev discovery.Result, next discovery.Result) (discovery.Change, bool) {
54+
func (p *P2PResolver) Diff(cacheKey string, prev, next discovery.Result) (discovery.Change, bool) {
5555
return discovery.DefaultDiff(cacheKey, prev, next)
5656
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ require (
2828
github.com/sirupsen/logrus v1.9.2
2929
github.com/tidwall/gjson v1.14.4 // indirect
3030
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
31+
go.uber.org/zap v1.26.0
3132
gopkg.in/natefinch/lumberjack.v2 v2.0.0
3233
)

Diff for: go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4x
832832
github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/PEd/zm8mDS9Vg=
833833
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
834834
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
835-
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
836835
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
837836
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
838837
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
@@ -1372,13 +1371,16 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
13721371
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
13731372
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
13741373
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
1374+
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
13751375
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
13761376
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
13771377
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
1378+
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
13781379
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
13791380
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
1380-
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
13811381
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
1382+
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
1383+
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
13821384
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
13831385
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
13841386
golang.org/x/arch v0.0.0-20220722155209-00200b7164a7/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=

Diff for: klog/custom/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"context"
19+
1920
"github.com/cloudwego/kitex-examples/kitex_gen/api"
2021
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
2122
"github.com/cloudwego/kitex/pkg/klog"

Diff for: klog/logrus/main.go

+37-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ package main
1515

1616
import (
1717
"context"
18+
"fmt"
19+
"log"
20+
"os"
21+
"path"
22+
"runtime"
23+
"time"
24+
1825
"github.com/cloudwego/kitex-examples/kitex_gen/api"
1926
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
2027
"github.com/cloudwego/kitex/pkg/klog"
2128
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
29+
"github.com/sirupsen/logrus"
2230
"gopkg.in/natefinch/lumberjack.v2"
23-
"log"
24-
"os"
25-
"path"
26-
"time"
2731
)
2832

2933
var _ api.Echo = &EchoImpl{}
@@ -60,6 +64,9 @@ func main() {
6064
}
6165

6266
logger := kitexlogrus.NewLogger()
67+
logger.Logger().SetReportCaller(true)
68+
// klog will warp a layer of logrus, so you need to calculate the depth of the caller file separately.
69+
logger.Logger().AddHook(NewCustomHook(10))
6370
// Provides compression and deletion
6471
lumberjackLogger := &lumberjack.Logger{
6572
Filename: fileName,
@@ -83,3 +90,29 @@ func main() {
8390
klog.CtxDebugf(context.Background(), "server stopped")
8491
}
8592
}
93+
94+
// CustomHook Custom Hook for processing logs
95+
type CustomHook struct {
96+
CallerDepth int
97+
}
98+
99+
func NewCustomHook(depth int) *CustomHook {
100+
return &CustomHook{
101+
CallerDepth: depth,
102+
}
103+
}
104+
105+
func (hook *CustomHook) Levels() []logrus.Level {
106+
return logrus.AllLevels
107+
}
108+
109+
func (hook *CustomHook) Fire(entry *logrus.Entry) error {
110+
// Get caller information and specify depth
111+
pc, file, line, ok := runtime.Caller(hook.CallerDepth)
112+
if ok {
113+
funcName := runtime.FuncForPC(pc).Name()
114+
entry.Data["caller"] = fmt.Sprintf("%s:%d %s", file, line, funcName)
115+
}
116+
117+
return nil
118+
}

Diff for: klog/zap/main.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ package main
1616

1717
import (
1818
"context"
19+
"log"
20+
"os"
21+
"path"
22+
"time"
23+
1924
"github.com/cloudwego/kitex-examples/kitex_gen/api"
2025
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
2126
"github.com/cloudwego/kitex/pkg/klog"
2227
kitexzap "github.com/kitex-contrib/obs-opentelemetry/logging/zap"
28+
"go.uber.org/zap"
2329
"gopkg.in/natefinch/lumberjack.v2"
24-
"log"
25-
"os"
26-
"path"
27-
"time"
2830
)
2931

3032
var _ api.Echo = &EchoImpl{}
@@ -59,8 +61,8 @@ func main() {
5961
return
6062
}
6163
}
62-
63-
logger := kitexzap.NewLogger()
64+
// klog will warp a layer of zap, so you need to calculate the depth of the caller file separately.
65+
logger := kitexzap.NewLogger(kitexzap.WithZapOptions(zap.AddCallerSkip(3), zap.AddCaller()))
6466
// Provides compression and deletion
6567
lumberjackLogger := &lumberjack.Logger{
6668
Filename: fileName,

Diff for: profiler/server/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var sizeClassTagging remote.MessageTagging = func(ctx context.Context, msg remot
6565
if data := msg.Data(); data == nil {
6666
return ctx, nil
6767
}
68-
var tags = make([]string, 0, 2)
68+
tags := make([]string, 0, 2)
6969
var reqType int32
7070
if args, ok := msg.Data().(ArgsGetter); ok {
7171
if req := args.GetReq(); req != nil {

0 commit comments

Comments
 (0)