Add logging

This commit is contained in:
Ian Howell 2019-05-03 13:20:38 -05:00
parent c875ad0c9f
commit 7e2609d1e8
5 changed files with 147 additions and 0 deletions
cmd
root.go
testdata/TestRootGoldenOutput
pkg

@ -7,6 +7,7 @@ import (
"github.com/ian-howell/airshipadm/pkg/environment"
"github.com/ian-howell/airshipadm/pkg/kube"
"github.com/ian-howell/airshipadm/pkg/log"
"github.com/spf13/cobra"
)
@ -23,11 +24,14 @@ func NewRootCmd(out io.Writer, client *kube.Client, args []string) *cobra.Comman
// Settings flags - This section should probably be moved to pkg/environment
rootCmd.PersistentFlags().StringVar(&settings.KubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
rootCmd.PersistentFlags().BoolVar(&settings.Debug, "debug", false, "enable verbose output")
// TODO(howell): Remove this panic
if err := rootCmd.PersistentFlags().Parse(args); err != nil {
panic(err.Error())
}
log.Init(&settings, out)
rootCmd.AddCommand(NewVersionCommand(out, client))
// Compound commands

@ -8,6 +8,7 @@ Available Commands:
version Show the version number of airshipadm and its underlying tools
Flags:
--debug enable verbose output
-h, --help help for airshipadm
--kubeconfig string path to kubeconfig

@ -6,4 +6,7 @@ type AirshipADMSettings struct {
// This flag is only needed when airshipadm is being used
// out-of-cluster
KubeConfigFilePath string
// Debug is used for verbose output
Debug bool
}

50
pkg/log/log.go Normal file

@ -0,0 +1,50 @@
package log
import (
"io"
"log"
"github.com/ian-howell/airshipadm/pkg/environment"
)
var debug = false
// Init initializes settings related to logging
func Init(settings *environment.AirshipADMSettings, out io.Writer) {
debug = settings.Debug
log.SetOutput(out)
}
// Debug is a wrapper for log.Debug
func Debug(v ...interface{}) {
if debug {
log.Print(v...)
}
}
// Debugf is a wrapper for log.Debugf
func Debugf(format string, v ...interface{}) {
if debug {
log.Printf(format, v...)
}
}
// Print is a wrapper for log.Print
func Print(v ...interface{}) {
log.Print(v...)
}
// Printf is a wrapper for log.Printf
func Printf(format string, v ...interface{}) {
log.Printf(format, v...)
}
// Fatal is a wrapper for log.Fatal
func Fatal(v ...interface{}) {
log.Fatal(v...)
}
// Fatalf is a wrapper for log.Fatalf
func Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}

89
pkg/log/log_test.go Normal file

@ -0,0 +1,89 @@
package log_test
import (
"bytes"
"strings"
"testing"
"github.com/ian-howell/airshipadm/pkg/environment"
"github.com/ian-howell/airshipadm/pkg/log"
)
const notEqualFmt = `Output does not match expected
GOT: %v
Expected: %v`
func TestLoggingWithoutDebug(t *testing.T) {
settings := environment.AirshipADMSettings{
Debug: false,
}
output := new(bytes.Buffer)
log.Init(&settings, output)
log.Print("Print test - debug false")
expected := "Print test - debug false"
outputFields := strings.Fields(output.String())
if len(outputFields) < 3 {
t.Fatalf("Expected log message to have the following format: YYYY/MM/DD HH:MM:SS Message")
}
outputMessage := strings.Join(outputFields[2:], " ")
if outputMessage != expected {
t.Errorf(notEqualFmt, outputMessage, expected)
}
output.Reset()
log.Printf("%s - %s", "Printf test", "debug false")
expected = "Printf test - debug false"
outputFields = strings.Fields(output.String())
if len(outputFields) < 3 {
t.Fatalf("Expected log message to have the following format: YYYY/MM/DD HH:MM:SS Message")
}
outputMessage = strings.Join(outputFields[2:], " ")
if outputMessage != expected {
t.Errorf(notEqualFmt, outputMessage, expected)
}
output.Reset()
log.Debug("Debug test - debug false")
log.Debugf("%s - %s", "Debugf test", "debug false")
if len(output.Bytes()) > 0 {
t.Errorf("Unexpected output: %s", output)
}
}
func TestLoggingWithDebug(t *testing.T) {
settings := environment.AirshipADMSettings{
Debug: true,
}
output := new(bytes.Buffer)
log.Init(&settings, output)
log.Debug("Debug test - debug true")
expected := "Debug test - debug true"
outputFields := strings.Fields(output.String())
if len(outputFields) < 3 {
t.Fatalf("Expected log message to have the following format: YYYY/MM/DD HH:MM:SS Message")
}
outputMessage := strings.Join(outputFields[2:], " ")
if outputMessage != expected {
t.Errorf(notEqualFmt, outputMessage, expected)
}
output.Reset()
log.Debugf("%s - %s", "Debugf test", "debug true")
expected = "Debugf test - debug true"
outputFields = strings.Fields(output.String())
if len(outputFields) < 3 {
t.Fatalf("Expected log message to have the following format: YYYY/MM/DD HH:MM:SS Message")
}
outputMessage = strings.Join(outputFields[2:], " ")
if outputMessage != expected {
t.Errorf(notEqualFmt, outputMessage, expected)
}
}