Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 12, 2019
1 parent b6c4b6f commit 47244ea
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
101 changes: 101 additions & 0 deletions logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"io"
"io/ioutil"
"log"
"os"
"strings"
"syscall"

"github.com/hashicorp/logutils"
)

// These are the environmental variables that determine if we log, and if
// we log whether or not the log should go to a file.
const (
EnvLog = "IAP_CURL_LOG"
EnvLogFile = "IAP_CURL_LOG_PATH"
)

// ValidLevels is a list of valid log levels
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) {
logOutput = ioutil.Discard

logLevel := LogLevel()
if logLevel == "" {
return
}

logOutput = os.Stderr
if logPath := os.Getenv(EnvLogFile); logPath != "" {
var err error
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
if err != nil {
return nil, err
}
}

// This was the default since the beginning
logOutput = &logutils.LevelFilter{
Levels: ValidLevels,
MinLevel: logutils.LogLevel(logLevel),
Writer: logOutput,
}

return
}

// SetOutput checks for a log destination with LogOutput, and calls
// log.SetOutput with the result. If LogOutput returns nil, SetOutput uses
// ioutil.Discard. Any error from LogOutout is fatal.
func SetOutput() {
out, err := LogOutput()
if err != nil {
log.Fatal(err)
}

if out == nil {
out = ioutil.Discard
}

log.SetOutput(out)
}

// LogLevel returns the current log level string based the environment vars
func LogLevel() string {
envLevel := os.Getenv(EnvLog)
if envLevel == "" {
return ""
}

logLevel := "TRACE"
if isValidLogLevel(envLevel) {
// allow following for better ux: info, Info or INFO
logLevel = strings.ToUpper(envLevel)
} else {
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
envLevel, ValidLevels)
}

return logLevel
}

// IsDebugOrHigher returns whether or not the current log level is debug or trace
func IsDebugOrHigher() bool {
level := string(LogLevel())
return level == "DEBUG" || level == "TRACE"
}

func isValidLogLevel(level string) bool {
for _, l := range ValidLevels {
if strings.ToUpper(level) == string(l) {
return true
}
}

return false
}
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -55,13 +56,22 @@ func main() {
}

func newCLI(args []string) CLI {
logWriter, err := LogOutput()
if err != nil {
panic(err)
}
log.SetOutput(logWriter)

var c CLI

c.stdout = os.Stdout
c.stderr = os.Stderr

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

for _, arg := range args {
switch arg {
Expand Down Expand Up @@ -159,14 +169,13 @@ func (c CLI) run() int {
args = append(args, c.args...)
args = append(args, url)

log.Printf("[TRACE] args: %#v\n", args)
log.Printf("[TRACE] env: %#v\n", env)

s := newShell(env.Binary, args)
return c.exit(s.run())
}

func (c CLI) debug(a ...interface{}) {
fmt.Fprint(c.stderr, a...)
}

func (c CLI) getURL() string {
if len(c.urls) == 0 {
return ""
Expand Down

0 comments on commit 47244ea

Please sign in to comment.