log: support for user defined log levels
This PR brings support for better log level handling. Terraform logs are already written in the form which can be understood by the module https://github.com/hashicorp/logutils . The TF_LOG environment variable now accepts a log level. Users can pass levels in the form of "info", "Info" or "INFO" If an invalid log level is passed, we print a warning but still continue, for backward compatibility with the old TF_LOG=1 style.
This commit is contained in:
parent
8f237a7256
commit
0090c063e8
59
log.go
59
log.go
|
@ -2,28 +2,65 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are the environmental variables that determine if we log, and if
|
// These are the environmental variables that determine if we log, and if
|
||||||
// we log whether or not the log should go to a file.
|
// we log whether or not the log should go to a file.
|
||||||
const EnvLog = "TF_LOG" //Set to True
|
const (
|
||||||
const EnvLogFile = "TF_LOG_PATH" //Set to a file
|
EnvLog = "TF_LOG" // Set to True
|
||||||
|
EnvLogFile = "TF_LOG_PATH" // Set to a file
|
||||||
|
)
|
||||||
|
|
||||||
// logOutput determines where we should send logs (if anywhere).
|
var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
|
||||||
|
|
||||||
|
// logOutput determines where we should send logs (if anywhere) and the log level.
|
||||||
func logOutput() (logOutput io.Writer, err error) {
|
func logOutput() (logOutput io.Writer, err error) {
|
||||||
logOutput = nil
|
logOutput = nil
|
||||||
if os.Getenv(EnvLog) != "" {
|
envLevel := os.Getenv(EnvLog)
|
||||||
logOutput = os.Stderr
|
if envLevel == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
logOutput = os.Stderr
|
||||||
var err error
|
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
||||||
logOutput, err = os.Create(logPath)
|
var err error
|
||||||
if err != nil {
|
logOutput, err = os.Create(logPath)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This was the default since the beginning
|
||||||
|
logLevel := logutils.LogLevel("TRACE")
|
||||||
|
|
||||||
|
if isValidLogLevel(envLevel) {
|
||||||
|
// allow following for better ux: info, Info or INFO
|
||||||
|
logLevel = logutils.LogLevel(strings.ToUpper(envLevel))
|
||||||
|
} else {
|
||||||
|
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
|
||||||
|
envLevel, validLevels)
|
||||||
|
}
|
||||||
|
|
||||||
|
logOutput = &logutils.LevelFilter{
|
||||||
|
Levels: validLevels,
|
||||||
|
MinLevel: logLevel,
|
||||||
|
Writer: logOutput,
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidLogLevel(level string) bool {
|
||||||
|
for _, l := range validLevels {
|
||||||
|
if strings.ToUpper(level) == string(l) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue