Skip to content

Commit 47244ea

Browse files
committed
Add logging
1 parent b6c4b6f commit 47244ea

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

logging.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"log"
78
"net/url"
89
"os"
910
"os/exec"
@@ -55,13 +56,22 @@ func main() {
5556
}
5657

5758
func newCLI(args []string) CLI {
59+
logWriter, err := LogOutput()
60+
if err != nil {
61+
panic(err)
62+
}
63+
log.SetOutput(logWriter)
64+
5865
var c CLI
5966

6067
c.stdout = os.Stdout
6168
c.stderr = os.Stderr
6269

6370
// Do not handle error
64-
c.cfg.Load()
71+
err = c.cfg.Load()
72+
if err != nil {
73+
log.Printf("[WARN] Load returns error but don't stop: %v\n", err)
74+
}
6575

6676
for _, arg := range args {
6777
switch arg {
@@ -159,14 +169,13 @@ func (c CLI) run() int {
159169
args = append(args, c.args...)
160170
args = append(args, url)
161171

172+
log.Printf("[TRACE] args: %#v\n", args)
173+
log.Printf("[TRACE] env: %#v\n", env)
174+
162175
s := newShell(env.Binary, args)
163176
return c.exit(s.run())
164177
}
165178

166-
func (c CLI) debug(a ...interface{}) {
167-
fmt.Fprint(c.stderr, a...)
168-
}
169-
170179
func (c CLI) getURL() string {
171180
if len(c.urls) == 0 {
172181
return ""

0 commit comments

Comments
 (0)