Skip to content

Commit

Permalink
Merge pull request #690 from bmeneguele/fix-log
Browse files Browse the repository at this point in the history
Fix logging functions and add tests
  • Loading branch information
bmeneg authored Jun 15, 2021
2 parents 90a6533 + b1c4a74 commit efa4be2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (l *logger) SetStdDest(stdout io.Writer, stderr io.Writer) {
// printKeysAndValues prints the keys and valus, as pairs, passed to those
// functions in the way expected by go-retryablehttp LeveledLogger interface
func printKeysAndValues(l *log.Logger, keysAndValues ...interface{}) {
for i := 0; i <= len(keysAndValues)/2; i += 2 {
for i := 0; i < len(keysAndValues)/2; i += 2 {
l.Printf("\t%s = %s\n", keysAndValues[i], keysAndValues[i+1])
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func addFileLinePrefix(msg string) string {

// Fatal prints the values and exit the program with os.Exit()
func (l *logger) Fatal(values ...interface{}) {
values = append([]interface{}{addFileLinePrefix("")}, values...)
values = append([]interface{}{addFileLinePrefix(" ")}, values...)
l.errorLogger.Fatal(values...)
}

Expand All @@ -128,7 +128,7 @@ func (l *logger) Fatalf(format string, values ...interface{}) {

// Fatal prints the values in a new line and exit the program with os.Exit()
func (l *logger) Fatalln(values ...interface{}) {
values = append([]interface{}{addFileLinePrefix("")}, values...)
values = append([]interface{}{addFileLinePrefix(" ")}, values...)
l.errorLogger.Fatalln(values...)
}

Expand Down
82 changes: 82 additions & 0 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package logger

import (
"bytes"
"io"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -37,3 +40,82 @@ func Test_addFileLinePrefix(t *testing.T) {
regex := regexp.MustCompile("testing.go:[0-9]+: test")
require.Regexp(t, regex, msg)
}

func TestLogFunctions(t *testing.T) {
type logFunc func(string, ...interface{})
type logFuncf func(string, ...interface{})
type logFuncln func(...interface{})

log := GetInstance()
log.SetLogLevel(LOG_DEBUG)

tests := []struct {
name string
prefix string
fn logFunc
fnf logFuncf
fnln logFuncln
}{
{
name: "error",
prefix: "ERROR:",
fn: log.Error,
fnf: log.Errorf,
fnln: log.Errorln,
},
{
name: "warn",
prefix: "WARNING:",
fn: log.Warn,
fnf: log.Warnf,
fnln: log.Warnln,
},
{
name: "info",
prefix: "",
fn: log.Info,
fnf: log.Infof,
fnln: log.Infoln,
},
{
name: "debug",
prefix: "DEBUG:",
fn: log.Debug,
fnf: log.Debugf,
fnln: log.Debugln,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Redirect system stdout to our own so we can check log output
// to stdout
oldStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Errorf("failed to redirect stdout: %s", err)
}
os.Stdout = w
log.SetStdDest(w, w)
outChan := make(chan string)

test.fn("test")
test.fnf("test\n")
test.fnln("test")

go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()
}()

w.Close()
os.Stdout = oldStdout
out := <-outChan

regex := regexp.MustCompile(test.prefix + " logger_test.go:[0-9]+: test")
require.Regexp(t, regex, out)
})
}

}

0 comments on commit efa4be2

Please sign in to comment.