formatters

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatText   = 0
	FormatJSON   = 1
	FormatCustom = 2
)

Format type constants

View Source
const (
	LevelTrace = 0
	LevelDebug = 1
	LevelInfo  = 2
	LevelWarn  = 3
	LevelError = 4
)

Log level constants

Variables

View Source
var DefaultFactory = NewFactory()

DefaultFactory is the global formatter factory

Functions

func CreateFormatter

func CreateFormatter(name string) (types.Formatter, error)

CreateFormatter creates a formatter using the default factory

func CreateFormatterByType

func CreateFormatterByType(formatType int) (types.Formatter, error)

CreateFormatterByType creates a formatter by type using the default factory

func Register

func Register(name string, constructor FormatterConstructor) error

Register registers a formatter with the default factory

Types

type BatchFormatter

type BatchFormatter interface {
	types.Formatter

	// FormatBatch formats multiple messages as a batch
	FormatBatch(messages []types.LogMessage) ([]byte, error)
}

BatchFormatter can format multiple messages at once

type ContextualFormatter

type ContextualFormatter interface {
	types.Formatter

	// WithContext adds contextual information to the formatter
	WithContext(key string, value interface{}) ContextualFormatter

	// ClearContext removes all contextual information
	ClearContext()
}

ContextualFormatter includes contextual information in formatting

type EnhancedFormatter

type EnhancedFormatter interface {
	types.Formatter

	// SetFieldOrder sets the order of fields in output
	SetFieldOrder(fields []string)

	// SetFieldFilter sets which fields to include/exclude
	SetFieldFilter(include []string, exclude []string)

	// SetIndentation sets indentation for structured output
	SetIndentation(indent string)
}

EnhancedFormatter provides enhanced formatting capabilities

type Factory

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

Factory creates formatter instances

func NewFactory

func NewFactory() *Factory

NewFactory creates a new formatter factory with default formatters registered

func (*Factory) CreateFormatter

func (f *Factory) CreateFormatter(name string) (types.Formatter, error)

CreateFormatter creates a formatter by name

func (*Factory) CreateFormatterByType

func (f *Factory) CreateFormatterByType(formatType int) (types.Formatter, error)

CreateFormatterByType creates a formatter by type constant

func (*Factory) ListFormatters

func (f *Factory) ListFormatters() []string

ListFormatters returns the names of all registered formatters

func (*Factory) Register

func (f *Factory) Register(name string, constructor FormatterConstructor) error

Register registers a new formatter constructor

type FormatOption

type FormatOption int

FormatOption defines format option types

type FormatOptions

type FormatOptions struct {
	TimestampFormat string
	IncludeLevel    bool
	IncludeTime     bool
	LevelFormat     LevelFormat
	IndentJSON      bool
	FieldSeparator  string
	TimeZone        *time.Location
	FlattenFields   bool // Whether to flatten nested fields in JSON output
	IncludeSource   bool // Whether to include source field
	IncludeHost     bool // Whether to include hostname field
}

FormatOptions controls the output format

func DefaultFormatOptions

func DefaultFormatOptions() FormatOptions

DefaultFormatOptions returns default formatting options

type FormatterConstructor

type FormatterConstructor func() (types.Formatter, error)

FormatterConstructor is a function that creates a formatter

type JSONFormatter

type JSONFormatter struct {
	Options       FormatOptions
	IncludeFields []string // Optional: specific fields to include
	ExcludeFields []string // Optional: fields to exclude
}

JSONFormatter formats log messages as JSON

func NewJSONFormatter

func NewJSONFormatter() *JSONFormatter

NewJSONFormatter creates a new JSON formatter

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(msg types.LogMessage) ([]byte, error)

Format formats a log message as JSON

func (*JSONFormatter) WithExcludeFields

func (f *JSONFormatter) WithExcludeFields(fields ...string) *JSONFormatter

WithExcludeFields sets fields to exclude from JSON output

func (*JSONFormatter) WithIncludeFields

func (f *JSONFormatter) WithIncludeFields(fields ...string) *JSONFormatter

WithIncludeFields sets fields to include in JSON output

type LevelFormat

type LevelFormat int

LevelFormat defines level format options

const (
	// LevelFormatName formats levels as their names (DEBUG, INFO, etc)
	LevelFormatName LevelFormat = iota
	// LevelFormatNameUpper formats levels as uppercase names
	LevelFormatNameUpper
	// LevelFormatNameLower formats levels as lowercase names
	LevelFormatNameLower
	// LevelFormatSymbol formats levels as single-character symbols
	LevelFormatSymbol
)

type StreamFormatter

type StreamFormatter interface {
	types.Formatter

	// StartStream initializes streaming output
	StartStream() ([]byte, error)

	// EndStream finalizes streaming output
	EndStream() ([]byte, error)
}

StreamFormatter formats messages for streaming output

type TextFormatter

type TextFormatter struct {
	Options FormatOptions
}

TextFormatter formats log messages as human-readable text

func NewTextFormatter

func NewTextFormatter() *TextFormatter

NewTextFormatter creates a new text formatter

func (*TextFormatter) Format

func (f *TextFormatter) Format(msg types.LogMessage) ([]byte, error)

Format formats a log message as text

func (*TextFormatter) FormatFields

func (f *TextFormatter) FormatFields(fields map[string]interface{}) string

FormatFields formats fields as key=value pairs

Jump to

Keyboard shortcuts

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