2015-12-08 01:10:30 +01:00
|
|
|
package logging
|
2014-05-31 01:07:26 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2015-12-08 01:10:30 +01:00
|
|
|
"io/ioutil"
|
2015-10-01 21:45:12 +02:00
|
|
|
"log"
|
2014-05-31 01:07:26 +02:00
|
|
|
"os"
|
2015-10-01 21:45:12 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/logutils"
|
2014-05-31 01:07:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// These are the environmental variables that determine if we log, and if
|
|
|
|
// we log whether or not the log should go to a file.
|
2015-10-01 21:45:12 +02:00
|
|
|
const (
|
|
|
|
EnvLog = "TF_LOG" // Set to True
|
|
|
|
EnvLogFile = "TF_LOG_PATH" // Set to a file
|
|
|
|
)
|
2014-05-31 01:07:26 +02:00
|
|
|
|
2015-10-01 21:45:12 +02:00
|
|
|
var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
|
|
|
|
|
2015-12-08 01:10:30 +01:00
|
|
|
// LogOutput determines where we should send logs (if anywhere) and the log level.
|
|
|
|
func LogOutput() (logOutput io.Writer, err error) {
|
|
|
|
logOutput = ioutil.Discard
|
2016-03-14 17:17:05 +01:00
|
|
|
|
|
|
|
logLevel := LogLevel()
|
|
|
|
if logLevel == "" {
|
2015-10-01 21:45:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOutput = os.Stderr
|
|
|
|
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
|
|
|
var err error
|
|
|
|
logOutput, err = os.Create(logPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-31 01:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 21:45:12 +02:00
|
|
|
// This was the default since the beginning
|
2016-03-14 17:17:05 +01:00
|
|
|
logOutput = &logutils.LevelFilter{
|
|
|
|
Levels: validLevels,
|
|
|
|
MinLevel: logutils.LogLevel(logLevel),
|
|
|
|
Writer: logOutput,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-10-01 21:45:12 +02:00
|
|
|
|
2016-03-14 17:17:05 +01:00
|
|
|
// LogLevel returns the current log level string based the environment vars
|
|
|
|
func LogLevel() string {
|
|
|
|
envLevel := os.Getenv(EnvLog)
|
|
|
|
if envLevel == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
logLevel := "TRACE"
|
2015-10-01 21:45:12 +02:00
|
|
|
if isValidLogLevel(envLevel) {
|
|
|
|
// allow following for better ux: info, Info or INFO
|
2016-03-14 17:17:05 +01:00
|
|
|
logLevel = strings.ToUpper(envLevel)
|
2015-10-01 21:45:12 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
|
|
|
|
envLevel, validLevels)
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:17:05 +01:00
|
|
|
return logLevel
|
|
|
|
}
|
2015-10-01 21:45:12 +02:00
|
|
|
|
2016-03-14 17:17:05 +01:00
|
|
|
// IsDebugOrHigher returns whether or not the current log level is debug or trace
|
|
|
|
func IsDebugOrHigher() bool {
|
|
|
|
level := string(LogLevel())
|
|
|
|
return level == "DEBUG" || level == "TRACE"
|
2014-05-31 01:07:26 +02:00
|
|
|
}
|
2015-10-01 21:45:12 +02:00
|
|
|
|
|
|
|
func isValidLogLevel(level string) bool {
|
|
|
|
for _, l := range validLevels {
|
|
|
|
if strings.ToUpper(level) == string(l) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|