log

package
v0.128.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, logger *Logger) context.Context

ContextWithLogger returns a new context with the given logger attached. Use FromContext to retrieve it.

func JSONStreamer

func JSONStreamer(r io.Reader, log *Logger, defaultLevel Level) error

JSONStreamer reads a stream of JSON log entries from r and logs them to log.

If a line of JSON is invalid an entry is created at the defaultLevel.

func LevelStrings

func LevelStrings() []string

LevelStrings returns a slice of all String values of the enum

Types

type Config

type Config struct {
	Level Level `help:"Log level." default:"info" env:"LOG_LEVEL"`
	JSON  bool  `help:"Log in JSON format." env:"LOG_JSON"`
}

Config for the logger.

type Entry

type Entry struct {
	Time       time.Time         `json:"-"`
	Level      Level             `json:"level"`
	Attributes map[string]string `json:"attributes,omitempty"`
	Message    string            `json:"message"`

	Error error `json:"-"`
}

type Interface

type Interface interface {
	Log(entry Entry)
	Logf(level Level, format string, args ...interface{})
	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	// Errorf conditionally logs an error. If err is nil, nothing is logged.
	Errorf(err error, format string, args ...interface{})
}

type Level

type Level int

Level is the log level.

const (
	// Default is a special value that means the log level will use a default.
	Default Level = 0
	Trace   Level = 1
	Debug   Level = 5
	Info    Level = 9
	Warn    Level = 13
	Error   Level = 17
)

Log levels.

func LevelString

func LevelString(s string) (Level, error)

LevelString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func LevelValues

func LevelValues() []Level

LevelValues returns all values of the enum

func ParseLevel added in v0.5.0

func ParseLevel(input string) (Level, error)

ParseLevel parses a log level from text.

func (Level) IsALevel

func (i Level) IsALevel() bool

IsALevel returns "true" if the value is listed in the enum definition. "false" otherwise

func (Level) MarshalText

func (i Level) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Level

func (Level) Severity

func (l Level) Severity() int

Severity returns the open telemetry severity of the log level.

func (Level) String

func (i Level) String() string

func (*Level) UnmarshalText

func (i *Level) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for Level

type Logger

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

Logger is the concrete logger.

func Configure

func Configure(w io.Writer, cfg Config) *Logger

Configure returns a new logger based on the config.

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext retrieves the current logger from the context or panics

func New

func New(level Level, sink Sink) *Logger

New returns a new logger.

func (Logger) AddSink

func (l Logger) AddSink(sink Sink) *Logger

func (Logger) Attrs added in v0.105.0

func (l Logger) Attrs(attributes map[string]string) *Logger

Attrs creates a new logger with the given attributes.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(err error, format string, args ...interface{})

func (Logger) GetLevel added in v0.33.1

func (l Logger) GetLevel() Level

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

func (Logger) Level

func (l Logger) Level(level Level) *Logger

func (*Logger) Log

func (l *Logger) Log(entry Entry)

func (*Logger) Logf

func (l *Logger) Logf(level Level, format string, args ...interface{})

func (Logger) Scope added in v0.1.1

func (l Logger) Scope(scope string) *Logger

func (*Logger) Tracef

func (l *Logger) Tracef(format string, args ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

func (*Logger) WriterAt

func (l *Logger) WriterAt(level Level) *io.PipeWriter

WriterAt returns a writer that logs each line at the given level.

type Sink

type Sink interface {
	Log(entry Entry) error
}

func Tee

func Tee(sinks ...Sink) Sink

Tee returns a sink that writes to all of the given sinks.

Jump to

Keyboard shortcuts

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