logger

package
v3.7.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package logger provides a log interface

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogger variable
	DefaultLogger Logger = NewLogger(WithLevel(ParseLevel(os.Getenv("MICRO_LOG_LEVEL"))))
	// DefaultLevel used by logger
	DefaultLevel Level = InfoLevel
	// DefaultCallerSkipCount used by logger
	DefaultCallerSkipCount = 2
)

Functions

func Debug

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

Debug writes msg to default logger on debug level

func Debugf

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

Debugf writes formatted msg to default logger on debug level

func Error

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

Error writes msg to default logger on error level

func Errorf

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

Errorf writes formatted msg to default logger on error level

func Fatal

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

Fatal writes msg to default logger on fatal level

func Fatalf

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

Fatalf writes formatted msg to default logger on fatal level

func Info

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

Info writes msg to default logger on info level

func Infof

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

Infof writes formatted msg to default logger on info level

func Init

func Init(opts ...Option) error

Init initialize logger

func NewContext

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

NewContext stores logger into passed context

func NewStdLogger added in v3.6.1

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

func RedirectStdLogger added in v3.6.1

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

func Trace

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

Trace writes msg to default logger on trace level

func Tracef

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

Tracef 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, args ...interface{})

Warn writes msg to default logger on warn level

func Warnf

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

Warnf writes formatted msg to default logger on warn level

Types

type Field added in v3.6.1

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 added in v3.7.0

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 LogFunc added in v3.4.6

type LogFunc func(ctx context.Context, level Level, args ...interface{})

LogFunc function used for Log method

type LogfFunc added in v3.4.6

type LogfFunc func(ctx context.Context, level Level, msg string, args ...interface{})

LogfFunc function used for Logf method

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{})
	// String returns the name of logger
	String() string
}

Logger is a generic logging interface

func Fields

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

Fields create logger with specific fields

func FromContext

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

FromContext returns logger from passed context

func NewLogger

func NewLogger(opts ...Option) Logger

NewLogger builds a new logger based on options

func NewOmitLogger added in v3.4.7

func NewOmitLogger(l Logger) Logger

type OmitLogger added in v3.4.7

type OmitLogger struct {
	// contains filtered or unexported fields
}

func (*OmitLogger) Clone added in v3.7.0

func (w *OmitLogger) Clone(opts ...Option) Logger

func (*OmitLogger) Debug added in v3.4.7

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

func (*OmitLogger) Debugf added in v3.4.7

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

func (*OmitLogger) Error added in v3.4.7

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

func (*OmitLogger) Errorf added in v3.4.7

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

func (*OmitLogger) Fatal added in v3.4.7

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

func (*OmitLogger) Fatalf added in v3.4.7

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

func (*OmitLogger) Fields added in v3.4.7

func (w *OmitLogger) Fields(fields ...interface{}) Logger

func (*OmitLogger) Info added in v3.4.7

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

func (*OmitLogger) Infof added in v3.4.7

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

func (*OmitLogger) Init added in v3.4.7

func (w *OmitLogger) Init(opts ...Option) error

func (*OmitLogger) Level added in v3.7.0

func (w *OmitLogger) Level(level Level)

func (*OmitLogger) Log added in v3.4.7

func (w *OmitLogger) Log(ctx context.Context, level Level, args ...interface{})

func (*OmitLogger) Logf added in v3.4.7

func (w *OmitLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{})

func (*OmitLogger) Options added in v3.4.7

func (w *OmitLogger) Options() Options

func (*OmitLogger) String added in v3.4.7

func (w *OmitLogger) String() string

func (*OmitLogger) Trace added in v3.4.7

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

func (*OmitLogger) Tracef added in v3.4.7

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

func (*OmitLogger) V added in v3.4.7

func (w *OmitLogger) V(level Level) bool

func (*OmitLogger) Warn added in v3.4.7

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

func (*OmitLogger) Warnf added in v3.4.7

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

type OmitWrapper added in v3.4.6

type OmitWrapper struct{}

func (*OmitWrapper) Log added in v3.4.6

func (w *OmitWrapper) Log(fn LogFunc) LogFunc

func (*OmitWrapper) Logf added in v3.4.6

func (w *OmitWrapper) Logf(fn LogfFunc) LogfFunc

type Option

type Option func(*Options)

Option func

func SetOption

func SetOption(k, v interface{}) Option

SetOption returns a function to setup a context with given value

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 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 WithName added in v3.3.17

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 WrapLogger added in v3.4.6

func WrapLogger(w Wrapper) Option

WrapLogger adds a logger Wrapper to a list of options passed into the logger

type Options

type Options struct {
	// Out holds the output writer
	Out io.Writer
	// Context holds exernal options
	Context context.Context
	// Fields holds additional metadata
	Fields []interface{}
	// Name holds the logger name
	Name string
	// CallerSkipCount number of frmaes to skip
	CallerSkipCount int
	// The logging level the logger should log
	Level Level
	// Wrappers logger wrapper that called before actual Log/Logf function
	Wrappers []Wrapper
}

Options holds logger options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions creates new options struct

type Wrapper added in v3.4.6

type Wrapper interface {
	// Log logs message with needed level
	Log(LogFunc) LogFunc
	// Logf logs message with needed level
	Logf(LogfFunc) LogfFunc
}

func NewOmitWrapper added in v3.4.6

func NewOmitWrapper() Wrapper

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