infra

package
v0.9.15-beta-15 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package infra contains functionality shared between test and benchmark managers.

The definitions in this package are publicly visible and may in some cases be usable elsewhere, as opposed to the internal package which also provides shared infrastructure.

Attribute Declarations

  • AttrFn defines the HandlerOptions.ReplaceAttr function template.
  • EmptyAttr() returns an empty attribute.

Creator Objects

A Creator object is a factory used to generate slog.Logger objects for testing. A number of predefined `Creator` objects can be found in the creator package.

Predefined Options

Functions are provided to return various standard slog.HandlerOptions objects. These are used in testing and benchmarks and may have some utility elsewhere.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmptyAttr

func EmptyAttr() slog.Attr

EmptyAttr returns an empty attribute object as a convenience.

func LevelOptions

func LevelOptions(level slog.Leveler) *slog.HandlerOptions

LevelOptions returns a slog.HandlerOptions object with the specified level.

func ReplaceAttrOptions

func ReplaceAttrOptions(fn AttrFn) *slog.HandlerOptions

ReplaceAttrOptions returns a slog.HandlerOptions with the specified ReplaceAttr function.

func SimpleOptions

func SimpleOptions() *slog.HandlerOptions

SimpleOptions returns a default, simple, slog.HandlerOptions object.

func SourceOptions

func SourceOptions() *slog.HandlerOptions

SourceOptions returns a slog.HandlerOptions object with the specified level and the AddSource field set to true.

Types

type AttrFn

type AttrFn func(groups []string, a slog.Attr) slog.Attr

AttrFn defines a type for ReplaceAttr functions. The slog.HandlerOptions struct defines this inline without defining a type.

type CreateHandlerFn

type CreateHandlerFn func(w io.Writer, options *slog.HandlerOptions) slog.Handler

CreateHandlerFn is a function that can create new slog.Handler objects.

type CreateLoggerFn

type CreateLoggerFn func(w io.Writer, options *slog.HandlerOptions) *slog.Logger

CreateLoggerFn is a function that can create new slog.Logger objects.

type Creator

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

A Creator object encapsulates the creation of new slog.Handler objects. This includes both the name of the handler and a CreateHandlerFn and/or CreateLoggerFn. The reason for two functions is the possibility that a slog.Logger is available but a slog.Handler is not. Creator factories can either slog.Handler or slog.Logger objects depending on configuration. Most tests use slog.Logger objects, but a few tests require a slog.Handler.

  • If only a CreateLoggerFn is provided the Creator.NewHandler method returns nil and the Creator.CanMakeHandler method returns false. Tests requiring handler creation use the latter method to skip the test.
  • If only a CreateHandlerFn is provided the Creator.NewHandler method uses that function to return a new handler and the Creator.NewLogger method also uses that function, then wraps the handler in `slog.New`.
  • If both functions are provided, they will each be used for the appropriate method.
Example
creator := NewCreator(
	"example",
	func(w io.Writer, options *slog.HandlerOptions) slog.Handler {
		return slog.NewJSONHandler(w, options)
	},
	nil, // loggerFn optional if handlerFn provided
	// summary and links optional
	"", nil)
var buffer bytes.Buffer
logger := creator.NewLogger(&buffer, nil)
logger.Info("message", "pi", math.Pi)
logMap, err := json.Parse(buffer.Bytes())
if err == nil {
	fmt.Printf("msg:%s pi:%7.5f\n", logMap[slog.MessageKey], logMap["pi"])
} else {
	fmt.Printf("!!! %s\n", err.Error())
}
Output:

msg:message pi:3.14159

func NewCreator

func NewCreator(name string, handlerFn CreateHandlerFn, loggerFn CreateLoggerFn, summary string, links Links) Creator

NewCreator returns a new Creator object for the specified name and CreateLoggerFn.

  • The name argument is required and must not be the empty string.
  • At least one of the handlerFn or loggerFn arguments must be non-nil. The handlerFn argument is preferred.
  • The summary and links arguments are desired but not required as they only show up on cmd/server pages.

func (*Creator) CanMakeHandler

func (c *Creator) CanMakeHandler() bool
func (c *Creator) HasLinks() bool

func (*Creator) HasSummary

func (c *Creator) HasSummary() bool
func (c *Creator) Links() map[string]string

func (*Creator) Name

func (c *Creator) Name() string

Name returns the name of the slog package.

func (*Creator) NewHandler

func (c *Creator) NewHandler(w io.Writer, options *slog.HandlerOptions) slog.Handler

NewHandler returns a new slog.Handler object. The actual creation is done by invoking the embedded CreateHandlerFn.

func (*Creator) NewLogger

func (c *Creator) NewLogger(w io.Writer, options *slog.HandlerOptions) *slog.Logger

NewLogger returns a new slog.Logger object. The actual creation is done by invoking the embedded CreateLoggerFn, if it is non-nil, or the embedded CreateHandlerFn.

func (*Creator) Summary

func (c *Creator) Summary() string
type Links map[string]string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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