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 ¶
- func EmptyAttr() slog.Attr
- func LevelOptions(level slog.Leveler) *slog.HandlerOptions
- func ReplaceAttrOptions(fn AttrFn) *slog.HandlerOptions
- func SimpleOptions() *slog.HandlerOptions
- func SourceOptions() *slog.HandlerOptions
- type AttrFn
- type CreateHandlerFn
- type CreateLoggerFn
- type Creator
- func (c *Creator) CanMakeHandler() bool
- func (c *Creator) HasLinks() bool
- func (c *Creator) HasSummary() bool
- func (c *Creator) Links() map[string]string
- func (c *Creator) Name() string
- func (c *Creator) NewHandler(w io.Writer, options *slog.HandlerOptions) slog.Handler
- func (c *Creator) NewLogger(w io.Writer, options *slog.HandlerOptions) *slog.Logger
- func (c *Creator) Summary() string
- type Links
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
AttrFn defines a type for ReplaceAttr functions. The slog.HandlerOptions struct defines this inline without defining a type.
type CreateHandlerFn ¶
CreateHandlerFn is a function that can create new slog.Handler objects.
type CreateLoggerFn ¶
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 (*Creator) HasSummary ¶
func (*Creator) NewHandler ¶
NewHandler returns a new slog.Handler object. The actual creation is done by invoking the embedded CreateHandlerFn.