logger

package
v3.10.47 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 7 Imported by: 35

Documentation

Overview

Package logger provides a log interface

Index

Constants

This section is empty.

Variables

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

Functions

func Debug deprecated

func Debug(ctx context.Context, args ...interface{})

Debug writes msg to default logger on debug level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Debugf deprecated

func Debugf(ctx context.Context, msg string, args ...interface{})

Debugf writes formatted msg to default logger on debug level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Error deprecated

func Error(ctx context.Context, args ...interface{})

Error writes msg to default logger on error level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Errorf deprecated

func Errorf(ctx context.Context, msg string, args ...interface{})

Errorf writes formatted msg to default logger on error level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Fatal deprecated

func Fatal(ctx context.Context, args ...interface{})

Fatal writes msg to default logger on fatal level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Fatalf deprecated

func Fatalf(ctx context.Context, msg string, args ...interface{})

Fatalf writes formatted msg to default logger on fatal level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Info deprecated

func Info(ctx context.Context, args ...interface{})

Info writes msg to default logger on info level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Infof deprecated

func Infof(ctx context.Context, msg string, args ...interface{})

Infof writes formatted msg to default logger on info level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Init deprecated

func Init(opts ...Option) error

Init initialize logger

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

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 deprecated

func Trace(ctx context.Context, args ...interface{})

Trace writes msg to default logger on trace level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Tracef deprecated

func Tracef(ctx context.Context, msg string, args ...interface{})

Tracef writes formatted msg to default logger on trace level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func V deprecated

func V(level Level) bool

V returns true if passed level enabled in default logger

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Warn deprecated

func Warn(ctx context.Context, args ...interface{})

Warn writes msg to default logger on warn level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func Warnf deprecated

func Warnf(ctx context.Context, msg string, args ...interface{})

Warnf writes formatted msg to default logger on warn level

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

Types

type ContextAttrFunc added in v3.10.43

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

type Field

type Field interface{}

Field contains keyval pair

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 ...Option) error
	// Clone create logger copy with new options
	Clone(opts ...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
	// Fields set fields to always be logged with keyval pairs
	Fields(fields ...interface{}) Logger
	// Info level message
	Info(ctx context.Context, args ...interface{})
	// Trace level message
	Trace(ctx context.Context, args ...interface{})
	// Debug level message
	Debug(ctx context.Context, args ...interface{})
	// Warn level message
	Warn(ctx context.Context, args ...interface{})
	// Error level message
	Error(ctx context.Context, args ...interface{})
	// Fatal level message
	Fatal(ctx context.Context, args ...interface{})
	// Infof level message
	Infof(ctx context.Context, msg string, args ...interface{})
	// Tracef level message
	Tracef(ctx context.Context, msg string, args ...interface{})
	// Debug level message
	Debugf(ctx context.Context, msg string, args ...interface{})
	// Warn level message
	Warnf(ctx context.Context, msg string, args ...interface{})
	// Error level message
	Errorf(ctx context.Context, msg string, args ...interface{})
	// Fatal level message
	Fatalf(ctx context.Context, msg string, args ...interface{})
	// Log logs message with needed level
	Log(ctx context.Context, level Level, args ...interface{})
	// Logf logs message with needed level
	Logf(ctx context.Context, level Level, msg string, args ...interface{})
	// Name returns broker instance name
	Name() string
	// String returns the type of logger
	String() string
}

Logger is a generic logging interface

func Fields deprecated

func Fields(fields ...interface{}) Logger

Fields create logger with specific fields

Deprecated: Dont use logger methods directly, use instance of logger to avoid additional allocations

func FromContext

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

FromContext returns logger from passed context

func NewLogger

func NewLogger(opts ...Option) Logger

type Option

type Option func(*Options)

Option func signature

func SetOption

func SetOption(k, v interface{}) Option

SetOption returns a function to setup a context with given value

func WithAddSource added in v3.10.43

func WithAddSource(v bool) Option

WitAddSource controls writing source file and pos in log

func WithAddStacktrace added in v3.10.43

func WithAddStacktrace(v bool) Option

WitAddStacktrace controls writing stacktrace on error

func WithCallerSkipCount

func WithCallerSkipCount(c int) Option

WithCallerSkipCount set frame count to skip

func WithContext

func WithContext(ctx context.Context) Option

WithContext set context

func WithContextAttrFuncs added in v3.10.43

func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option

WithContextAttrFuncs appends default funcs for the context arrts filler

func WithFields

func WithFields(fields ...interface{}) Option

WithFields set default fields for the logger

func WithLevel

func WithLevel(level Level) Option

WithLevel set default level for the logger

func WithMicroKeys added in v3.10.43

func WithMicroKeys() Option

func WithName

func WithName(n string) Option

WithName sets the name

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput set default output writer for the logger

func WithSlogKeys added in v3.10.43

func WithSlogKeys() Option

func WithTimeFunc added in v3.10.46

func WithTimeFunc(fn func() time.Time) Option

WithTimeFunc sets the func to obtain current time

func WithZapKeys added in v3.10.43

func WithZapKeys() Option

func WithZerologKeys added in v3.10.43

func WithZerologKeys() Option

type Options

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

Options holds logger options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions creates new options struct

Directories

Path Synopsis
Package wrapper provides wrapper for Logger
Package wrapper provides wrapper for Logger

Jump to

Keyboard shortcuts

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