fc6c3620c4
Go 1.14 now streams t.Log output as it happens [1], so we no longer need to maintain our log STDOUT helper. I preserved the interface, so `testlog` still takes in a `*testing.T` though unused. Changing it requires so too many changes that I didn't want to make quite yet. [1] https://golang.org/doc/go1.14#go-test
83 lines
2 KiB
Go
83 lines
2 KiB
Go
// Package testlog creates a *log.Logger backed by *testing.T to ease logging
|
|
// in tests. This allows logs from components being tested to only be printed
|
|
// if the test fails (or the verbose flag is specified).
|
|
package testlog
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
)
|
|
|
|
// LogPrinter is the methods of testing.T (or testing.B) needed by the test
|
|
// logger.
|
|
type LogPrinter interface {
|
|
Logf(format string, args ...interface{})
|
|
}
|
|
|
|
// NewWriter creates a new io.Writer backed by a Logger.
|
|
func NewWriter(t LogPrinter) io.Writer {
|
|
return os.Stderr
|
|
}
|
|
|
|
// NewPrefixWriter creates a new io.Writer backed by a Logger with a custom
|
|
// prefix per Write.
|
|
func NewPrefixWriter(t LogPrinter, prefix string) io.Writer {
|
|
return &prefixStderr{[]byte(prefix)}
|
|
}
|
|
|
|
// New returns a new test logger. See https://golang.org/pkg/log/#New
|
|
func New(t LogPrinter, prefix string, flag int) *log.Logger {
|
|
return log.New(os.Stderr, prefix, flag)
|
|
}
|
|
|
|
// WithPrefix returns a new test logger with the Lmicroseconds flag set.
|
|
func WithPrefix(t LogPrinter, prefix string) *log.Logger {
|
|
return New(t, prefix, log.Lmicroseconds)
|
|
}
|
|
|
|
// Logger returns a new test logger with the Lmicroseconds flag set and no
|
|
// prefix.
|
|
func Logger(t LogPrinter) *log.Logger {
|
|
return WithPrefix(t, "")
|
|
}
|
|
|
|
//HCLogger returns a new test hc-logger.
|
|
func HCLogger(t LogPrinter) hclog.InterceptLogger {
|
|
level := hclog.Trace
|
|
envLogLevel := os.Getenv("NOMAD_TEST_LOG_LEVEL")
|
|
if envLogLevel != "" {
|
|
level = hclog.LevelFromString(envLogLevel)
|
|
}
|
|
opts := &hclog.LoggerOptions{
|
|
Level: level,
|
|
Output: os.Stderr,
|
|
IncludeLocation: true,
|
|
}
|
|
return hclog.NewInterceptLogger(opts)
|
|
}
|
|
|
|
type prefixStderr struct {
|
|
prefix []byte
|
|
}
|
|
|
|
// Write to stdout with a prefix per call containing non-whitespace characters.
|
|
func (w *prefixStderr) Write(p []byte) (int, error) {
|
|
if len(p) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
// Skip prefix if only writing whitespace
|
|
if len(bytes.TrimSpace(p)) > 0 {
|
|
_, err := os.Stderr.Write(w.prefix)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
return os.Stderr.Write(p)
|
|
}
|