log

package
v1.0.0-dev.46 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package log provides a generic interface around loggers.

The log package must be used in conjunction with a logger in contrib package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logs a message at debug level.

func Debugf

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

Debugf logs a templated message at debug level.

For templating details see implementation doc.

func Error

func Error(args ...interface{})

Error logs a message at error level.

func Errorf

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

Errorf logs a templated message at error level.

For templating details see implementation doc.

func Fatal

func Fatal(args ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

func Info

func Info(args ...interface{})

Info logs a message at info level.

func Infof

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

Infof logs a templated message at info level.

For templating details see implementation doc.

func Panic

func Panic(args ...interface{})

Panic is equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, args ...interface{})

Panicf is equivalent to Printf() followed by a call to panic().

func Printf

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

Printf logs a templated message.

For templating details see implementation doc.

func Set

func Set(logger Logger)

Set returns an instance of logger.

func ToContext

func ToContext(ctx context.Context) context.Context
Example
bar := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")
	logger.Infof("%s method.", "bar")
}

foo := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")

	ctx = logger.ToContext(ctx)

	bar(ctx)
}

withoutContext := func() {
	log.Info("withoutContext method")
}

ctx := context.Background()
log.Set(logrus.NewLogger(
	logrus.WithFormatter(text.New(text.WithDisableTimestamp(true))),
).WithField("main_field", "example"))

ctx = log.ToContext(ctx)

foo(ctx)

withoutContext()
Output:

level=info msg="foo method." foo_field=example main_field=example
level=info msg="bar method." bar_field=example foo_field=example main_field=example
level=info msg="withoutContext method" main_field=example

func Trace

func Trace(args ...interface{})

Trace logs a message at trace level.

func Tracef

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

Tracef logs a templated message at trace level.

For templating details see implementation doc.

func Warn

func Warn(args ...interface{})

Warn logs a message at warn level.

func Warnf

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

Warnf logs a templated message at warn level.

For templating details see implementation doc.

Types

type Fields

type Fields map[string]interface{}

Fields to pass when we want to call WithFields for structured logging.

type Logger

type Logger interface {
	Printf(format string, args ...interface{})

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

	Trace(args ...interface{})

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

	Debug(args ...interface{})

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

	Info(args ...interface{})

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

	Warn(args ...interface{})

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

	Error(args ...interface{})

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

	Fatal(args ...interface{})

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

	Panic(args ...interface{})

	WithFields(keyValues map[string]interface{}) Logger

	WithField(key string, value interface{}) Logger

	WithError(err error) Logger

	WithTypeOf(obj interface{}) Logger

	ToContext(ctx context.Context) context.Context

	FromContext(ctx context.Context) Logger

	Output() io.Writer

	Fields() Fields
}

Logger is our contract for the logger.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext calls concrete Logger.FromContext().

Example
bar := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")
	logger.Infof("%s method.", "bar")
}

foo := func(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")

	ctx = logger.ToContext(ctx)

	bar(ctx)
}

withoutContext := func() {
	log.Info("withoutContext method")
}

ctx := context.Background()
log.Set(logrus.NewLogger(
	logrus.WithFormatter(text.New(text.WithDisableTimestamp(true))),
).WithField("main_field", "example"))

ctx = log.ToContext(ctx)

foo(ctx)

withoutContext()
Output:

level=info msg="foo method." foo_field=example main_field=example
level=info msg="bar method." bar_field=example foo_field=example main_field=example
level=info msg="withoutContext method" main_field=example

func GetLogger

func GetLogger() Logger

GetLogger returns instance of Logger.

func WithError

func WithError(err error) Logger

WithError adds an error as a field to logger

func WithField

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

WithField adds a key and value to logger.

func WithFields

func WithFields(keyValues map[string]interface{}) Logger

WithFields adds fields to logger.

func WithTypeOf

func WithTypeOf(obj interface{}) Logger

WithTypeOf adds type information to logger.

type Noop

type Noop struct{}

Noop is a dummy implementation of Logger

func NewNoop

func NewNoop() Noop

NewNoop create a Noop Logger

func (Noop) Debug

func (n Noop) Debug(args ...interface{})

func (Noop) Debugf

func (n Noop) Debugf(format string, args ...interface{})

func (Noop) Error

func (n Noop) Error(args ...interface{})

func (Noop) Errorf

func (n Noop) Errorf(format string, args ...interface{})

func (Noop) Fatal

func (n Noop) Fatal(args ...interface{})

func (Noop) Fatalf

func (n Noop) Fatalf(format string, args ...interface{})

func (Noop) Fields

func (n Noop) Fields() Fields

func (Noop) FromContext

func (n Noop) FromContext(ctx context.Context) Logger

func (Noop) Info

func (n Noop) Info(args ...interface{})

func (Noop) Infof

func (n Noop) Infof(format string, args ...interface{})

func (Noop) Output

func (n Noop) Output() io.Writer

func (Noop) Panic

func (n Noop) Panic(args ...interface{})

func (Noop) Panicf

func (n Noop) Panicf(format string, args ...interface{})

func (Noop) Printf

func (n Noop) Printf(format string, args ...interface{})

func (Noop) ToContext

func (n Noop) ToContext(ctx context.Context) context.Context

func (Noop) Trace

func (n Noop) Trace(args ...interface{})

func (Noop) Tracef

func (n Noop) Tracef(format string, args ...interface{})

func (Noop) Warn

func (n Noop) Warn(args ...interface{})

func (Noop) Warnf

func (n Noop) Warnf(format string, args ...interface{})

func (Noop) WithError

func (n Noop) WithError(err error) Logger

func (Noop) WithField

func (n Noop) WithField(key string, value interface{}) Logger

func (Noop) WithFields

func (n Noop) WithFields(keyValues map[string]interface{}) Logger

func (Noop) WithTypeOf

func (n Noop) WithTypeOf(obj interface{}) Logger

Jump to

Keyboard shortcuts

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