formatter

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 12 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// ConsoleFormatterAttrsPart is the part used by PartOrder for all attributes.
	ConsoleFormatterAttrsPart = "attrs"

	// ConsoleFormatterLevelPart is the part used for PartOrder for the message level.
	ConsoleFormatterLevelPart = "level"

	// ConsoleFormatterMessagePart is the part used for PartOrder for the message itself.
	ConsoleFormatterMessagePart = "message"

	// ConsoleFormatterSourcePart is the part used for PartOrder for the source code location for the record.
	ConsoleFormatterSourcePart = "source"

	// ConsoleFormatterTimePart is the part used for PartOrder for the time of the message/record.
	ConsoleFormatterTimePart = "time"
)
View Source
const (
	// JSONFormatterLevelAttr is the default JSON key to use when outputting the level.
	JSONFormatterLevelAttr = "@level"

	// JSONFormatterMessageAttr is the default JSON key to use when outputting the message.
	JSONFormatterMessageAttr = "@msg"

	// JSONFormatterNestedAttributeAttr is the default JSON key to use when outputting attributes, if attribute nesting
	// is enabled.
	JSONFormatterNestedAttributeAttr = "@attributes"

	// JSONFormatterSourceAttr is the default JSON key to use when outputting the source code location.
	JSONFormatterSourceAttr = "@source"

	// JSONFormatterTimeAttr is the default JSON key to use when outputting the time of the record.
	JSONFormatterTimeAttr = "@time"
)

Variables

This section is empty.

Functions

func ColorizeAttrFormatter added in v0.6.0

func ColorizeAttrFormatter(ctx context.Context, level slog.Leveler, group, attrKey string,
	attrValue slog.Value) (string, slog.Value, error)

ColorizeAttrFormatter is a customized formatter for colorizing attribute keys.

func ColorizeErrorAttrFormatter added in v0.6.0

func ColorizeErrorAttrFormatter(ctx context.Context, level slog.Leveler, group, attrKey string,
	attrValue slog.Value) (string, slog.Value, error)

ColorizeErrorAttrFormatter is a customized formatter for colorizing error keys.

func ColorizeLevelFormatter added in v0.6.0

func ColorizeLevelFormatter(ctx context.Context, level slog.Leveler) (string, error)

ColorizeLevelFormatter is a customized formatter for colorizing levels.

func ColorizeSourceFormatter added in v0.6.0

func ColorizeSourceFormatter(ctx context.Context, level slog.Leveler, pc uintptr) (string, error)

ColorizeSourceFormatter is a customized formatter for colorizing the source file and line.

func ContextWithConsoleFormatterOptions added in v0.5.0

func ContextWithConsoleFormatterOptions(ctx context.Context, opts ConsoleFormatterOptions) context.Context

ContextWithConsoleFormatterOptions adds the options to the given context and returns the new context.

func ContextWithJSONFormatterOptions added in v0.5.0

func ContextWithJSONFormatterOptions(ctx context.Context, opts JSONFormatterOptions) context.Context

ContextWithJSONFormatterOptions adds the options to the given context and returns the new context.

func DefaultConsoleFormatter

func DefaultConsoleFormatter(colorize bool) *consoleFormatter

DefaultConsoleFormatter returns a console formatter with typical defaults already set.

func DefaultJSONFormatter

func DefaultJSONFormatter() *jsonFormatter

DefaultJSONFormatter returns a JSON formatter with typical defaults already set.

func FormatLevelValueDefault

func FormatLevelValueDefault(ctx context.Context, level slog.Leveler) (string, error)

FormatLevelValueDefault is a default level formatter which simply shortens the level to 3 letters.

Standard slogx levels are formatted using their ShortString() function while any other level is shortened to the first three characters and capitalized.

func FormatSourceValueDefault

func FormatSourceValueDefault(ctx context.Context, level slog.Leveler, pc uintptr) (string, error)

FormatSourceValueDefault is a default source code location formatter which returns the location as filename:line.

func FormatTimeValueDefault

func FormatTimeValueDefault(ctx context.Context, level slog.Leveler, t time.Time) (string, error)

FormatTimeValueDefault is a default source code location formatter which returns the time as the UTC time in RFC3339 format.

func NewConsoleFormatter

func NewConsoleFormatter(opts ConsoleFormatterOptions) *consoleFormatter

NewConsoleFormatter creates and returns a new console formatter.

func NewJSONFormatter

func NewJSONFormatter(opts JSONFormatterOptions) *jsonFormatter

NewJSONFormatter creates and returns a new JSON formatter.

Types

type BufferFormatter

type BufferFormatter interface {
	// FormatRecord should take the data from the record and format it as needed, storing it in the returned buffer.
	FormatRecord(context.Context, time.Time, slogx.Level, uintptr, string, []slog.Attr) (*slogx.Buffer, error)
}

BufferFormatter describes the interface a formatter which outputs a record to a buffer must implement.

type ColorBufferFormatter

type ColorBufferFormatter interface {
	BufferFormatter

	// IsColorized should return whether or not the formatter uses colorized output.
	IsColorized() bool
}

ColorBufferFormatter describes the interface a formatter which supports colorized text and outputs a record to a buffer must implement.

type ConsoleFormatterOptions

type ConsoleFormatterOptions struct {
	// AttrFormatter is the middleware formatting function to call to format any attribute.
	//
	// Attribute values should be resolved by the handler before formatting. Any value returned by the formatter should
	// be resolved prior to return.
	//
	// If nil, attributes are simply printed unchanged as key=value.
	AttrFormatter FormatAttrFn

	// EnableColor determines whether or not to enable colorized output.
	EnableColor bool

	// IgnoreAttrs is a list of regular expressions to use for matching attributes which should not be printed.
	//
	// Note that this only applies to attributes and not defined parts like the level, message, source or time. If you
	// want to ignore those, simply leave them out of the PartOrder array.
	//
	// If any regular expression does not compile, it is simply ignored.
	IgnoreAttrs []string

	// LevelFormatter is the middleware formatting function to call to format the level.
	//
	// If nil, the level is printed using FormatLevelValueDefault().
	LevelFormatter FormatLevelValueFn

	// MessageFormatter is the middlware formatting function to call to format the message.
	//
	// If nil, the message is printed as-is.
	MessageFormatter FormatMessageValueFn

	// PartOrder is the order in which to print the various parts of the message.
	//
	// The following values are valid for the string:
	// ConsoleFormatterAttrPart - specific attribute to print; use a single period (.) to separate group name from
	//                            attribute name if the attribute is nested within a group
	// ConsoleFormatterAttrRegexPart - attributes matching the regular expression to print; use an escaped period (\.)
	//                                 to separate group name from attribute name if the attribute is nested within a
	//                                 group; if the regex does not compile, it is ignored
	// ConsoleFormatterAttrsPart - all attributes
	// ConsoleFormatterLevelPart - the log level of the message from the record as a string
	// ConsoleFormatterMessagePart - the message from the record
	// ConsoleFormatterSourcePart - the location at which the message was logged
	// ConsoleFormatterTimePart - the time of the message from the record
	//
	// If any other string is specified other than those above, it is simply printed as-is.
	//
	// If an attribute is specified but not present in the record, it is ignored.
	//
	// By default the format will be "TimePart LevelPart SourcePart > MessagePart AttrsPart".
	PartOrder []ConsoleFormatterPart

	// PartSeparator is how to separate the parts when they're being printed. Must be at least 1 character.
	//
	// By default, a space is used.
	PartSeparator string

	// SortAttributes determines whether or not to sort attributes in the output.
	//
	// Note that this *only* affects the output for ConsoleFormatterAttrsPart.
	SortAttributes bool

	// SourceFormatter is the middleware formatting function to call to format the source code location where the record
	// was created.
	//
	// If nil, the source code location is printed using FormatSourceValueDefault().
	SourceFormatter FormatSourceValueFn

	// SpecificAttrFormatter is the middleware formatting function to call to format a specific attribute.
	//
	// The key for the map corresponds to the name of the specific attribute to format. If an attribute is nested within
	// a group, use a single period (.) to designate the group and attribute (eg: GROUP.ATTRIBUTE). Nested groups use
	// the same format (eg: GROUP1.GROUP2.ATTRIBUTE).
	//
	// Attribute values should be resolved by the handler before formatting. Any value returned by the formatter should
	// be resolved prior to return.
	//
	// If nil or if the attribute does not exist in the map, the default is to fall back to the AttrFormatter function.
	SpecificAttrFormatter map[string]FormatAttrFn

	// TimeFormatter is the middleware formatting function to call to the time of the record.
	//
	// If nil, the time is printed using FormatTimeValueDefault().
	TimeFormatter FormatTimeValueFn

	// UniqueAttributesOnly indicates whether or not to only print unique attributes.
	//
	// If multiple attributes are present within the same group with the same key name, only the latest attribute
	// will be printed.
	//
	// For example:
	//   logger := logger.With(slog.String("k1", "v1"))
	//   logger.Warn("this is a warning message", slog.String("k1", "v2"))
	//
	// If this value is true and the code above is used, only {"k1":"v2"} would be shown as an attribute.
	//
	// The only exception to this rule is that attributes which are not nested and overlap with the time, level, source
	// or message keys will be ignored as time, level, source and message are core parts of the output.
	UniqueAttributesOnly bool
}

ConsoleFormatterOptions holds the options for the console formatter.

func ConsoleFormatterOptionsFromContext added in v0.5.0

func ConsoleFormatterOptionsFromContext(ctx context.Context) *ConsoleFormatterOptions

ConsoleFormatterOptionsFromContext retrieves the formatter options from the context.

If the options are not set in the context, a set of default options is returned instead.

func DefaultConsoleFormatterOptions

func DefaultConsoleFormatterOptions() ConsoleFormatterOptions

DefaultConsoleFormatterOptions returns a default set of options for the console formatter.

type ConsoleFormatterPart

type ConsoleFormatterPart string

ConsoleFormatterPart is just a string.

func ConsoleFormatterAttrPart

func ConsoleFormatterAttrPart(attrKey string) ConsoleFormatterPart

ConsoleFormatterAttrPart is the part used by PartOrder for a specific attribute.

func ConsoleFormatterAttrRegexPart added in v0.6.0

func ConsoleFormatterAttrRegexPart(attrKey string) ConsoleFormatterPart

ConsoleFormatterAttrRegexPart is the part used by PartOrder for an attribute regular expression.

func (ConsoleFormatterPart) GetAttr added in v0.6.0

func (p ConsoleFormatterPart) GetAttr() string

GetAttr returns the name of the specific attribute if this part is for a specific attribute. Otherwise it returns an empty string.

func (ConsoleFormatterPart) GetAttrRegex added in v0.6.0

func (p ConsoleFormatterPart) GetAttrRegex() string

GetAttrRegex returns the regular expression to use when matching attributes attribute if this part contains a regular expression. Otherwise it returns an empty string.

func (ConsoleFormatterPart) IsRegexAttr added in v0.6.0

func (p ConsoleFormatterPart) IsRegexAttr() bool

IsRegexAttrPart indicates whether or not this part is for attributes matching a regular expression.

func (ConsoleFormatterPart) IsSpecificAttr added in v0.6.0

func (p ConsoleFormatterPart) IsSpecificAttr() bool

IsSpecificAttr indicates whether or not this part is for a specific attribute.

type FormatAttrFn

type FormatAttrFn func(context.Context, slog.Leveler, string, string, slog.Value) (string, slog.Value, error)

FormatAttrFn is used to format the key and value for a particular attribute in the record.

The group name will be an empty string for attributes not nested within a group. Otherwise, the group will be populated with the name of the group to which the attribute belongs. If an attribue is nested in multiple groups, each group is separated by a single period (.) character (eg: group1.group2.group3).

The function should return the formatted key and value. The value passed to this function should be resolved by the handler prior to being passed. The value returned by the function will not be resolved again, so if it needs resolving, it should be done prior to returning it.

type FormatLevelValueFn

type FormatLevelValueFn func(context.Context, slog.Leveler) (string, error)

FormatLevelValueFn is used to format the record's level.

type FormatMessageValueFn

type FormatMessageValueFn func(context.Context, slog.Leveler, string) (string, error)

FormatMessageValueFn is used to format the message.

type FormatSourceValueFn

type FormatSourceValueFn func(context.Context, slog.Leveler, uintptr) (string, error)

FormatSourceValueFn is used to format the source code location where the record was created.

type FormatTimeValueFn

type FormatTimeValueFn func(context.Context, slog.Leveler, time.Time) (string, error)

FormatTimeValueFn is used to format the time the record was created.

type JSONFormatterOptions

type JSONFormatterOptions struct {
	// AttrFormatter is the middleware formatting function to call to format any attribute.
	//
	// Attribute values should be resolved by the handler before formatting. Any value returned by the formatter should
	// be resolved prior to return.
	//
	// If nil, attributes remain unchanged.
	AttrFormatter FormatAttrFn

	// IgnoreAttrs is a list of regular expressions to use for matching attributes which should not be printed.
	//
	// Note that this only applies to attributes and not defined parts of the record such as time, level and the
	// message itself as they are always printed. Likewise, if NestedAttributes is true, the NestedAttributeAttr
	// is always printed and if IncludeSource is true, the source is always printed.
	//
	// If any regular expression does not compile, it is simply ignored.
	IgnoreAttrs []string

	// IncludeSource determines whether or not to include the source code location of the record in the output.
	IncludeSource bool

	// LevelAttr is the name of the JSON attribute to use for the level.
	//
	// If empty, defaults to JSONFormatterLevelAttr.
	LevelAttr string

	// LevelFormatter is the middleware formatting function to call to format the level.
	//
	// If nil, the level is printed using FormatLevelValueDefault().
	LevelFormatter FormatLevelValueFn

	// MessageAttr is the name of the JSON attribute to use for the message.
	//
	// If empty, defaults to JSONFormatterMessageAttr.
	MessageAttr string

	// MessageFormatter is the middlware formatting function to call to format the message.
	//
	// If nil, the message is printed as-is.
	MessageFormatter FormatMessageValueFn

	// NestAttributes indicates whether or not to nest the record's attributes under their own key in the JSON output.
	//
	// If true, the NestedAttributeAttr will always be part of the output even if it is empty.
	NestAttributes bool

	// NetstedAttributeAttr is the name of the JSON attribute to use if nesting attributes.
	//
	// If empty, defaults to JSONFormatterNestedAttributeAttr.
	NestedAttributeAttr string

	// SortAttrs indicates whether or not to sort attributes in the output.
	//
	// Note that this *only* affects attributes and not the time, message, source or level.
	SortAttrs bool

	// SourceAttr is the name of the JSON attribute to use for the source code location.
	//
	// If empty, defaults to JSONFormatterSourceAttr.
	SourceAttr string

	// SourceFormatter is the middleware formatting function to call to format the source code location where the record
	// was created.
	//
	// If nil, the source code location is printed using FormatSourceValueDefault().
	SourceFormatter FormatSourceValueFn

	// SpecificAttrFormatter is the middleware formatting function to call to format a specific attribute.
	//
	// The key for the map corresponds to the name of the specific attribute to format. If an attribute is nested within
	// a group, use a single period (.) to designate the group and attribute (eg: GROUP.ATTRIBUTE). Nested groups use
	// the same format (eg: GROUP1.GROUP2.ATTRIBUTE).
	//
	// Attribute values should be resolved by the handler before formatting. Any value returned by the formatter should
	// be resolved prior to return.
	//
	// If nil or if the attribute does not exist in the map, the default is to fall back to the AttrFormatter function.
	SpecificAttrFormatter map[string]FormatAttrFn

	// TimeAttr is the name of the JSON attribute to use for the time of the record.
	//
	// If empty, defaults to JSONFormatterTimeAttr.
	TimeAttr string

	// TimeFormatter is the middleware formatting function to call to the time of the record.
	//
	// If nil, the time is printed using FormatTimeValueDefault().
	TimeFormatter FormatTimeValueFn
}

JSONFormatterOptions holds the options for the JSON formatter.

func DefaultJSONFormatterOptions

func DefaultJSONFormatterOptions() JSONFormatterOptions

DefaultJSONFormatterOptions returns a default set of options for the JSON formatter.

func JSONFormatterOptionsFromContext added in v0.5.0

func JSONFormatterOptionsFromContext(ctx context.Context) *JSONFormatterOptions

JSONFormatterOptionsFromContext retrieves the options from the context.

If the options are not set in the context, a set of default options is returned instead.

Jump to

Keyboard shortcuts

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