forked from xinliangnote/go-gin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc613b9
commit 4302709
Showing
95 changed files
with
6,078 additions
and
1,966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package grpc_log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go-gin-api/app/config" | ||
"go-gin-api/app/util" | ||
"google.golang.org/grpc" | ||
"log" | ||
"os" | ||
) | ||
|
||
var grpcChannel = make(chan string, 100) | ||
|
||
func ClientInterceptor() grpc.UnaryClientInterceptor { | ||
|
||
go handleGrpcChannel() | ||
|
||
return func(ctx context.Context, method string, | ||
req, reply interface{}, cc *grpc.ClientConn, | ||
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
|
||
// 开始时间 | ||
startTime := util.GetCurrentMilliUnix() | ||
|
||
err := invoker(ctx, method, req, reply, cc, opts...) | ||
|
||
// 结束时间 | ||
endTime := util.GetCurrentMilliUnix() | ||
|
||
// 日志格式 | ||
grpcLogMap := make(map[string]interface{}) | ||
|
||
grpcLogMap["request_time"] = startTime | ||
grpcLogMap["request_data"] = req | ||
grpcLogMap["request_method"] = method | ||
|
||
grpcLogMap["response_data"] = reply | ||
grpcLogMap["response_error"] = err | ||
|
||
grpcLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime) | ||
|
||
grpcLogJson, _ := util.JsonEncode(grpcLogMap) | ||
|
||
grpcChannel <- grpcLogJson | ||
|
||
return err | ||
} | ||
} | ||
|
||
func handleGrpcChannel() { | ||
if f, err := os.OpenFile(config.AppGrpcLogName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil { | ||
log.Println(err) | ||
} else { | ||
for accessLog := range grpcChannel { | ||
_, _ = f.WriteString(accessLog + "\n") | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.