-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsdk.go
More file actions
74 lines (60 loc) · 2.43 KB
/
sdk.go
File metadata and controls
74 lines (60 loc) · 2.43 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package logging
import (
"context"
"github.com/hashicorp/go-hclog"
)
// GetSDKRootLogger returns the root logger used for writing logs from an SDK.
// If no root logger has been created, it will return nil.
func GetSDKRootLogger(ctx context.Context) hclog.Logger {
logger := ctx.Value(SDKRootLoggerKey)
if logger == nil {
return nil
}
return logger.(hclog.Logger)
}
// GetSDKRootLoggerOptions 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 GetSDKRootLoggerOptions(ctx context.Context) *hclog.LoggerOptions {
if GetSDKRootLogger(ctx) == nil {
return nil
}
loggerOptionsRaw := ctx.Value(SDKRootLoggerOptionsKey)
if loggerOptionsRaw == nil {
return nil
}
loggerOptions, ok := loggerOptionsRaw.(*hclog.LoggerOptions)
if !ok {
return nil
}
return loggerOptions
}
// SetSDKRootLogger sets `logger` as the root logger used for writing logs from
// an SDK.
func SetSDKRootLogger(ctx context.Context, logger hclog.Logger) context.Context {
return context.WithValue(ctx, SDKRootLoggerKey, logger)
}
// SetSDKRootLoggerOptions sets `loggerOptions` as the root logger options
// used for creating the SDK root logger.
func SetSDKRootLoggerOptions(ctx context.Context, loggerOptions *hclog.LoggerOptions) context.Context {
return context.WithValue(ctx, SDKRootLoggerOptionsKey, loggerOptions)
}
// NewSDKSubsystemLoggerWarning is the text included in log output when a
// subsystem is auto-generated by terraform-plugin-log because it was used
// before the SDK instantiated it.
const NewSDKSubsystemLoggerWarning = "This log was generated by an SDK subsystem logger that wasn't created before being used. Use tflog.NewSubsystem to create this logger before it is used."
// GetSDKSubsystemLogger returns the subsystem logger for the named subsystem
// in SDK space. If no such subsystem logger has been created, it will return
// nil.
func GetSDKSubsystemLogger(ctx context.Context, subsystem string) hclog.Logger {
logger := ctx.Value(SDKRootLoggerKey + loggerKey("."+subsystem))
if logger == nil {
return nil
}
return logger.(hclog.Logger)
}
// SetSDKSubsystemLogger sets `logger` as the logger for the named subsystem in
// SDK space.
func SetSDKSubsystemLogger(ctx context.Context, subsystem string, logger hclog.Logger) context.Context {
return context.WithValue(ctx, SDKRootLoggerKey+loggerKey("."+subsystem), logger)
}