get properly configured hcloggers

make sure plugins get hcloggers configured to match core
This commit is contained in:
James Bardin 2020-10-17 09:57:03 -04:00
parent 8a7838266e
commit abf6b9b378
3 changed files with 33 additions and 25 deletions

View File

@ -8,13 +8,13 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
plugin "github.com/hashicorp/go-plugin" plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
terraformProvider "github.com/hashicorp/terraform/builtin/providers/terraform" terraformProvider "github.com/hashicorp/terraform/builtin/providers/terraform"
"github.com/hashicorp/terraform/internal/getproviders" "github.com/hashicorp/terraform/internal/getproviders"
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/internal/providercache" "github.com/hashicorp/terraform/internal/providercache"
tfplugin "github.com/hashicorp/terraform/plugin" tfplugin "github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/providers"
@ -328,12 +328,6 @@ func (m *Meta) internalProviders() map[string]providers.Factory {
// providers.Interface against it. // providers.Interface against it.
func providerFactory(meta *providercache.CachedProvider) providers.Factory { func providerFactory(meta *providercache.CachedProvider) providers.Factory {
return func() (providers.Interface, error) { return func() (providers.Interface, error) {
logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Level: hclog.Trace,
Output: os.Stderr,
})
execFile, err := meta.ExecutableFile() execFile, err := meta.ExecutableFile()
if err != nil { if err != nil {
return nil, err return nil, err
@ -341,7 +335,7 @@ func providerFactory(meta *providercache.CachedProvider) providers.Factory {
config := &plugin.ClientConfig{ config := &plugin.ClientConfig{
HandshakeConfig: tfplugin.Handshake, HandshakeConfig: tfplugin.Handshake,
Logger: logger, Logger: logging.NewHCLogger("plugin"),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Managed: true, Managed: true,
Cmd: exec.Command(execFile), Cmd: exec.Command(execFile),
@ -386,15 +380,10 @@ func devOverrideProviderFactory(provider addrs.Provider, localDir getproviders.P
// running, and implements providers.Interface against it. // running, and implements providers.Interface against it.
func unmanagedProviderFactory(provider addrs.Provider, reattach *plugin.ReattachConfig) providers.Factory { func unmanagedProviderFactory(provider addrs.Provider, reattach *plugin.ReattachConfig) providers.Factory {
return func() (providers.Interface, error) { return func() (providers.Interface, error) {
logger := hclog.New(&hclog.LoggerOptions{
Name: "unmanaged-plugin",
Level: hclog.Trace,
Output: os.Stderr,
})
config := &plugin.ClientConfig{ config := &plugin.ClientConfig{
HandshakeConfig: tfplugin.Handshake, HandshakeConfig: tfplugin.Handshake,
Logger: logger, Logger: logging.NewHCLogger("unmanaged-plugin"),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Managed: false, Managed: false,
Reattach: reattach, Reattach: reattach,

View File

@ -14,6 +14,7 @@ import (
plugin "github.com/hashicorp/go-plugin" plugin "github.com/hashicorp/go-plugin"
"github.com/kardianos/osext" "github.com/kardianos/osext"
"github.com/hashicorp/terraform/internal/logging"
tfplugin "github.com/hashicorp/terraform/plugin" tfplugin "github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/plugin/discovery" "github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/provisioners" "github.com/hashicorp/terraform/provisioners"
@ -167,6 +168,8 @@ func internalPluginClient(kind, name string) (*plugin.Client, error) {
Managed: true, Managed: true,
VersionedPlugins: tfplugin.VersionedPlugins, VersionedPlugins: tfplugin.VersionedPlugins,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
AutoMTLS: enableProviderAutoMTLS,
Logger: logging.NewHCLogger("plugin"),
} }
return plugin.NewClient(cfg), nil return plugin.NewClient(cfg), nil
@ -174,7 +177,16 @@ func internalPluginClient(kind, name string) (*plugin.Client, error) {
func provisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory { func provisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory {
return func() (provisioners.Interface, error) { return func() (provisioners.Interface, error) {
client := tfplugin.Client(meta) cfg := &plugin.ClientConfig{
Cmd: exec.Command(meta.Path),
HandshakeConfig: tfplugin.Handshake,
VersionedPlugins: tfplugin.VersionedPlugins,
Managed: true,
Logger: logging.NewHCLogger("provisioner"),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
AutoMTLS: enableProviderAutoMTLS,
}
client := plugin.NewClient(cfg)
return newProvisionerClient(client) return newProvisionerClient(client)
} }
} }

View File

@ -26,6 +26,21 @@ var ValidLevels = []LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
var logger hclog.Logger var logger hclog.Logger
func init() { func init() {
logger = NewHCLogger("")
}
// LogOutput determines where we should send logs (if anywhere) and the log level.
func LogOutput() (logOutput io.Writer, err error) {
return logger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true}), nil
}
// HCLogger returns the default global loggers
func HCLogger() hclog.Logger {
return logger
}
// NewHCLogger returns a new hclog.Logger instance with the given name
func NewHCLogger(name string) hclog.Logger {
logOutput := io.Writer(os.Stderr) logOutput := io.Writer(os.Stderr)
logLevel := CurrentLogLevel() logLevel := CurrentLogLevel()
if logLevel == "" { if logLevel == "" {
@ -41,21 +56,13 @@ func init() {
} }
} }
logger = hclog.New(&hclog.LoggerOptions{ return hclog.New(&hclog.LoggerOptions{
Name: name,
Level: hclog.LevelFromString(logLevel), Level: hclog.LevelFromString(logLevel),
Output: logOutput, Output: logOutput,
}) })
} }
// LogOutput determines where we should send logs (if anywhere) and the log level.
func LogOutput() (logOutput io.Writer, err error) {
return logger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true}), nil
}
func HCLogger() hclog.Logger {
return logger
}
// SetOutput checks for a log destination with LogOutput, and calls // SetOutput checks for a log destination with LogOutput, and calls
// log.SetOutput with the result. If LogOutput returns nil, SetOutput uses // log.SetOutput with the result. If LogOutput returns nil, SetOutput uses
// ioutil.Discard. Any error from LogOutout is fatal. // ioutil.Discard. Any error from LogOutout is fatal.