formatter

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 16 Imported by: 2

Documentation

Overview

Formatter is used to format log events to a format which can logged to a console, file, ...

Index

Constants

View Source
const (
	// DefaultKeyLevel is the default key to write the level of log entries to
	// the output with. See Json.KeyLevel for more information.
	DefaultKeyLevel = "level"
)

Variables

View Source
var (
	// DefaultTimeLayout is the default format to format times of log entries
	// with. See Text.TimeLayout for more information.
	DefaultTimeLayout = "15:04:05.000"

	// DefaultLevelWidth is the default width of levels. See Text.LevelWidth for
	// more information.
	DefaultLevelWidth = int8(-5)

	// DefaultMinMessageWidth is the default width of messages. See
	// Text.LevelWidth for more information.
	DefaultMinMessageWidth = int16(50)

	// DefaultMultiLineMessageAfterFields is default setting if multiline
	// messages should be printed after fields. See
	// Text.MultiLineMessageAfterFields for more information.
	DefaultMultiLineMessageAfterFields = true

	// DefaultAllowMultiLineMessage is default setting if multiline should be
	// allowed to be multilines. See Text.AllowMultiLineMessage for more
	// information.
	DefaultAllowMultiLineMessage = false

	// DefaultPrintRootLogger is default setting if the root logger field should
	// be logged. See Text.PrintRootLogger / Json.PrintRootLogger for more
	// information.
	DefaultPrintRootLogger = false
)

Functions

This section is empty.

Types

type Aware

type Aware interface {
	// GetFormatter returns the current formatter.
	GetFormatter() Formatter
}

Aware describes an object that is aware of a Formatter and exports its current state.

type Formatter

type Formatter interface {
	// Format formats the given event to a format which can logged to a console,
	// file, ...
	Format(log.Event, log.Provider, hints.Hints) ([]byte, error)
}

Formatter is used to format log events to a format which can logged to a console, file, ...

var Default Formatter = NewText()

Default is the default instance of Formatter which should cover the most of the cases.

func NewFacade added in v0.5.0

func NewFacade(provider func() Formatter) Formatter

NewFacade creates a new facade instance of Formatter using the given provider.

func Noop added in v0.9.0

func Noop() Formatter

Noop provides a noop implementation of Formatter.

type Func added in v0.9.0

type Func func(log.Event, log.Provider, hints.Hints) ([]byte, error)

Func is wrapping the given function into a Formatter.

func (Func) Format added in v0.9.0

func (instance Func) Format(event log.Event, provider log.Provider, h hints.Hints) ([]byte, error)

Format implements Formatter.Format()

type Json

type Json struct {
	// KeyLevel is the key to write the level of log entries to the output with.
	// If not set DefaultKeyLevel is used.
	KeyLevel string

	// LevelFormatter is used to format the level.Level of a given log.Entry.
	// into the field with key of KeyLevel.
	LevelFormatter Level

	// PrintRootLogger will (if set to true) also print the field logger for the
	// root logger. If set to false the logger field will be only printed for
	// every logger but not for the root one. If not set set
	// DefaultPrintRootLogger will be used.
	PrintRootLogger *bool

	// KeySorter will force the printed fields to be sorted using this sorter.
	// The fields which contains the level.Level will be always the first,
	// regardless of the result of the KeySorter. If this field is empty the
	// fields are not sorted and the order is not deterministic and reliable.
	KeySorter fields.KeySorter
}

Json is an implementation of Formatter which formats given log entries in a JSON format (https://en.wikipedia.org/wiki/JSON) where every log.Entry is one line in the output.

func NewJson

func NewJson(customizer ...func(*Json)) *Json

NewJson creates a new instance of Text which is ready to use.

func (*Json) Format

func (instance *Json) Format(event log.Event, using log.Provider, _ hints.Hints) ([]byte, error)

Format implements Formatter.Format()

type Level added in v0.10.0

type Level interface {
	// FormatLevel formats the given level.Level.
	FormatLevel(in level.Level, using log.Provider) (interface{}, error)
}

Level is used to format a given level.Level.

var DefaultLevel Level = NewNamesBasedLevel(level.NewNamesFacade(func() level.Names {
	return nlevel.DefaultNames
}))

DefaultLevel is the default instance of Level which should cover the most of the cases.

func NewLevelFacade added in v0.10.0

func NewLevelFacade(provider func() Level) Level

NewFacade creates a new facade instance of Formatter using the given provider.

func NewNamesBasedLevel added in v0.10.0

func NewNamesBasedLevel(names nlevel.Names) Level

NewNamesBasedLevel creates a new instance of Level which uses given nlevel.Names to resolve the name of a given log.Level and format it with it.

func NewOrdinalBasedLevel added in v0.10.0

func NewOrdinalBasedLevel() Level

NewNamesBasedLevel creates a new instance of Level which formats the given level.Level by its ordinal.

func NoopLevel added in v0.10.0

func NoopLevel() Level

NoopLevel provides a noop implementation of Level.

type LevelFunc added in v0.10.0

type LevelFunc func(in level.Level, using log.Provider) (interface{}, error)

LevelFunc is wrapping the given function into a Level.

func (LevelFunc) FormatLevel added in v0.10.0

func (instance LevelFunc) FormatLevel(in level.Level, using log.Provider) (interface{}, error)

FormatLevel implements Level.FormatLevel()

type MutableAware added in v0.20.0

type MutableAware interface {
	Aware

	// SetFormatter modifies the current formatter to the given one.
	SetFormatter(Formatter)
}

MutableAware is similar to Aware but additionally is able to modify the Formatter by calling SetFormatter(Formatter).

type QuoteType

type QuoteType uint8

QuoteType defines how values should be quoted.

const (
	// QuoteTypeNormal defines that values are quoted how it is expected due to
	// their types; string="<string>", int=<int>, ...
	QuoteTypeNormal QuoteType = 0

	// QuoteTypeMinimal defines that values are only quoted if required. When it
	// is possible quoting will be prevented; foo="hello\"" bar=world
	QuoteTypeMinimal QuoteType = 1

	// QuoteTypeEverything forces to everything if required or not.
	QuoteTypeEverything QuoteType = 2
)

type SimpleTextValue added in v0.10.0

type SimpleTextValue struct {
	// QuoteType defines how values are quoted.
	QuoteType QuoteType
}

SimpleTextValue is a simple implementation of TextValue.

func NewSimpleTextValue added in v0.10.0

func NewSimpleTextValue(customizer ...func(*SimpleTextValue)) *SimpleTextValue

NewSimpleTextValue creates a new instance of SimpleTextValue which is ready to use.

func (*SimpleTextValue) FormatTextValue added in v0.10.0

func (instance *SimpleTextValue) FormatTextValue(v interface{}, _ log.Provider) ([]byte, error)

FormatTextValue implements TextValue.FormatTextValue().

type Template added in v0.20.0

type Template struct {
	// ColorMode defines when the output should be colorized. If not configured
	// color.ModeAuto will be used by default.
	ColorMode color.Mode

	// LevelColorizer is used to colorize output based on the level.Level of an
	// log.Event to be logged. If not set nlevel.DefaultColorizer will be used.
	LevelColorizer nlevel.Colorizer
	// contains filtered or unexported fields
}

Template is an implementation of Formatter which formats given log entries in a human readable format, formatted by a template it was created with.

func MustNewTemplate added in v0.20.0

func MustNewTemplate(plain string, customizer ...func(*Template)) *Template

MustNewTemplate is same as NewTemplate but will panic in case of errors.

func MustNewTemplateByFactory added in v0.20.0

func MustNewTemplateByFactory(factory TemplateFactory, customizer ...func(*Template)) *Template

MustNewTemplateByFactory is same as NewTemplateByFactory but will panic in case of errors.

func MustNewTemplateWithFuncMap added in v0.20.0

func MustNewTemplateWithFuncMap(plain string, funcMap template.FuncMap, customizer ...func(*Template)) *Template

MustNewTemplateWithFuncMap is same as NewTemplateWithFuncMap but will panic in case of errors.

func NewTemplate added in v0.20.0

func NewTemplate(plain string, customizer ...func(*Template)) (*Template, error)

NewTemplate creates a new instance of Template which is ready to use for the given plain template.

func NewTemplateByFactory added in v0.20.0

func NewTemplateByFactory(factory TemplateFactory, customizer ...func(*Template)) (*Template, error)

NewTemplateByFactory creates a new instance of Template which is ready to use using the given factory.

func NewTemplateWithFuncMap added in v0.20.0

func NewTemplateWithFuncMap(plain string, funcMap template.FuncMap, customizer ...func(*Template)) (*Template, error)

NewTemplateWithFuncMap creates a new instance of Template which is ready to use for the given plain template and funcMap.

func (*Template) Format added in v0.20.0

func (instance *Template) Format(event log.Event, using log.Provider, h hints.Hints) ([]byte, error)

Format implements Formatter.Format()

type TemplateFactory added in v0.20.0

type TemplateFactory func(rootFuncMap template.FuncMap) (*template.Template, error)

TemplateFactory will create for the given rootFuncMap a new instance of template.Template.

type TemplateRenderingContext added in v0.20.0

type TemplateRenderingContext struct {
	// log.Event is the foundation of this context.
	log.Event

	// Provider contains the corresponding log.Provider of the current log.Event.
	Provider log.Provider

	// Template is the actual instance of Template which which is executing the
	// rendering of the log.Event.
	Template *Template
	// contains filtered or unexported fields
}

TemplateRenderingContext is used by Template.Format as a context object while rendering the log.Event.

func (TemplateRenderingContext) Error added in v0.20.0

func (instance TemplateRenderingContext) Error() error

Error is a convenience method to easy return the current error of the corresponding log.Event, can be nil.

func (TemplateRenderingContext) FieldKeysSpec added in v0.20.0

func (instance TemplateRenderingContext) FieldKeysSpec() fields.KeysSpec

FieldKeysSpec is a convenience method to easy return the current fields.KeysSpec of the corresponding log.Event.

func (TemplateRenderingContext) Fields added in v0.20.0

func (instance TemplateRenderingContext) Fields() (result map[string]interface{}, err error)

Fields returns all fields of the log.Event (except the default ones).

func (TemplateRenderingContext) Hints added in v0.20.0

func (instance TemplateRenderingContext) Hints() hints.Hints

Hints provides the hints.Hints the current log.Event should be rendered with.

func (TemplateRenderingContext) Level added in v0.20.0

func (instance TemplateRenderingContext) Level() level.Level

Level is a convenience method to easy return the current level.Level of the corresponding log.Event.

func (TemplateRenderingContext) LevelName added in v0.20.0

func (instance TemplateRenderingContext) LevelName() (string, error)

LevelName is a convenience method to easy return the current level.Level of the corresponding log.Event.

func (TemplateRenderingContext) LevelNames added in v0.20.0

func (instance TemplateRenderingContext) LevelNames() nlevel.Names

LevelNames is a convenience method to easy return the current nlevel.Names of the corresponding log.Provider.

func (TemplateRenderingContext) Logger added in v0.20.0

func (instance TemplateRenderingContext) Logger() *string

Logger is a convenience method to easy return the current logger name of the corresponding log.Event, can be nil.

func (TemplateRenderingContext) Message added in v0.20.0

func (instance TemplateRenderingContext) Message() *string

Message is a convenience method to easy return the current message of the corresponding log.Event, can be nil.

func (TemplateRenderingContext) Timestamp added in v0.20.0

func (instance TemplateRenderingContext) Timestamp() *time.Time

Timestamp is a convenience method to easy return the current timestamp of the corresponding log.Event, can be nil.

type Text added in v0.9.0

type Text struct {
	// ColorMode defines when the output should be colorized. If not configured
	// color.ModeAuto will be used by default.
	ColorMode color.Mode

	// LevelColorizer is used to colorize output based on the level.Level of an
	// log.Event to be logged. If not set nlevel.DefaultColorizer will be used.
	LevelColorizer nlevel.Colorizer

	// TimeLayout defines how the time of log events should be formatted. Please
	// see time.Time#Format() for more details. If not set DefaultTimeLayout
	// will be used.
	TimeLayout string

	// LevelWidth defines the width of the string representation of the level
	// which will be printed to the output. If set to 0 the length will be kept
	// as it is. If bigger than 0 and has the exact same length of the printed
	// text; it will be printed as it is. If the representation is longer it
	// will be trimmed; if shorter whitespaces will be added. If smaller than 0
	// it behaves the same way but whitespaces will be added at the beginning
	// instead of the end. If not set DefaultLevelWidth will be used.
	LevelWidth *int8

	// MinMessageWidth defines the width of the message which will be printed to
	// the output. If set to 0 the length will be kept as it is. If bigger than
	// 0 and has the exact same length of the printed text or the printed text
	// will be longer ; it will be printed as it is. If the message is shorter
	// whitespaces will be added to the end. If smaller than 0 it behaves the
	// same way but whitespaces will be added at the beginning instead of the
	// end. If not set DefaultMinMessageWidth will be used.
	MinMessageWidth *int16

	// MultiLineMessageAfterFields will force multiline messages
	// (if set to true) to be printed behind the fields; instead (if default)
	// in front of them. If not set DefaultMultiLineMessageAfterFields will
	// be used.
	MultiLineMessageAfterFields *bool

	// AllowMultiLineMessage will allow (if set to true) multiline messages to
	// be printed as multiline to the output, too. If set to false linebreaks
	// will be replaced with ⏎. If not set DefaultAllowMultiLineMessage will
	// be used.
	AllowMultiLineMessage *bool

	// PrintRootLogger will (if set to true) also print the field logger for the
	// root logger. If set to false the logger field will be only printed for
	// every logger but not for the root one. If not set
	// DefaultPrintRootLogger will be used.
	PrintRootLogger *bool

	// ValueFormatter is used to format the field values (not the message). If
	// not set formatter.DefaultTextValue will be used.
	ValueFormatter TextValue

	// KeySorter is used to sort the field when they are printed. If not set
	// fields.DefaultKeySorter will be used.
	KeySorter fields.KeySorter
	// contains filtered or unexported fields
}

Text is an implementation of Formatter which formats given log entries in a human-readable format. Additionally, it can also colorize the formatted output.

func NewText added in v0.9.0

func NewText(customizer ...func(*Text)) *Text

NewText creates a new instance of Text which is ready to use.

func (*Text) Format added in v0.9.0

func (instance *Text) Format(event log.Event, using log.Provider, h hints.Hints) ([]byte, error)

Format implements Formatter.Format()

type TextValue added in v0.10.0

type TextValue interface {
	// FormatValue formats the given value to a readable format.
	FormatTextValue(value interface{}, provider log.Provider) ([]byte, error)
}

TextValue formats a given value to be printed in the text.

var DefaultTextValue TextValue = NewSimpleTextValue()

DefaultTextValue is the default instance of TextValue which should cover the most of the cases.

func NewTextValueFacade added in v0.10.0

func NewTextValueFacade(provider func() TextValue) TextValue

NewTextValueFacade creates a new facade instance of TextValue using the given provider.

func NoopTextValue added in v0.10.0

func NoopTextValue() TextValue

NoopTextValue provides a noop implementation of TextValue.

type TextValueFunc added in v0.10.0

type TextValueFunc func(interface{}, log.Provider) ([]byte, error)

TextValueFunc is wrapping the given function into a TextValue.

func (TextValueFunc) FormatTextValue added in v0.10.0

func (instance TextValueFunc) FormatTextValue(value interface{}, provider log.Provider) ([]byte, error)

FormatTextValue implements TextValue.FormatTextValue().

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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