github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/log.go (about) 1 // Copyright 2022 Edward McFarlane. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package larking 6 7 import ( 8 "context" 9 10 "github.com/go-logr/logr" 11 "google.golang.org/grpc" 12 ) 13 14 type logStream struct { 15 grpc.ServerStream 16 log logr.Logger 17 } 18 19 func (s logStream) Context() context.Context { 20 return logr.NewContext(s.ServerStream.Context(), s.log) 21 } 22 23 func NewUnaryContextLogr(log logr.Logger) grpc.UnaryServerInterceptor { 24 return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { 25 ctx = logr.NewContext(ctx, log) 26 return handler(ctx, req) 27 } 28 } 29 30 func NewStreamContextLogr(log logr.Logger) grpc.StreamServerInterceptor { 31 return func(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error { 32 return handler(srv, logStream{ 33 ServerStream: ss, 34 log: log, 35 }) 36 } 37 }