terraform-plugin-log version
Relevant provider source code
tflog.Trace(ctx, "message with %d string format parameter", 1)
Plus other logging functions in tflog and tfsdklog. Also affects With() functions.
Expected Behavior
The potentially expected behavior here is that the logging functions such as Debug and friends act like f suffix string formatting functions since they accept a final ...interface{} variadic argument. The package was written to encourage structured logging with key-value pairs instead. Since ...interface{} does not enforce the key-value pair intention, the expected behavior here is then that the coding issue should be caught at compile-time or other feedback options are provided (e.g. static analysis tooling to report unpaired key-values issues).
Actual Behavior
The logger will still log the message, but the raw formatting parameter shows up in the message and the unpaired key should show in the EXTRA_VALUE_AT_END field.
Proposal
We should consider switching the variadic parameter to ...map[string]interface{} to enforce the key-value pairing, while still allowing simple logging messages. In the case of multiple maps, overwrite any previously defined keys.
Another, less preferable option, would be to offer a go/analysis based static analysis package and installable command, so downstream implementations can catch this coding issue (e.g. during CI).
A non-preferable option would be to leave the existing functions and offer f suffix functions, e.g. Debugf(), that do act similar to other formatter functions. This goes against the package's encouragement of structured logging.
References
terraform-plugin-log version
Relevant provider source code
Plus other logging functions in
tflogandtfsdklog. Also affectsWith()functions.Expected Behavior
The potentially expected behavior here is that the logging functions such as
Debugand friends act likefsuffix string formatting functions since they accept a final...interface{}variadic argument. The package was written to encourage structured logging with key-value pairs instead. Since...interface{}does not enforce the key-value pair intention, the expected behavior here is then that the coding issue should be caught at compile-time or other feedback options are provided (e.g. static analysis tooling to report unpaired key-values issues).Actual Behavior
The logger will still log the message, but the raw formatting parameter shows up in the message and the unpaired key should show in the
EXTRA_VALUE_AT_ENDfield.Proposal
We should consider switching the variadic parameter to
...map[string]interface{}to enforce the key-value pairing, while still allowing simple logging messages. In the case of multiple maps, overwrite any previously defined keys.Another, less preferable option, would be to offer a
go/analysisbased static analysis package and installable command, so downstream implementations can catch this coding issue (e.g. during CI).A non-preferable option would be to leave the existing functions and offer
fsuffix functions, e.g.Debugf(), that do act similar to other formatter functions. This goes against the package's encouragement of structured logging.References