Merge "improve logging"

This commit is contained in:
Zuul 2020-09-01 12:48:29 +00:00 committed by Gerrit Code Review
commit 4566efc325
3 changed files with 26 additions and 7 deletions

View File

@ -3,7 +3,7 @@ SHELL := /bin/bash
GIT_VERSION ?= v0.1.0 GIT_VERSION ?= v0.1.0
GIT_MODULE ?= opendev.org/airship/airshipctl/pkg/version GIT_MODULE ?= opendev.org/airship/airshipctl/pkg/version
GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo -trimpath
GO_FLAGS += -ldflags "-X ${GIT_MODULE}.gitVersion=${GIT_VERSION}" GO_FLAGS += -ldflags "-X ${GIT_MODULE}.gitVersion=${GIT_VERSION}"
BINDIR := bin BINDIR := bin

View File

@ -15,6 +15,7 @@
package log package log
import ( import (
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -28,6 +29,9 @@ var (
// Init initializes settings related to logging // Init initializes settings related to logging
func Init(debugFlag bool, out io.Writer) { func Init(debugFlag bool, out io.Writer) {
debug = debugFlag debug = debugFlag
if debug {
airshipLog.SetFlags(log.LstdFlags | log.Llongfile)
}
airshipLog.SetOutput(out) airshipLog.SetOutput(out)
} }
@ -39,25 +43,25 @@ func DebugEnabled() bool {
// Debug is a wrapper for log.Debug // Debug is a wrapper for log.Debug
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
if debug { if debug {
airshipLog.Print(v...) writeLog(v...)
} }
} }
// Debugf is a wrapper for log.Debugf // Debugf is a wrapper for log.Debugf
func Debugf(format string, v ...interface{}) { func Debugf(format string, v ...interface{}) {
if debug { if debug {
airshipLog.Printf(format, v...) writeLog(fmt.Sprintf(format, v...))
} }
} }
// Print is a wrapper for log.Print // Print is a wrapper for log.Print
func Print(v ...interface{}) { func Print(v ...interface{}) {
airshipLog.Print(v...) writeLog(v...)
} }
// Printf is a wrapper for log.Printf // Printf is a wrapper for log.Printf
func Printf(format string, v ...interface{}) { func Printf(format string, v ...interface{}) {
airshipLog.Printf(format, v...) writeLog(fmt.Sprintf(format, v...))
} }
// Fatal is a wrapper for log.Fatal // Fatal is a wrapper for log.Fatal
@ -74,3 +78,15 @@ func Fatalf(format string, v ...interface{}) {
func Writer() io.Writer { func Writer() io.Writer {
return airshipLog.Writer() return airshipLog.Writer()
} }
func writeLog(v ...interface{}) {
if debug {
err := airshipLog.Output(3, fmt.Sprint(v...))
if err != nil {
log.Print(v...)
log.Print(err)
}
} else {
airshipLog.Print(v...)
}
}

View File

@ -17,6 +17,7 @@ package log_test
import ( import (
"bytes" "bytes"
"regexp" "regexp"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -73,7 +74,8 @@ func TestLoggingDebug(t *testing.T) {
expected := "DebugTrue args 5\n" expected := "DebugTrue args 5\n"
require.Regexp(logFormatRegex, actual) require.Regexp(logFormatRegex, actual)
actual = actual[prefixLength:] lastIndex := strings.LastIndex(actual, ":")
actual = actual[lastIndex+2:]
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })
@ -86,7 +88,8 @@ func TestLoggingDebug(t *testing.T) {
expected := "DebugfTrue args 5\n" expected := "DebugfTrue args 5\n"
require.Regexp(logFormatRegex, actual) require.Regexp(logFormatRegex, actual)
actual = actual[prefixLength:] lastIndex := strings.LastIndex(actual, ":")
actual = actual[lastIndex+2:]
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })