Search code examples
terraformterraform-provider-gcp

What're the advantages of using tflog package over log package for logging?


Context: I'm developing a TF Provider and I could see the latest "Writing Log Output" doc from HashiCorp where they recommend using tflog package for logging.

That said, I can see TF Provider for GCP are still using log package. What're the advantages of using tflog over log?


Solution

  • The Structured Logging section of the documentation you linked describes the authors' justification for recommending this different logging strategy:

    The tflog package uses structured logging, based on go-hclog. Rather than writing logs as sentences with embedded variables and values, tflog takes a sentence describing the logging event and a set of variables to log. When variables 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 variables with downstream log output.

    Although not mentioned explicitly as an advantage in the documentation, it does also mention that tflog has a notion of log levels, and there's no corresponding concept in the standard library log package at the time of writing.

    Given that, I would conclude that the two intended advantages of tflog over standard library log are:

    • tflog uses a structured logging approach where the separate variables in the result are machine-parsable and therefore amenable to automated filtering via scripts.
    • tflog associates a log level with each message, and the SDKs allow customizing the log level for a particular execution to control the amount of output.

    I think getting any further context on this would require asking the authors of the SDKs, since this is a subjective design tradeoff rather than a situation where there is one clear correct answer.

    I assume that some existing providers continue to use standard library log just because that code was written before tflog existed. tflog v0.2.0 (apparently the first publicly-published version) was released in December 2021, whereas big Terraform providers like the Google Cloud Platform provider have been under development for almost a decade before that.