logging

package
v0.0.0-...-8b7d7e7 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: Apache-2.0 Imports: 17 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCallEnsureLoggerFirst = errors.New("call EnsureLogger() before LoggerFrom() method")

ErrCallEnsureLoggerFirst is returned when LoggerFrom() is called before EnsureLogger().

Functions

func ElapsedMillisTimeEncoder

func ElapsedMillisTimeEncoder(setupTime time.Time) zapcore.TimeEncoder

func EnsureLogFile

func EnsureLogFile(ctx context.Context) context.Context

EnsureLogFile ensures that a log file is present in the context. If not, it creates one and adds it to the context. The file's Closer is also added to the context if not already present.

func EnsureLogger

func EnsureLogger(ctx context.Context, fields ...Fields) context.Context

EnsureLogger ensures that a logger is attached to the context. The returned context will have a logger attached to it. Given fields will be added to the logger, either new or existing.

func LogFileFrom

func LogFileFrom(ctx context.Context) *os.File

func WithFatalCaptured

func WithFatalCaptured(fn func()) []any

WithFatalCaptured runs the given function and captures the arguments passed to Fatal function. It is useful in tests to verify that the expected error was reported.

func WithLogFile

func WithLogFile(ctx context.Context, f *os.File) context.Context

func WithLogFileCloser

func WithLogFileCloser(ctx context.Context, closer Closer) context.Context

func WithLogger

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

WithLogger attaches the given logger to the context.

Types

type Closer

type Closer func() error

func LogFileCloserFrom

func LogFileCloserFrom(ctx context.Context) Closer

type Fields

type Fields map[string]any

Fields is a map. It is used to add structured context to the logging output.

type Logger

type Logger interface {
	// WithName returns a new Logger with the given name.
	WithName(name string) Logger

	// WithFields returns a new Logger with the given fields.
	WithFields(fields Fields) Logger

	// Debug logs a message at the debug level.
	Debug(args ...any)
	// Info logs a message at the info level.
	Info(args ...any)
	// Warn logs a message at the warn level.
	Warn(args ...any)
	// Error logs a message at the error level.
	Error(args ...any)
	// Fatal logs a message at the fatal level and then exit the program.
	Fatal(args ...any)

	// Debugf logs a message at the debug level using given format.
	Debugf(format string, args ...any)
	// Infof logs a message at the info level using given format.
	Infof(format string, args ...any)
	// Warnf logs a message at the warn level using given format.
	Warnf(format string, args ...any)
	// Errorf logs a message at the error level using given format.
	Errorf(format string, args ...any)
	// Fatalf logs a message at the fatal level using given format and then
	// exit the program.
	Fatalf(format string, args ...any)
}

Logger is the interface for logging, similar to the Uber's zap.Logger.

func LoggerFrom

func LoggerFrom(ctx context.Context) Logger

LoggerFrom returns the logger from the context. If EnsureLogger() was not called before, it will panic.

Jump to

Keyboard shortcuts

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