log

package
v16.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package log provides an opiniated logging facility as it provides only 4 log levels.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BufferWriter

type BufferWriter interface {
	Writer
	Events() []*Event
}

func NewBufferWriter

func NewBufferWriter(level Level, lines int) BufferWriter

type Event

type Event struct {
	Time      time.Time
	Level     Level
	Component string
	Caller    string
	Message   string

	Data Fields
	// contains filtered or unexported fields
}

func (*Event) Debug

func (e *Event) Debug() Logger

func (*Event) Error

func (e *Event) Error() Logger

func (*Event) Info

func (e *Event) Info() Logger

func (*Event) Log

func (e *Event) Log(format string, args ...interface{})

func (*Event) Warn

func (e *Event) Warn() Logger

func (*Event) WithComponent

func (e *Event) WithComponent(component string) Logger

func (*Event) WithError

func (e *Event) WithError(err error) Logger

func (*Event) WithField

func (e *Event) WithField(key string, value interface{}) Logger

func (*Event) WithFields

func (e *Event) WithFields(f Fields) Logger

func (*Event) WithOutput

func (e *Event) WithOutput(w Writer) Logger

func (*Event) Write

func (l *Event) Write(p []byte) (int, error)

type Eventx

type Eventx struct {
	Time      time.Time   `json:"ts"`
	Level     Level       `json:"level"`
	Component string      `json:"component"`
	Reference string      `json:"ref"`
	Message   string      `json:"message"`
	Caller    string      `json:"caller"`
	Detail    interface{} `json:"detail"`
}

type Fields

type Fields map[string]interface{}

type Formatter

type Formatter interface {
	Bytes(e *Event) []byte
	String(e *Event) string
}

func NewConsoleFormatter

func NewConsoleFormatter(useColor bool) Formatter

func NewJSONFormatter

func NewJSONFormatter() Formatter

type Level

type Level uint

LogLevel represents a log level

const (
	Lsilent Level = 0
	Lerror  Level = 1
	Lwarn   Level = 2
	Linfo   Level = 3
	Ldebug  Level = 4
)

func (*Level) MarshalJSON

func (level *Level) MarshalJSON() ([]byte, error)

func (Level) String

func (level Level) String() string

String returns a string representing the log level.

type LevelRewriteRule

type LevelRewriteRule struct {
	Level     Level
	Component string
	Match     map[string]string
}

type Logger

type Logger interface {
	// WithOutput adds an output to the Logger. The messages are written to the
	// provided writer if the log level of the message is more or equally critical
	// than level. Pass true for useColoe if colored output is desired. If the
	// writer doesn't support colored output, it will be automatically disabled.
	// The returned value implements the LoggerOutput interface which allows to
	// change the log level at any later point in time.
	WithOutput(w Writer) Logger

	// With returns a new Logger with the given context. The context may
	// printed along the message. This is up to the implementation.
	WithComponent(component string) Logger

	WithField(key string, value interface{}) Logger
	WithFields(fields Fields) Logger

	WithError(err error) Logger

	Log(format string, args ...interface{})

	// Debug writes a message with the debug log level to all registered outputs.
	// The message will be written according to fmt.Printf(). The detail field will
	// be reset to nil.
	Debug() Logger

	// Info writes a message with the info log level to all registered outputs.
	// The message will be written according to fmt.Printf(). The detail field will
	// be reset to nil.
	Info() Logger

	// Warn writes a message with the warn log level to all registered outputs.
	// The message will be written according to fmt.Printf(). The detail field will
	// be reset to nil.
	Warn() Logger

	// Error writes a message with the error log level to all registered outputs.
	// The message will be written according to fmt.Printf(). The detail field will
	// be reset to nil.
	Error() Logger

	// Write implements the io.Writer interface such that it can be used in e.g. the
	// the log/Logger facility. Messages will be printed with debug level.
	Write(p []byte) (int, error)
}

Logger is an interface that provides means for writing log messages.

There are 4 log levels available (debug, info, warn, error) with increasing severity. A message will be written to an output if the log level of the message has the same or a higher severity than the output. Otherwise it will be discarded.

Different outputs should be added with the AddOutput function.

The context is a string that represents who wrote the message.

func New

func New(component string) Logger

New returns an implementation of the Logger interface.

type Writer

type Writer interface {
	Write(e *Event) error
}

func NewConsoleWriter

func NewConsoleWriter(w io.Writer, level Level, useColor bool) Writer

func NewJSONWriter

func NewJSONWriter(w io.Writer, level Level) Writer

func NewLevelRewriter

func NewLevelRewriter(writer Writer, rules []LevelRewriteRule) Writer

func NewMultiWriter

func NewMultiWriter(writer ...Writer) Writer

func NewSyncWriter

func NewSyncWriter(writer Writer) Writer

func NewTopicWriter

func NewTopicWriter(writer Writer, topics []string) Writer

Jump to

Keyboard shortcuts

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