Skip to content

Commit

Permalink
docs:optimize logger caller (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViolaPioggia authored Nov 28, 2023
1 parent 0fe3c64 commit cb97807
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
13 changes: 6 additions & 7 deletions async_call/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package main

import (
"context"
"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/client"
"log"
"strings"
"time"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/client"
)

func NewFuture(f func() (interface{}, error)) func() (interface{}, error) {
Expand All @@ -41,9 +42,8 @@ func NewFuture(f func() (interface{}, error)) func() (interface{}, error) {
}

func sequentialCall(client echo.Client) {

for i := 1; i < 5; i++ {
var req = &api.Request{Message: "my request"}
req := &api.Request{Message: "my request"}
resp, err := client.Echo(context.Background(), req)
if err != nil {
log.Fatal(err)
Expand All @@ -55,7 +55,7 @@ func sequentialCall(client echo.Client) {
func asyncParallelCall(client echo.Client) {
var futures []func() (interface{}, error)
for i := 0; i < 5; i++ {
var req = &api.Request{Message: "my request"}
req := &api.Request{Message: "my request"}

futures = append(futures, NewFuture(func() (interface{}, error) {
return client.Echo(context.Background(), req)
Expand Down Expand Up @@ -88,5 +88,4 @@ func main() {
t1 := time.Now()
asyncParallelCall(client)
log.Println("cast time: " + time.Since(t1).String())

}
7 changes: 4 additions & 3 deletions async_call/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package main

import (
"context"
"log"
"time"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
"log"
"time"
)

var _ api.Echo = &EchoImpl{}
Expand All @@ -32,7 +33,7 @@ type EchoImpl struct{}
// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
klog.Info("echo called")
//computing...
// computing...
time.Sleep(time.Duration(1) * time.Second)
return &api.Response{Message: req.Message}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion discovery/p2p/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func (p *P2PResolver) Name() string {
// Diff computes the difference between two results.
// When `next` is cacheable, the Change should be cacheable, too. And the `Result` field's CacheKey in
// the return value should be set with the given cacheKey.
func (p *P2PResolver) Diff(cacheKey string, prev discovery.Result, next discovery.Result) (discovery.Change, bool) {
func (p *P2PResolver) Diff(cacheKey string, prev, next discovery.Result) (discovery.Change, bool) {
return discovery.DefaultDiff(cacheKey, prev, next)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ require (
github.com/sirupsen/logrus v1.9.2
github.com/tidwall/gjson v1.14.4 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/zap v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,6 @@ github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4x
github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/PEd/zm8mDS9Vg=
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -1372,13 +1371,16 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
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.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
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/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.0.0-20220722155209-00200b7164a7/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
1 change: 1 addition & 0 deletions klog/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"context"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
Expand Down
41 changes: 37 additions & 4 deletions klog/logrus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ package main

import (
"context"
"fmt"
"log"
"os"
"path"
"runtime"
"time"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"path"
"time"
)

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -60,6 +64,9 @@ func main() {
}

logger := kitexlogrus.NewLogger()
logger.Logger().SetReportCaller(true)
// klog will warp a layer of logrus, so you need to calculate the depth of the caller file separately.
logger.Logger().AddHook(NewCustomHook(10))
// Provides compression and deletion
lumberjackLogger := &lumberjack.Logger{
Filename: fileName,
Expand All @@ -83,3 +90,29 @@ func main() {
klog.CtxDebugf(context.Background(), "server stopped")
}
}

// CustomHook Custom Hook for processing logs
type CustomHook struct {
CallerDepth int
}

func NewCustomHook(depth int) *CustomHook {
return &CustomHook{
CallerDepth: depth,
}
}

func (hook *CustomHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (hook *CustomHook) Fire(entry *logrus.Entry) error {
// Get caller information and specify depth
pc, file, line, ok := runtime.Caller(hook.CallerDepth)
if ok {
funcName := runtime.FuncForPC(pc).Name()
entry.Data["caller"] = fmt.Sprintf("%s:%d %s", file, line, funcName)
}

return nil
}
14 changes: 8 additions & 6 deletions klog/zap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ package main

import (
"context"
"log"
"os"
"path"
"time"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
kitexzap "github.com/kitex-contrib/obs-opentelemetry/logging/zap"
"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"path"
"time"
)

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -59,8 +61,8 @@ func main() {
return
}
}

logger := kitexzap.NewLogger()
// klog will warp a layer of zap, so you need to calculate the depth of the caller file separately.
logger := kitexzap.NewLogger(kitexzap.WithZapOptions(zap.AddCallerSkip(3), zap.AddCaller()))
// Provides compression and deletion
lumberjackLogger := &lumberjack.Logger{
Filename: fileName,
Expand Down
2 changes: 1 addition & 1 deletion profiler/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var sizeClassTagging remote.MessageTagging = func(ctx context.Context, msg remot
if data := msg.Data(); data == nil {
return ctx, nil
}
var tags = make([]string, 0, 2)
tags := make([]string, 0, 2)
var reqType int32
if args, ok := msg.Data().(ArgsGetter); ok {
if req := args.GetReq(); req != nil {
Expand Down

0 comments on commit cb97807

Please sign in to comment.