|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/hashicorp/logutils" |
| 12 | +) |
| 13 | + |
| 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. |
| 16 | +const ( |
| 17 | + EnvLog = "IAP_CURL_LOG" |
| 18 | + EnvLogFile = "IAP_CURL_LOG_PATH" |
| 19 | +) |
| 20 | + |
| 21 | +// ValidLevels is a list of valid log levels |
| 22 | +var ValidLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"} |
| 23 | + |
| 24 | +// LogOutput determines where we should send logs (if anywhere) and the log level. |
| 25 | +func LogOutput() (logOutput io.Writer, err error) { |
| 26 | + logOutput = ioutil.Discard |
| 27 | + |
| 28 | + logLevel := LogLevel() |
| 29 | + if logLevel == "" { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + logOutput = os.Stderr |
| 34 | + if logPath := os.Getenv(EnvLogFile); logPath != "" { |
| 35 | + var err error |
| 36 | + logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // This was the default since the beginning |
| 43 | + logOutput = &logutils.LevelFilter{ |
| 44 | + Levels: ValidLevels, |
| 45 | + MinLevel: logutils.LogLevel(logLevel), |
| 46 | + Writer: logOutput, |
| 47 | + } |
| 48 | + |
| 49 | + return |
| 50 | +} |
| 51 | + |
| 52 | +// SetOutput checks for a log destination with LogOutput, and calls |
| 53 | +// log.SetOutput with the result. If LogOutput returns nil, SetOutput uses |
| 54 | +// ioutil.Discard. Any error from LogOutout is fatal. |
| 55 | +func SetOutput() { |
| 56 | + out, err := LogOutput() |
| 57 | + if err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + if out == nil { |
| 62 | + out = ioutil.Discard |
| 63 | + } |
| 64 | + |
| 65 | + log.SetOutput(out) |
| 66 | +} |
| 67 | + |
| 68 | +// LogLevel returns the current log level string based the environment vars |
| 69 | +func LogLevel() string { |
| 70 | + envLevel := os.Getenv(EnvLog) |
| 71 | + if envLevel == "" { |
| 72 | + return "" |
| 73 | + } |
| 74 | + |
| 75 | + logLevel := "TRACE" |
| 76 | + if isValidLogLevel(envLevel) { |
| 77 | + // allow following for better ux: info, Info or INFO |
| 78 | + logLevel = strings.ToUpper(envLevel) |
| 79 | + } else { |
| 80 | + log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v", |
| 81 | + envLevel, ValidLevels) |
| 82 | + } |
| 83 | + |
| 84 | + return logLevel |
| 85 | +} |
| 86 | + |
| 87 | +// IsDebugOrHigher returns whether or not the current log level is debug or trace |
| 88 | +func IsDebugOrHigher() bool { |
| 89 | + level := string(LogLevel()) |
| 90 | + return level == "DEBUG" || level == "TRACE" |
| 91 | +} |
| 92 | + |
| 93 | +func isValidLogLevel(level string) bool { |
| 94 | + for _, l := range ValidLevels { |
| 95 | + if strings.ToUpper(level) == string(l) { |
| 96 | + return true |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return false |
| 101 | +} |
0 commit comments