Accept TF_LOG=json to enable TRACE logs in JSON format

This is not currently a supported interface, but we plan to release
tool(s) that consume parts of it that are more dependable later,
separately from Terraform CLI itself.
This commit is contained in:
Paddy 2021-03-16 14:59:15 -07:00 committed by GitHub
parent acfa79b7b2
commit 0b5c4a6a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -81,7 +81,7 @@ func HCLogger() hclog.Logger {
// newHCLogger returns a new hclog.Logger instance with the given name // newHCLogger returns a new hclog.Logger instance with the given name
func newHCLogger(name string) hclog.Logger { func newHCLogger(name string) hclog.Logger {
logOutput := io.Writer(os.Stderr) logOutput := io.Writer(os.Stderr)
logLevel := globalLogLevel() logLevel, json := globalLogLevel()
if logPath := os.Getenv(envLogFile); logPath != "" { if logPath := os.Getenv(envLogFile); logPath != "" {
f, err := os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666) f, err := os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
@ -97,6 +97,7 @@ func newHCLogger(name string) hclog.Logger {
Level: logLevel, Level: logLevel,
Output: logOutput, Output: logOutput,
IndependentLevels: true, IndependentLevels: true,
JSONFormat: json,
}) })
} }
@ -127,7 +128,8 @@ func NewProviderLogger(prefix string) hclog.Logger {
// CurrentLogLevel returns the current log level string based the environment vars // CurrentLogLevel returns the current log level string based the environment vars
func CurrentLogLevel() string { func CurrentLogLevel() string {
return strings.ToUpper(globalLogLevel().String()) ll, _ := globalLogLevel()
return strings.ToUpper(ll.String())
} }
func providerLogLevel() hclog.Level { func providerLogLevel() hclog.Level {
@ -139,19 +141,25 @@ func providerLogLevel() hclog.Level {
return parseLogLevel(providerEnvLevel) return parseLogLevel(providerEnvLevel)
} }
func globalLogLevel() hclog.Level { func globalLogLevel() (hclog.Level, bool) {
var json bool
envLevel := strings.ToUpper(os.Getenv(envLog)) envLevel := strings.ToUpper(os.Getenv(envLog))
if envLevel == "" { if envLevel == "" {
envLevel = strings.ToUpper(os.Getenv(envLogCore)) envLevel = strings.ToUpper(os.Getenv(envLogCore))
} }
return parseLogLevel(envLevel) if envLevel == "JSON" {
json = true
}
return parseLogLevel(envLevel), json
} }
func parseLogLevel(envLevel string) hclog.Level { func parseLogLevel(envLevel string) hclog.Level {
if envLevel == "" { if envLevel == "" {
return hclog.Off return hclog.Off
} }
if envLevel == "JSON" {
envLevel = "TRACE"
}
logLevel := hclog.Trace logLevel := hclog.Trace
if isValidLogLevel(envLevel) { if isValidLogLevel(envLevel) {
@ -166,7 +174,7 @@ func parseLogLevel(envLevel string) hclog.Level {
// IsDebugOrHigher returns whether or not the current log level is debug or trace // IsDebugOrHigher returns whether or not the current log level is debug or trace
func IsDebugOrHigher() bool { func IsDebugOrHigher() bool {
level := globalLogLevel() level, _ := globalLogLevel()
return level == hclog.Debug || level == hclog.Trace return level == hclog.Debug || level == hclog.Trace
} }

View File

@ -12,6 +12,10 @@ Terraform has detailed logs which can be enabled by setting the `TF_LOG` environ
You can set `TF_LOG` to one of the log levels `TRACE`, `DEBUG`, `INFO`, `WARN` or `ERROR` to change the verbosity of the logs. You can set `TF_LOG` to one of the log levels `TRACE`, `DEBUG`, `INFO`, `WARN` or `ERROR` to change the verbosity of the logs.
Setting `TF_LOG` to `JSON` outputs logs at the `TRACE` level or higher, and uses a parseable JSON encoding as the formatting.
~> **Warning:** The JSON encoding of log files is not considered a stable interface. It may change at any time, without warning. It is meant to support tooling that will be forthcoming, and that tooling is the only supported way to interact with JSON formatted logs.
Logging can be enabled separately for terraform itself and the provider plugins Logging can be enabled separately for terraform itself and the provider plugins
using the `TF_LOG_CORE` or `TF_LOG_PROVIDER` environment variables. These take using the `TF_LOG_CORE` or `TF_LOG_PROVIDER` environment variables. These take
the same level arguments as `TF_LOG`, but only activate a subset of the logs. the same level arguments as `TF_LOG`, but only activate a subset of the logs.