logger

package
v0.0.0-...-5206ba3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IndentAndQuoteLines

func IndentAndQuoteLines(buffer, indent string) string

Just like IndentLines, but converts all non-space / non-ascii printable characters to \xHH sequences.

func IndentLines

func IndentLines(buffer, indent string) string

IndentLines indents each line in the buffer with the specified string.

func LogLines

func LogLines(logger Printer, buffer, indent string)

LogLines breaks a buffer into lines and logs each one of them with the specified indentation and printer.

func PrintEvent

func PrintEvent(ev *Event, log Logger)

func SetCtx

func SetCtx(ctx context.Context, l Logger) context.Context

Types

type Accumulator

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

Accumulator is a thread safe object implementing the Logger interface that accumulates all messages being logged.

You can then use the Retrieve() or Forward() methods to access or print the accumulated messages.

func NewAccumulator

func NewAccumulator() *Accumulator

func (*Accumulator) Add

func (dl *Accumulator) Add(prio Priority, format string, args ...interface{})

func (*Accumulator) Debugf

func (dl *Accumulator) Debugf(format string, args ...interface{})

func (*Accumulator) Errorf

func (dl *Accumulator) Errorf(format string, args ...interface{})

func (*Accumulator) Forward

func (dl *Accumulator) Forward(log Logger)

func (*Accumulator) ForwardWithPrinter

func (dl *Accumulator) ForwardWithPrinter(log Logger, printer func(*Event, Logger))

func (*Accumulator) Infof

func (dl *Accumulator) Infof(format string, args ...interface{})

func (*Accumulator) Retrieve

func (dl *Accumulator) Retrieve() []Event

func (*Accumulator) SetOutput

func (dl *Accumulator) SetOutput(writer io.Writer)

func (*Accumulator) Warnf

func (dl *Accumulator) Warnf(format string, args ...interface{})

type DefaultLogger

type DefaultLogger struct {
	Printer Printer
	Setter  func(writer io.Writer)
}

DefaultLogger uses a single Printf to implement the Logger interface.

Printer must be provided. Use log.Printf to rely on default golang logging, with:

logger := &DefaultLoger{Printer: log.Printf}

... or just use the pre-defined `Go` variable bleow for default Go logging.

func (DefaultLogger) Debugf

func (dl DefaultLogger) Debugf(format string, args ...interface{})

func (DefaultLogger) Errorf

func (dl DefaultLogger) Errorf(format string, args ...interface{})

func (DefaultLogger) Infof

func (dl DefaultLogger) Infof(format string, args ...interface{})

func (DefaultLogger) Printf

func (dl DefaultLogger) Printf(format string, args ...interface{})

func (DefaultLogger) SetOutput

func (dl DefaultLogger) SetOutput(output io.Writer)

func (DefaultLogger) Warnf

func (dl DefaultLogger) Warnf(format string, args ...interface{})

type Event

type Event struct {
	Time     time.Time
	Priority Priority
	Message  string
}

Event represents something that was logged.

type Forwardable

type Forwardable interface {
	Forward(Logger)
}

Forwardable is the interface implemented by any logger capable of forwarding, upon request, buffered messages to another logger.

type IndentedError

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

IndentedError reformats the error to have indented new lines.

func NewIndentedError

func NewIndentedError(err error, indent string) *IndentedError

NewIndentedError wraps an error into an object that ensures that when the error is converted to a string and printed, via the Error method, every line of the error message is indented with indent.

For example, let's say you have an error err that converted with fmt.Printf(... %s ...) leads to the following output:

An error occured in executing function, stack trace follows.
  Function1() ...
  Function2() ...

Using NewIndentedError(err, "| "), would lead to:

| An error occured in executing function, stack trace follows.
|   Function1() ...
|   Function2() ...

Further, the error returned by NewIntendedError implements the Unwrap interface, allowing programmatic access to the original error.

func (*IndentedError) Error

func (ie *IndentedError) Error() string

func (*IndentedError) Unwrap

func (ie *IndentedError) Unwrap() error

type Logger

type Logger interface {
	// Use for messages that are pretty much only useful when debugging
	// specific behaviors. Should be used rarely.
	// Expect debug messages to be discarded.
	Debugf(format string, args ...interface{})
	// Use for messages that clearly a) outline a problem that b) the operator MUST act upon.
	// Errors are generally reserved for things that cause the program to fail and must be fixed.
	Errorf(format string, args ...interface{})
	// Use for messages that a) outline a problem that b) is actionable, and c) the operator
	// may want to act upon - even though the program may have recovered or continued without
	// intervention (in degraded mode, or without some functionality, ...).
	Warnf(format string, args ...interface{})
	// Use for messages that are either not actionable, or require no action by the operator.
	Infof(format string, args ...interface{})

	SetOutput(writer io.Writer)
}

Logger is the interface used by the enkit libraries to log messages.

The logrus library can be used out of the box without additional glue. The golang log library is used by default, through DefaultLogger. DefaultLogger can be used to pass an arbitrary log function as well.

Go is a pre-defined logger that will log using default log library.

var Nil Logger = &NilLogger{}

Nil is a pre-defined logger that will discard all the output.

Replacing the Nil global with something else can allow seeing log messages that would normally be discarded.

func GetCtx

func GetCtx(ctx context.Context) Logger

type NilLogger

type NilLogger struct{}

NilLogger is a logger that discards all messages.

Prefer using logger.Nil to instantiating your copy of &NilLogger{}.

func (NilLogger) Debugf

func (dl NilLogger) Debugf(format string, args ...interface{})

func (NilLogger) Errorf

func (dl NilLogger) Errorf(format string, args ...interface{})

func (NilLogger) Infof

func (dl NilLogger) Infof(format string, args ...interface{})

func (NilLogger) Printf

func (dl NilLogger) Printf(format string, args ...interface{})

func (NilLogger) SetOutput

func (dl NilLogger) SetOutput(output io.Writer)

func (NilLogger) Warnf

func (dl NilLogger) Warnf(format string, args ...interface{})

type Printer

type Printer func(format string, args ...interface{})

Printer is a Printf like function.

type Priority

type Priority int

Represents a logging priority. Logging priorities map to the Debugf, Infof, Warnf, and Errorf printers.

const (
	DebugPriority Priority = iota
	InfoPriority
	WarnPriority
	ErrorPriority
)

type Proxy

type Proxy struct {
	Logger
}

Proxy is a simple logger that as long as it is passed by pointer, allows to change the underlying logger implementation.

This allows to initialize some code to use a logging object, to then replace it later after the initialization stage.

func (*Proxy) Replace

func (p *Proxy) Replace(newl Logger)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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