improve logging

This will help on debugging and / or error messages which
will show what package and what line a message came from

This is pared down from what is currently being proposed for the UI
https://review.opendev.org/#/c/747473/

Change-Id: I68eb8b0141a8d56d5ea14aa623a25f9287ff1e75
This commit is contained in:
Schiefelbein, Andrew 2020-08-24 09:48:29 -05:00
parent 0966c5bb3b
commit 20d2a99190
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,31 +29,34 @@ 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)
} }
// 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
@ -69,3 +73,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)
}) })