11 "github.com/hashicorp/logutils"
14 // These are the environmental variables that determine if we log, and if
15 // we log whether or not the log should go to a file.
17 EnvLog = "TF_LOG" // Set to True
18 EnvLogFile = "TF_LOG_PATH" // Set to a file
21 var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
23 // LogOutput determines where we should send logs (if anywhere) and the log level.
24 func LogOutput() (logOutput io.Writer, err error) {
25 logOutput = ioutil.Discard
27 logLevel := LogLevel()
33 if logPath := os.Getenv(EnvLogFile); logPath != "" {
35 logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
41 // This was the default since the beginning
42 logOutput = &logutils.LevelFilter{
44 MinLevel: logutils.LogLevel(logLevel),
51 // SetOutput checks for a log destination with LogOutput, and calls
52 // log.SetOutput with the result. If LogOutput returns nil, SetOutput uses
53 // ioutil.Discard. Any error from LogOutout is fatal.
55 out, err := LogOutput()
67 // LogLevel returns the current log level string based the environment vars
68 func LogLevel() string {
69 envLevel := os.Getenv(EnvLog)
75 if isValidLogLevel(envLevel) {
76 // allow following for better ux: info, Info or INFO
77 logLevel = strings.ToUpper(envLevel)
79 log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
80 envLevel, validLevels)
86 // IsDebugOrHigher returns whether or not the current log level is debug or trace
87 func IsDebugOrHigher() bool {
88 level := string(LogLevel())
89 return level == "DEBUG" || level == "TRACE"
92 func isValidLogLevel(level string) bool {
93 for _, l := range validLevels {
94 if strings.ToUpper(level) == string(l) {