Skip to content

Commit

Permalink
tfexec: Add (Terraform).SetLog() method
Browse files Browse the repository at this point in the history
Reference: #290
  • Loading branch information
bflad committed Apr 13, 2022
1 parent 0cf3a0c commit 10402aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
3 changes: 1 addition & 2 deletions tfexec/cmd.go
Expand Up @@ -149,8 +149,7 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
env[logPathEnvVar] = ""
} else {
env[logPathEnvVar] = tf.logPath
// Log levels other than TRACE are currently unreliable, the CLI recommends using TRACE only.
env[logEnvVar] = "TRACE"
env[logEnvVar] = tf.log
}

// constant automation override env vars
Expand Down
30 changes: 27 additions & 3 deletions tfexec/terraform.go
Expand Up @@ -48,9 +48,14 @@ type Terraform struct {
skipProviderVerify bool
env map[string]string

stdout io.Writer
stderr io.Writer
logger printfer
stdout io.Writer
stderr io.Writer
logger printfer

// TF_LOG environment variable, defaults to TRACE if logPath is set.
log string

// TF_LOG_PATH environment variable
logPath string

versionLock sync.Mutex
Expand Down Expand Up @@ -122,10 +127,29 @@ func (tf *Terraform) SetStderr(w io.Writer) {
tf.stderr = w
}

// SetLog sets the TF_LOG environment variable for Terraform CLI execution.
// This must be combined with a call to SetLogPath to take effect.
//
// This is only compatible with Terraform CLI 0.15.0 or later as setting the
// log level was unreliable in earlier versions. It will default to TRACE for
// those earlier versions when SetLogPath is called.
func (tf *Terraform) SetLog(log string) error {
err := tf.compatible(context.Background(), nil, tf0_15_0)
if err != nil {
return err
}
tf.log = log
return nil
}

// SetLogPath sets the TF_LOG_PATH environment variable for Terraform CLI
// execution.
func (tf *Terraform) SetLogPath(path string) error {
tf.logPath = path
// Prevent setting the log path without enabling logging
if tf.log == "" {
tf.log = "TRACE"
}
return nil
}

Expand Down

0 comments on commit 10402aa

Please sign in to comment.