Implement logging
In this tutorial, you will implement log messages in your provider and filter special values from the log output. Then you will manage log output to view those log statements when executing Terraform. To do this, you will:
- Add log messages.
This creates provider-defined log messages in Terraform's logs. - Add structured log fields.
This enhances logging data with provider-defined key-value pairs for greater consistency across multiple logs and easier log viewing. - Add log filtering.
This redacts certain log messages or structured log field data from being included in the log output. - View all Terraform log output during commands.
This shows all Terraform logs in the terminal running a Terraform command. - Save Terraform log output to a file during commands.
This saves all Terraform logs to a file when running a Terraform command. - View specific Terraform log output.
This manages Terraform log output to show only certain logs.
Prerequisites
To follow this tutorial, you need:
- Go 1.21+ installed and configured.
- Terraform v1.8+ installed locally.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory.
Your code should match the 03-read-coffees
directory
from the example repository.
Implement log messages
Providers support logging through the tflog
package of the github.com/hashicorp/terraform-plugin-log
Go module. This package implements structured logging and filtering capabilities.
Open the internal/provider/provider.go
file.
Update the top of the Configure
method logic with the following.
Replace the import
statement at the beginning of the file with the following.
Implement structured log fields
The tflog
package supports adding additional key-value pairs to logging for consistency and tracing flow. These pairs can be added for the rest of the provider request with the tflog.SetField()
call or inline as a final parameter with any logging calls.
Open the internal/provider/provider.go
file.
Inside your provider's Configure
method, set three logging fields and a log
message immediately before the hashicups.NewClient()
call with the following.
Add a log message at the end of the Configure
method with the following.
Implement log filtering
Add a filter to mask the user's password before the tflog.Debug(ctx, "Creating
HashiCups client")
call in the Configure
method with the following.
Build and install the updated provider.
View all Terraform log output
Log output from Terraform is controlled by various environment variables, such as TF_LOG
or otherwise prefixed with TF_LOG_
.
Navigate to the examples/coffees
directory.
Run a Terraform plan with the TF_LOG
environment variable set to TRACE
.
Terraform will output a large amount of log entries for all components within Terraform itself, the Terraform Plugin Framework, and any provider logging.
Save all Terraform log output
Viewing log output inline with the running command is helpful if you plan on looking through the logs in the same terminal session or want to pipe the output to other shell commands, such as grep
. Terraform can instead write this output to a log file on the local filesystem for opening in text editors or for archiving purposes.
Run a Terraform plan with both the TF_LOG
and TF_LOG_PATH
environment variables set.
Open the examples/coffees/trace.txt
file and verify it contains the log
messages you added to your provider's Configure
method.
Remove the examples/coffees/trace.txt
file.
View specific Terraform log output
The previous examples used the TRACE
logging level. Trace logs are the most verbose level of logging available and may contain an overwhelming amount of information that is only relevant if you deeply understand certain internal components of Terraform or the Terraform Plugin Framework. You can instead lower the logging level to DEBUG
, INFO
, WARN
, or ERROR
.
Run a Terraform plan with the TF_LOG
environment variable set to INFO
.
You can enable log output for certain components, such as only provider logs and not Terraform CLI logs.
Run a Terraform plan with the TF_LOG_PROVIDER
environment variable set to INFO
.
Navigate to the terraform-provider-hashicups
directory.
Next steps
Congratulations! You have implemented logging in the provider, viewed it, and refined the output.
If you were stuck during this tutorial, checkout the
04-logging
directory in the example repository to see the code implemented in this
tutorial.
- To learn more about the Terraform Plugin Framework, refer to the Terraform Plugin Framework documentation.
- For a full capability comparison between the SDKv2 and the Plugin Framework, refer to the Which SDK Should I Use? documentation.
- The example repository contains directories corresponding to each tutorial in this collection.
- Submit any Terraform Plugin Framework bug reports or feature requests to the development team in the Terraform Plugin Framework Github repository.
- Submit any Terraform Plugin Framework questions in the Terraform Plugin Framework Discuss forum.