Documentation
¶
Overview ¶
Package logger provides a log interface
Index ¶
- Variables
- func NewContext(ctx context.Context, l Logger) context.Context
- func NewStdLogger(l Logger, level Level) *log.Logger
- func RedirectStdLogger(l Logger, level Level) func()
- type ContextAttrFunc
- type Field
- type Level
- type Logger
- type Option
- func SetOption(k, v interface{}) Option
- func WithAddCallerSkipCount(n int) Option
- func WithAddFields(fields ...interface{}) Option
- func WithAddSource(v bool) Option
- func WithAddStacktrace(v bool) Option
- func WithContext(ctx context.Context) Option
- func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option
- func WithDedupKeys(b bool) Option
- func WithFields(fields ...interface{}) Option
- func WithLevel(level Level) Option
- func WithMeter(m meter.Meter) Option
- func WithMicroKeys() Option
- func WithName(n string) Option
- func WithOutput(out io.Writer) Option
- func WithSlogKeys() Option
- func WithTimeFunc(fn func() time.Time) Option
- func WithZapKeys() Option
- func WithZerologKeys() Option
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultLogger variable DefaultLogger = NewLogger() // DefaultLevel used by logger DefaultLevel = InfoLevel )
var DefaultContextAttrFuncs []ContextAttrFunc
Functions ¶
func NewContext ¶
NewContext stores logger into passed context
func NewStdLogger ¶
NewStdLogger returns new *log.Logger baked by logger.Logger implementation
func RedirectStdLogger ¶
RedirectStdLogger replace *log.Logger with logger.Logger implementation
Types ¶
type ContextAttrFunc ¶ added in v3.10.43
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 ¶
ParseLevel converts a level string into a logger Level value. returns an InfoLevel if the input string does not match known values.
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, msg string, args ...interface{})
// Trace level message
Trace(ctx context.Context, msg string, args ...interface{})
// Debug level message
Debug(ctx context.Context, msg string, args ...interface{})
// Warn level message
Warn(ctx context.Context, msg string, args ...interface{})
// Error level message
Error(ctx context.Context, msg string, args ...interface{})
// Fatal level message
Fatal(ctx context.Context, msg string, args ...interface{})
// Log logs message with needed level
Log(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 FromContext ¶
FromContext returns logger from passed context
func MustContext ¶ added in v3.10.77
MustContext returns logger from passed context or DefaultLogger if empty
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 WithAddCallerSkipCount ¶ added in v3.10.63
WithAddCallerSkipCount add skip count for copy logger
func WithAddFields ¶ added in v3.10.107
func WithAddFields(fields ...interface{}) Option
WithAddFields add fields for the logger
func WithAddSource ¶ added in v3.10.43
WithAddSource controls writing source file and pos in log
func WithAddStacktrace ¶ added in v3.10.43
WithAddStacktrace controls writing stacktrace on error
func WithContextAttrFuncs ¶ added in v3.10.43
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option
WithContextAttrFuncs appends default funcs for the context attrs filler
func WithDedupKeys ¶ added in v3.11.15
WithDedupKeys dont log duplicate keys
func WithFields ¶
func WithFields(fields ...interface{}) Option
WithFields set default fields for the logger
func WithMicroKeys ¶ added in v3.10.43
func WithMicroKeys() Option
func WithOutput ¶
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
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 {
// 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
// Name holds the logger name
Name string
// Out holds the output writer
Out io.Writer
// Context holds exernal options
Context context.Context
// Meter used to count logs for specific level
Meter meter.Meter
// TimeFunc used to obtain current time
TimeFunc func() time.Time
// Fields holds additional metadata
Fields []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
// AddSource enabled writing source file and position in log
AddSource bool
// AddStacktrace controls writing of stacktaces on error
AddStacktrace bool
// DedupKeys deduplicate keys in log output
DedupKeys bool
}
Options holds logger options