Writing Log Output
Use the tflog
package to write logs for your provider. SDKs like the terraform-plugin-framework
, terraform-plugin-go
, and terraform-plugin-sdk/v2
set up logging for you, so you only need to write the logs themselves. You can write log output at varying verbosity levels, add fields to logs, and create subsystems to group logs that relate to distinct sections of code (e.g., the API client).
Warning: Do not use fmt.Println
and similar methods to log to standard output (stdout). They will not generate visible output in older versions of Terraform and are truncated in recent versions of Terraform.
Structured Logging
The tflog
package uses structured logging, based on go-hclog
. Rather than writing logs as sentences with embedded fields and values, tflog
takes a sentence describing the logging event and a set of fields to log. When fields are separate from the log description, you can use them to programmatically parse, filter, and search log output. This separation also allows other parts of the system to associate fields with downstream log output.
Requirements
One of the following SDK versions is required to properly output structured logs with the tflog
package:
terraform-plugin-framework
version 0.6.0 or laterterraform-plugin-sdk
version 2.10.0 or later
All calls to tflog
package functionality must use an SDK provided context.Context
, which stores the logging implementation. Every terraform-plugin-framework
method implemented by providers automatically includes the correct context.Context
. Providers written with terraform-plugin-sdk
must use context-aware functionality, such as the helper/schema.Resource
type ReadContext
field.
Follow the legacy logging instructions to write logs with older SDK versions or when context-aware functionality is not available, for instance SchemaStateFunc.
Log Levels
You must choose a verbosity level for each line of log output. This lets consumers specify a type of log output to write from your provider. For example, you can use environment fields to set your provider to write only logs of type Warn
during a Terraform run.
Error
The least verbose output that typically describes an unexpected condition prior to halting execution. It often provides more information about a user-facing error.
Warn
Output that describes an unexpected condition, but not one that should halt execution. It often includes deprecations or environment issues.
Info
Output that describes a certain logic condition or event. It often includes details about the environment your provider is running in or how it has been configured to run.
Debug
Verbose output that typically describes important operational details like milestones in logic. It often describes behaviors that may confusing even though they are correct.
Trace
The most verbose output that describes the lowest level operational details, such as intra-function steps or raw data.
Fields
Use fields to attach information to specific logs or to an entire logger, which adds that information to all subsequent logs. You can also combine both methods to simplify your logging code.
Single Log Fields
To specify filterable fields in the log output, add a map of additional fields after the log message.
The following example adds both a URL and a method for an API request.
You can also use other standard Go types for values.
Multiple Log Fields
Loggers are transported using a context.Context
type, so injecting a field into a logger returns a new context.Context
containing the modified logger. Subsequent calls to tflog
with that logger will implicitly include the field.
Use tflog.SetField()
to attach fields to a logger.
You can also conditionally attach the fields by creating a new context and injecting it into the logger.
Subsystems
You can create a subsystem to manage loggers for sections of code that are large, complex, or have distinct functionality. You can then configure environment fields that allow each subsystem to be included or excluded from log output. For example, you may want to create a subsystem for logs that relate to the API client so that you can turn them off when when debugging an unrelated issue.
Create Subsystems
To create a new subsystem, pass context and the subsystem name to the NewSubsystem()
method.
Optionally, specify a log level for the subsystem.
You can also create an environment field to control the logging level instead of hardcoding it into the subsystem.
Use Subsystems
Logging or adding fields to subsystem loggers requires separate function calls for each log level:
tflog.SubsystemError()
: Equivalent totflog.Error()
, but using a subsystem logger.tflog.SubsystemWarn()
: Equivalent totflog.Warn()
, but using a subsystem logger.tflog.SubsystemInfo()
: Equivalent totflog.Info()
, but using a subsystem logger.tflog.SubsystemDebug()
: Equivalent totflog.Debug()
, but using a subsystem logger.tflog.SubsystemTrace()
: Equivalent totflog.Trace()
, but using a subsystem logger.tflog.SubsystemSetField()
: Equivalent totflog.SetField()
, but using a subsystem logger.
For example, use tflog.SubsystemDebug()
to write a debug level log with a specific subsystem.
Use tflog.SubsystemSetField()
to attach fields to a specific subsystem.
You can also conditionally attach fields by creating a new context and injecting it into the logger.
Legacy Logging
Providers on older SDK versions should write logs via the Go standard library log
package. Only a log level and message are supported. There is no support for fields, filtering, or subsystems.
Legacy Log Levels
You must choose a verbosity level for each line of log output. This lets consumers specify a type of log output to write from your provider. For example, you can use environment fields to set your provider to write only logs at the warning or higher level during a Terraform run.
Terraform has specific message formatting requirements to properly set the log level when using the log
package:
Error
The least verbose output that typically describes an unexpected condition prior to halting execution. It often provides more information about a user-facing error.
Warn
Output that describes an unexpected condition, but not one that should halt execution. It often includes deprecations or environment issues.
Info
Output that describes a certain logic condition or event. It often includes details about the environment your provider is running in or how it has been configured to run.
Debug
Verbose output that typically describes important operational details like milestones in logic. It often describes behaviors that may confusing even though they are correct.
Trace
The most verbose output that describes the lowest level operational details, such as intra-function steps or raw data.
Legacy Log Troubleshooting
Duplicate Timestamp and Incorrect Level Messages
Using the log
package for logging can generate messages that look like the following in the output from Terraform:
Resolve this by adjusting the log
package to not include a timestamp prefix via the log.SetFlags()
function before the provider server starts.
This example shows an updated main.go
implementation for a terraform-plugin-sdk
based provider:
Also ensure the log.SetPrefix()
function is not being used, as log messages must start with the [LEVEL]
prefix.