Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add global logger for package level log functions #1180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions modules/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var (
// TestingT can be used to use Go's testing.T to log. If this is used, but no testing.T is provided, it will fallback
// to Default.
TestingT = New(testingT{})
// Global is used by the package-level Logf and Log functions. Overwrite this to change the logging function.
Global = New(terratestLogger{extraDepth: 1})
)

type TestLogger interface {
Expand Down Expand Up @@ -96,10 +98,12 @@ func (_ testingT) Logf(t testing.TestingT, format string, args ...interface{}) {
return
}

type terratestLogger struct{}
type terratestLogger struct {
extraDepth int
}

func (_ terratestLogger) Logf(t testing.TestingT, format string, args ...interface{}) {
DoLog(t, 3, os.Stdout, fmt.Sprintf(format, args...))
func (l terratestLogger) Logf(t testing.TestingT, format string, args ...interface{}) {
DoLog(t, 3+l.extraDepth, os.Stdout, fmt.Sprintf(format, args...))
}

// Deprecated: use Logger instead, as it provides more flexibility on logging.
Expand All @@ -124,18 +128,18 @@ func Logf(t testing.TestingT, format string, args ...interface{}) {
tt.Helper()
}

DoLog(t, 2, os.Stdout, fmt.Sprintf(format, args...))
Global.Logf(t, format, args)
}

// Log logs the given arguments to stdout, along with a timestamp and information about what test and file is doing the
// logging. This is an alternative to t.Logf that logs to stdout immediately, rather than buffering all log output and
// only displaying it at the very end of the test. See the Logf method for more info.
func Log(t testing.TestingT, args ...interface{}) {
func Log(t testing.TestingT, msg string) {
if tt, ok := t.(helper); ok {
tt.Helper()
}

DoLog(t, 2, os.Stdout, args...)
Global.Logf(t, msg)
}

// DoLog logs the given arguments to the given writer, along with a timestamp and information about what test and file is
Expand Down