tfsdklogtest

package
Version: v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2022 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package tfsdklogtest provides functionality for unit testing of SDK logging. Provider developers should use the tflogtest package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MultilineJSONDecode

func MultilineJSONDecode(data io.Reader) ([]map[string]interface{}, error)

MultilineJSONDecode supports decoding the output of a JSON logger into a slice of maps, with each element representing a log entry.

Example
var output bytes.Buffer

ctx := RootLogger(context.Background(), &output)

// Root SDK logger is now available for usage, such as writing entries,
// calling SetField(), or calling NewSubsystem().
tfsdklog.Trace(ctx, "entry 1")
tfsdklog.Trace(ctx, "entry 2")

entries, err := MultilineJSONDecode(&output)

if err != nil {
	// Typical unit testing would call t.Fatalf() here.
	fmt.Printf("unable to read multiple line JSON: %s", err)
}

// Entries can be checked via go-cmp's cmp.Diff() or other testing methods.
// This example outputs them to stdout in an explicitly formatted string,
// which would not be expected in typical unit testing.
for _, entry := range entries {
	fmt.Printf("@message: %s\n", entry["@message"])
}
Output:

@message: entry 1
@message: entry 2

func RootLogger

func RootLogger(ctx context.Context, output io.Writer) context.Context

RootLogger returns a context containing a SDK root logger suitable for unit testing that is:

  • Written to the given io.Writer, such as a bytes.Buffer.
  • Written with JSON output, that can be decoded with MultilineJSONDecode.
  • Log level set to TRACE.
  • Without location/caller information in log entries.
  • Without timestamps in log entries.
Example
var output bytes.Buffer

ctx := RootLogger(context.Background(), &output)

// Root SDK logger is now available for usage, such as writing entries,
// calling SetField(), or calling NewSubsystem().
tfsdklog.Trace(ctx, "hello, world", map[string]interface{}{
	"foo":    123,
	"colors": []string{"red", "blue", "green"},
})

fmt.Println(output.String())
Output:

{"@level":"trace","@message":"hello, world","@module":"sdk","colors":["red","blue","green"],"foo":123}

Types

This section is empty.

Jump to

Keyboard shortcuts

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