forked from ciricc/zerolog-grpc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
36 lines (27 loc) · 764 Bytes
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package zerologgrpcprovider
import (
"context"
"fmt"
"github.com/rs/zerolog"
)
// GetLogger returns provided logger from the context
func GetLogger(ctx context.Context) (*zerolog.Logger, error) {
loggerInterface := ctx.Value(loggerCtxKey{})
if loggerInterface == nil {
return nil, fmt.Errorf("no logger provided to the context")
}
loggerValue, ok := loggerInterface.(*zerolog.Logger)
if !ok {
return nil, fmt.Errorf("failed to cast logger interface value to *zerolog.Logger type")
}
return loggerValue, nil
}
// MustGetLogger returns the logger from the context
// May panic if logger is not available
func MustGetLogger(ctx context.Context) *zerolog.Logger {
logger, err := GetLogger(ctx)
if err != nil {
panic(err)
}
return logger
}