-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
38 lines (33 loc) · 1.09 KB
/
options.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
37
38
package log
import (
"context"
"github.com/go-coldbrew/log/loggers"
"github.com/go-coldbrew/options"
)
const logLevelKey = "OverrideLogLevel"
// OverrideLogLevel allows the default log level to be overridden from request context
// This is useful when you want to override the log level for a specific request
// For example, you can set the log level to debug for a specific request
// while the default log level is set to info
func OverrideLogLevel(ctx context.Context, level loggers.Level) context.Context {
return options.AddToOptions(ctx, logLevelKey, level.String())
}
// GetOverridenLogLevel fetches overriden log level from context
// If no log level is overriden, it returns false
// If log level is overriden, it returns the log level and true
func GetOverridenLogLevel(ctx context.Context) (loggers.Level, bool) {
opt := options.FromContext(ctx)
if opt == nil {
return 0, false
}
if val, found := opt.Get(logLevelKey); found {
if level, ok := val.(string); ok {
l, err := loggers.ParseLevel(level)
if err != nil {
return 0, false
}
return l, true
}
}
return 0, false
}