From e2fa378112ab7bdf431a7e8e0fba8f2aa14d1740 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 25 Oct 2023 13:14:09 +0200 Subject: [PATCH] modified the request logger to log more useful things --- server/main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/main.go b/server/main.go index 516727fd..6fd3c114 100644 --- a/server/main.go +++ b/server/main.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "net/textproto" + "strings" "time" "github.com/TUM-Dev/Campus-Backend/server/utils" @@ -114,14 +115,23 @@ func main() { func UnaryRequestLogger(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { start := time.Now() resp, err := handler(ctx, req) - fields := log.Fields{"elapsed": time.Since(start)} - log.WithContext(ctx).WithFields(fields).WithError(err).Info(info.FullMethod) + fields := log.Fields{"elapsed": time.Since(start), "method": strings.TrimPrefix(info.FullMethod, "/api.Campus/")} + if err != nil { + log.WithFields(fields).Info("request") + } else { + log.WithFields(fields).WithError(err).Warn("request") + } return resp, err } func StreamRequestLogger(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { start := time.Now() err := handler(srv, stream) - log.WithField("elapsed", time.Since(start)).WithError(err).Info(info.FullMethod) + fields := log.Fields{"elapsed": time.Since(start), "method": strings.TrimPrefix(info.FullMethod, "/api.Campus/")} + if err != nil { + log.WithFields(fields).Info("request") + } else { + log.WithFields(fields).WithError(err).Warn("request") + } return err }