Redirect TF_LOG logs into a file

3 min read | by Jordi Prats

When we enable tracing terraform using TF_LOG the output can be overwhelmingly hard to read due to the amount of info it's printed. Using TF_LOG_PATH we can tell terraform to save all the debug info to a file so we can see the usual output and be able to check the logs later on

We only need to set the file where we want to store the debug traces as an environment variable as follows:

$ TF_LOG=trace TF_LOG_PATH="./trace.log" terraform plan Acquiring state lock. This may take a few moments... (...) Plan: 9 to add, 2 to change, 0 to destroy. Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. Releasing state lock. This may take a few moments... 

The terraform output will look as usual, but it will create the file we have told it to use, being able to check it once it has finished:

cat ./trace.log 2021-09-19T23:32:50.703+0200 [DEBUG] Adding temp file log sink: /tmp/terraform-log514864094 2021-09-19T23:32:50.703+0200 [INFO] Terraform version: 1.0.5 2021-09-19T23:32:50.703+0200 [INFO] Go runtime version: go1.16.4 2021-09-19T23:32:50.703+0200 [INFO] CLI args: []string{"/home/pet2cattle/.tfenv/versions/1.0.5/terraform", "plan"} 2021-09-19T23:32:50.703+0200 [TRACE] Stdout is a terminal of width 211 2021-09-19T23:32:50.703+0200 [TRACE] Stderr is a terminal of width 211 2021-09-19T23:32:50.703+0200 [TRACE] Stdin is a terminal 2021-09-19T23:32:50.703+0200 [DEBUG] Attempting to open CLI config file: /home/pet2cattle/.terraformrc 2021-09-19T23:32:50.703+0200 [DEBUG] File doesn't exist, but doesn't need to. Ignoring. 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /home/pet2cattle/.terraform.d/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /home/pet2cattle/.local/share/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /usr/share/xfce4/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /usr/share/xfce/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /var/lib/snapd/desktop/terraform/plugins 2021-09-19T23:32:50.703+0200 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins 2021-09-19T23:32:50.704+0200 [INFO] CLI command args: []string{"plan"} 2021-09-19T23:32:50.705+0200 [TRACE] Meta.Backend: built configuration for "s3" backend with hash value 1465895727 2021-09-19T23:32:50.705+0200 [TRACE] Preserving existing state lineage "85261a2b-e1b4-a938-6caf-9c4b7baa041c" 2021-09-19T23:32:50.705+0200 [TRACE] Preserving existing state lineage "85261a2b-e1b4-a938-6caf-9c4b7baa041c" 2021-09-19T23:32:50.706+0200 [TRACE] Meta.Backend: working directory was previously initialized for "s3" backend 2021-09-19T23:32:50.706+0200 [TRACE] Meta.Backend: using already-initialized, unchanged "s3" backend configuration 2021-09-19T23:32:50.707+0200 [INFO] AWS Auth provider used: "SharedCredentialsProvider" 2021-09-19T23:32:50.707+0200 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021-09-19T23:32:50.707+0200 [DEBUG] [aws-sdk-go] DEBUG: Request sts/GetCallerIdentity Details: ---[ REQUEST POST-SIGN ]----------------------------- POST / HTTP/1.1 Host: sts.amazonaws.com User-Agent: aws-sdk-go/1.37.0 (go1.16.4; linux; amd64) APN/1.0 HashiCorp/1.0 Terraform/1.0.5 (...) 

Posted on 05/10/2021

Categories