provider/aws: log HTTP req/resp at DEBUG level

This should be quite helpful in debugging aws-sdk-go operations.

Required some tweaking around the `helper/logging` functions to expose an
`IsDebugOrHigher()` helper for us to use.
This commit is contained in:
Paul Hinze 2016-03-14 11:17:05 -05:00
parent d5f82abee4
commit 25fce81bfc
2 changed files with 44 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"crypto/tls" "crypto/tls"
@ -151,6 +152,11 @@ func (c *Config) Client() (interface{}, error) {
HTTPClient: cleanhttp.DefaultClient(), HTTPClient: cleanhttp.DefaultClient(),
} }
if logging.IsDebugOrHigher() {
awsConfig.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
awsConfig.Logger = awsLogger{}
}
if c.Insecure { if c.Insecure {
transport := awsConfig.HTTPClient.Transport.(*http.Transport) transport := awsConfig.HTTPClient.Transport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{ transport.TLSClientConfig = &tls.Config{
@ -433,3 +439,15 @@ var addTerraformVersionToUserAgent = request.NamedHandler{
Fn: request.MakeAddToUserAgentHandler( Fn: request.MakeAddToUserAgentHandler(
"terraform", terraform.Version, terraform.VersionPrerelease), "terraform", terraform.Version, terraform.VersionPrerelease),
} }
type awsLogger struct{}
func (l awsLogger) Log(args ...interface{}) {
tokens := make([]string, 0, len(args))
for _, arg := range args {
if token, ok := arg.(string); ok {
tokens = append(tokens, token)
}
}
log.Printf("[DEBUG] [aws-sdk-go] %s", strings.Join(tokens, " "))
}

View File

@ -22,8 +22,9 @@ var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
// LogOutput determines where we should send logs (if anywhere) and the log level. // LogOutput determines where we should send logs (if anywhere) and the log level.
func LogOutput() (logOutput io.Writer, err error) { func LogOutput() (logOutput io.Writer, err error) {
logOutput = ioutil.Discard logOutput = ioutil.Discard
envLevel := os.Getenv(EnvLog)
if envLevel == "" { logLevel := LogLevel()
if logLevel == "" {
return return
} }
@ -37,23 +38,38 @@ func LogOutput() (logOutput io.Writer, err error) {
} }
// This was the default since the beginning // This was the default since the beginning
logLevel := logutils.LogLevel("TRACE") logOutput = &logutils.LevelFilter{
Levels: validLevels,
MinLevel: logutils.LogLevel(logLevel),
Writer: logOutput,
}
return
}
// LogLevel returns the current log level string based the environment vars
func LogLevel() string {
envLevel := os.Getenv(EnvLog)
if envLevel == "" {
return ""
}
logLevel := "TRACE"
if isValidLogLevel(envLevel) { if isValidLogLevel(envLevel) {
// allow following for better ux: info, Info or INFO // allow following for better ux: info, Info or INFO
logLevel = logutils.LogLevel(strings.ToUpper(envLevel)) logLevel = strings.ToUpper(envLevel)
} else { } else {
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v", log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
envLevel, validLevels) envLevel, validLevels)
} }
logOutput = &logutils.LevelFilter{ return logLevel
Levels: validLevels, }
MinLevel: logLevel,
Writer: logOutput,
}
return // IsDebugOrHigher returns whether or not the current log level is debug or trace
func IsDebugOrHigher() bool {
level := string(LogLevel())
return level == "DEBUG" || level == "TRACE"
} }
func isValidLogLevel(level string) bool { func isValidLogLevel(level string) bool {