-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logger interceptor to gRPC server
- Loading branch information
1 parent
05cf5ed
commit 17d1f94
Showing
6 changed files
with
69 additions
and
20 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
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,35 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"github.com/rs/zerolog/log" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"time" | ||
) | ||
|
||
func GrpcLogger(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) { | ||
startTime := time.Now() | ||
result, err := handler(ctx, req) | ||
duration := time.Since(startTime) | ||
|
||
statusCode := codes.Unknown | ||
if st, ok := status.FromError(err); ok { | ||
statusCode = st.Code() | ||
} | ||
|
||
logger := log.Info() | ||
if err != nil { | ||
logger = log.Error() | ||
} | ||
|
||
logger. | ||
Str("protocol", "grpc"). | ||
Str("method", info.FullMethod). | ||
Int("status_code", int(statusCode)). | ||
Str("status_text", statusCode.String()). | ||
Dur("duration", duration). | ||
Msg("sending protocol") | ||
return result, err | ||
} |
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