logger

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 522

Documentation

Overview

Package logger contains different methods to log.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Default is the default logger that is used for the Logf function, if no one is provided. It uses the
	// TerratestLogger to log messages. This can be overwritten to change the logging globally.
	Default = New(terratestLogger{})
	// Discard discards all logging.
	Discard = New(discardLogger{})
	// Terratest logs the given format and arguments, formatted using fmt.Sprintf, to stdout, along with a timestamp and
	// information about what test and file is doing the logging. Before Go 1.14, this is an alternative to t.Logf as it
	// logs to stdout immediately, rather than buffering all log output and only displaying it at the very end of the test.
	// This is useful because:
	//
	// 1. It allows you to iterate faster locally, as you get feedback on whether your code changes are working as expected
	//    right away, rather than at the very end of the test run.
	//
	// 2. If you have a bug in your code that causes a test to never complete or if the test code crashes, t.Logf would
	//    show you no log output whatsoever, making debugging very hard, where as this method will show you all the log
	//    output available.
	//
	// 3. If you have a test that takes a long time to complete, some CI systems will kill the test suite prematurely
	//    because there is no log output with t.Logf (e.g., CircleCI kills tests after 10 minutes of no log output). With
	//    this log method, you get log output continuously.
	//
	Terratest = New(terratestLogger{})
	// 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{})
)
View Source
var MutexStdout sync.Mutex

MutexStdout is used to synchronize Log and Logf calls that write to stdout.

Functions

func CallerPrefix

func CallerPrefix(callDepth int) string

CallerPrefix returns the file and line number information about the methods that called this method, based on the current goroutine's stack. The argument callDepth is the number of stack frames to ascend, with 0 identifying the method that called CallerPrefix, 1 identifying the method that called that method, and so on.

This code is adapted from testing.go, where it is in a private method called decorate.

func DoLog

func DoLog(t testing.TestingT, callDepth int, writer io.Writer, args ...any)

DoLog logs the given arguments to the given writer, along with a timestamp and information about what test and file is doing the logging.

func Log

func Log(t testing.TestingT, args ...any)

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 Logf deprecated

func Logf(t testing.TestingT, format string, args ...any)

Logf logs the given format and arguments, formatted using fmt.Sprintf, to stdout, along with a timestamp and information about what test and file is doing the logging. Before Go 1.14, this is an alternative to t.Logf as it logs to stdout immediately, rather than buffering all log output and only displaying it at the very end of the test. This is useful because:

  1. It allows you to iterate faster locally, as you get feedback on whether your code changes are working as expected right away, rather than at the very end of the test run.

  2. If you have a bug in your code that causes a test to never complete or if the test code crashes, t.Logf would show you no log output whatsoever, making debugging very hard, where as this method will show you all the log output available.

  3. If you have a test that takes a long time to complete, some CI systems will kill the test suite prematurely because there is no log output with t.Logf (e.g., CircleCI kills tests after 10 minutes of no log output). With this log method, you get log output continuously.

Although t.Logf now supports streaming output since Go 1.14, this is kept for compatibility purposes.

Deprecated: use Logger instead, as it provides more flexibility on logging.

Types

type Logger added in v0.27.3

type Logger struct {
	// contains filtered or unexported fields
}

Logger wraps a TestLogger implementation and provides nil-safe logging.

func New added in v0.27.3

func New(l TestLogger) *Logger

New creates a new Logger instance wrapping the given TestLogger implementation.

func (*Logger) Logf added in v0.27.3

func (l *Logger) Logf(t testing.TestingT, format string, args ...any)

Logf logs the given format and arguments using the encapsulated TestLogger implementation.

type TestLogger added in v0.27.3

type TestLogger interface {
	Logf(t testing.TestingT, format string, args ...any)
}

TestLogger is the interface for custom logger implementations that can be used with the Logger wrapper.

Directories

Path Synopsis
Package parser contains methods to parse and restructure log output from go testing and terratest.
Package parser contains methods to parse and restructure log output from go testing and terratest.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL