logger

package
v4.0.20 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 10 Imported by: 9

Documentation

Overview

Package logger provides a log interface

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogger variable
	DefaultLogger = NewLogger()
	// DefaultLevel used by logger
	DefaultLevel = InfoLevel
	// DefaultCallerSkipCount used by logger
	DefaultCallerSkipCount = 2
)
View Source
var DefaultContextAttrFuncs []ContextAttrFunc

Functions

func Debug

func Debug(ctx context.Context, msg string, attrs ...interface{})

Debug writes formatted msg to default logger on debug level

func Error

func Error(ctx context.Context, msg string, attrs ...interface{})

Error writes formatted msg to default logger on error level

func Fatal

func Fatal(ctx context.Context, msg string, attrs ...interface{})

Fatal writes formatted msg to default logger on fatal level

func Info

func Info(ctx context.Context, msg string, attrs ...interface{})

Info writes formatted msg to default logger on info level

func Init

func Init(opts ...options.Option) error

Init initialize default logger

func NewContext

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

NewContext stores logger into passed context

func NewStdLogger

func NewStdLogger(l Logger, level Level) *log.Logger

NewStdLogger returns new *log.Logger baked by logger.Logger implementation

func RedirectStdLogger

func RedirectStdLogger(l Logger, level Level) func()

RedirectStdLogger replace *log.Logger with logger.Logger implementation

func Trace

func Trace(ctx context.Context, msg string, attrs ...interface{})

Trace writes formatted msg to default logger on trace level

func V

func V(level Level) bool

V returns true if passed level enabled in default logger

func Warn

func Warn(ctx context.Context, msg string, attrs ...interface{})

Warn writes formatted msg to default logger on warn level

func WithAddSource added in v4.0.17

func WithAddSource(v bool) options.Option

WitAddSource controls writing source file and pos in log

func WithAddStacktrace added in v4.0.17

func WithAddStacktrace(v bool) options.Option

WithAddStacktrace controls writing stacktrace on error

func WithAttrs added in v4.0.13

func WithAttrs(attrs ...interface{}) options.Option

WithAttrs set default fields for the logger

func WithCallerSkipCount

func WithCallerSkipCount(c int) options.Option

WithCallerSkipCount set frame count to skip

func WithContextAttrFuncs added in v4.0.13

func WithContextAttrFuncs(fncs ...ContextAttrFunc) options.Option

WithContextAttrFuncs appends default funcs for the context arrts filler

func WithLevel

func WithLevel(lvl Level) options.Option

WithLevel set default level for the logger

func WithMicroKeys added in v4.0.14

func WithMicroKeys() options.Option

func WithOutput

func WithOutput(out io.Writer) options.Option

WithOutput set default output writer for the logger

func WithSlogKeys added in v4.0.14

func WithSlogKeys() options.Option

func WithZapKeys added in v4.0.14

func WithZapKeys() options.Option

func WithZerologKeys added in v4.0.14

func WithZerologKeys() options.Option

Types

type ContextAttrFunc added in v4.0.13

type ContextAttrFunc func(ctx context.Context) []interface{}

type Level

type Level int8

Level means logger level

const (
	// TraceLevel level usually used to find bugs, very verbose
	TraceLevel Level = iota - 2
	// DebugLevel level used only when enabled debugging
	DebugLevel
	// InfoLevel level used for general info about what's going on inside the application
	InfoLevel
	// WarnLevel level used for non-critical entries
	WarnLevel
	// ErrorLevel level used for errors that should definitely be noted
	ErrorLevel
	// FatalLevel level used for critical errors and then calls `os.Exit(1)`
	FatalLevel
)

func ParseLevel

func ParseLevel(lvl string) Level

ParseLevel converts a level string into a logger Level value. returns an InfoLevel if the input string does not match known values.

func (Level) Enabled

func (l Level) Enabled(lvl Level) bool

Enabled returns true if the given level is at or above this level.

func (Level) String

func (l Level) String() string

String returns logger level string representation

type Logger

type Logger interface {
	// Init initialises options
	Init(opts ...options.Option) error
	// Clone create logger copy with new options
	Clone(opts ...options.Option) Logger
	// V compare provided verbosity level with current log level
	V(level Level) bool
	// Level sets the log level for logger
	Level(level Level)
	// The Logger options
	Options() Options
	// Attrs set attrs to always be logged with keyval pairs
	Attrs(attrs ...interface{}) Logger
	// Info level message
	Info(ctx context.Context, msg string, attrs ...interface{})
	// Tracef level message
	Trace(ctx context.Context, msg string, attrs ...interface{})
	// Debug level message
	Debug(ctx context.Context, msg string, attrs ...interface{})
	// Warn level message
	Warn(ctx context.Context, msg string, attrs ...interface{})
	// Error level message
	Error(ctx context.Context, msg string, attrs ...interface{})
	// Fatal level message
	Fatal(ctx context.Context, msg string, attrs ...interface{})
	// Log logs message with needed level
	Log(ctx context.Context, level Level, msg string, attrs ...interface{})
	// String returns the type name of logger
	String() string
	// String returns the name of logger
	Name() string
}

Logger is a generic logging interface

func Attrs added in v4.0.13

func Attrs(attrs ...interface{}) Logger

Attrs create default logger with specific attrs

func FromContext

func FromContext(ctx context.Context) (Logger, bool)

FromContext returns logger from passed context

func NewLogger

func NewLogger(opts ...options.Option) Logger

type Options

type Options struct {
	// Out holds the output writer
	Out io.Writer
	// Context holds exernal options
	Context context.Context
	// TimeFunc used to obtain current time
	TimeFunc func() time.Time
	// TimeKey is the key used for the time of the log call
	TimeKey string
	// Name holds the logger name
	Name string
	// LevelKey is the key used for the level of the log call
	LevelKey string
	// MessageKey is the key used for the message of the log call
	MessageKey string
	// ErrorKey is the key used for the error info
	ErrorKey string
	// SourceKey is the key used for the source file and line of the log call
	SourceKey string
	// StacktraceKey is the key used for the stacktrace
	StacktraceKey string
	// Attrs holds additional attributes
	Attrs []interface{}
	// ContextAttrFuncs contains funcs that executed before log func on context
	ContextAttrFuncs []ContextAttrFunc
	// CallerSkipCount number of frmaes to skip
	CallerSkipCount int
	// The logging level the logger should log
	Level Level
	// AddStacktrace controls writing of stacktaces on error
	AddStacktrace bool
	// AddSource enabled writing source file and position in log
	AddSource bool
}

Options holds logger options

func NewOptions

func NewOptions(opts ...options.Option) Options

NewOptions creates new options struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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