/
sink.go
51 lines (40 loc) · 1.27 KB
/
sink.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
39
40
41
42
43
44
45
46
47
48
49
50
51
package logging
import (
"context"
"github.com/hashicorp/go-hclog"
)
// GetSink returns the sink logger used for writing logs.
// If no sink logger has been created, it will return nil.
func GetSink(ctx context.Context) hclog.Logger {
logger := ctx.Value(SinkKey)
if logger == nil {
return nil
}
return logger.(hclog.Logger)
}
// GetSinkOptions returns the root logger options used for
// creating the root SDK logger. If the root logger has not been created or
// the options are not present, it will return nil.
func GetSinkOptions(ctx context.Context) *hclog.LoggerOptions {
if GetSink(ctx) == nil {
return nil
}
loggerOptionsRaw := ctx.Value(SinkOptionsKey)
if loggerOptionsRaw == nil {
return nil
}
loggerOptions, ok := loggerOptionsRaw.(*hclog.LoggerOptions)
if !ok {
return nil
}
return loggerOptions
}
// SetSink sets `logger` as the sink logger used for writing logs.
func SetSink(ctx context.Context, logger hclog.Logger) context.Context {
return context.WithValue(ctx, SinkKey, logger)
}
// SetSinkOptions sets `loggerOptions` as the root logger options
// used for creating the SDK root logger.
func SetSinkOptions(ctx context.Context, loggerOptions *hclog.LoggerOptions) context.Context {
return context.WithValue(ctx, SinkOptionsKey, loggerOptions)
}