logger

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 6 Imported by: 18

README

GO License GoDoc Go Report Card

logger

A configurable structure leveled logger stored in context with an adapter interface for logger libraries allows not to depend on any of them directly.

Documentation

Index

Constants

View Source
const (
	// LevelDebug is a debug level.
	LevelDebug = slog.LevelDebug
	// LevelInfo is an info level.
	LevelInfo = slog.LevelInfo
	// LevelWarn is a warn level.
	LevelWarn = slog.LevelWarn
	// LevelError is an error level.
	LevelError = slog.LevelError
	// LevelFatal is a fatal level.
	LevelFatal = slog.Level(12)
)

Variables

AllLevels is a slice of all levels.

Functions

func ContextWithLogger

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

ContextWithLogger adds Logger to context and returns new context.

func Debug

func Debug(ctx context.Context, msg string)

Debug prints debug-level log message. Usually very verbose and shown only when debug level is enabled.

func Error

func Error(ctx context.Context, msg string)

Error prints error-level log message.

func Fatal

func Fatal(ctx context.Context, msg string)

Fatal prints fatal-level log message and exit the program with code 1.

func Info

func Info(ctx context.Context, msg string)

Info prints info-level log message.

func Warn

func Warn(ctx context.Context, msg string)

Warn prints warn-level log message.

Types

type Fields

type Fields map[string]interface{}

Fields type, used to pass to `WithFields`.

type Level added in v0.4.0

type Level = slog.Level

Level type.

func ParseLevel added in v1.0.0

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the Logrus log level constant.

type Logger

type Logger interface {
	Debug(msg string)
	Info(msg string)
	Warn(msg string)
	Error(msg string)
	Fatal(msg string)

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

Logger serves as an adapter interface for logger libraries so that we not depend on any of them directly.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext extracts Logger from context. If no instance found - returns Logger from default logInstance.

func Init

func Init(ctx context.Context, p Params) Logger

Init initiates logger and add format options. Should be called only once, on start of app.

func WithError

func WithError(ctx context.Context, err error) Logger

WithError adds an error as single field to the Logger Entry.

func WithField

func WithField(ctx context.Context, key string, value interface{}) Logger

WithField adds s single field to the Logger Entry.

func WithFields

func WithFields(ctx context.Context, fields map[string]interface{}) Logger

WithFields adds fields key-value to the Logger Entry.

type Params

type Params struct {
	// Writer is a writer to write logs to. By default, it's os.Stderr.
	Writer io.WriteCloser
	// Level is one of "debug", "info", "warn", "error", "fatal".
	Level string
	// Format is one of "json" or "text".
	Format string
	// WithSource enables source code info in logs.
	WithSource bool
}

Params holds logger specific params.

Jump to

Keyboard shortcuts

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