logger

package
v1.0.100 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package logger implements a concise, functional, flexible and extensible logger for Go. It provides many advanced features and has good customizability and extensibility. It also aims at easy-to-use.

Index

Constants

View Source
const MaxSlot = 8

MaxSlot is the total count of slots.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Level is the level of Logger, logs with a lower level will be omitted.
	// If it is not specified, Trace is used. Otherwise, its value MUST be
	// between Trace and Off inclusive.
	Level iface.Level
	// TrackLevel is the auto backtracking level of Logger.
	// If the level of a emitted log is NOT lower than the TrackLevel, the stack
	// of the current goroutine will be output with the log.
	// If it is not specified, Fatal is used. Otherwise, its value MUST be
	// between Trace and Off inclusive.
	TrackLevel iface.Level
	// ExitLevel is the auto exiting level of Logger.
	// If the level of a emitted log is NOT lower than the ExitLevel, the Logger
	// will call os.Exit after outputting the log.
	// If it is not specified, Off is used. Otherwise, its value MUST be
	// between Trace and Off inclusive.
	ExitLevel iface.Level
	// TimingLevel is the level of a emitted log when Logger.Timing or
	// Logger.Timingf is called.
	// If it is not specified, Trace is used. Otherwise, its value MUST be
	// between Trace and Fatal inclusive.
	TimingLevel iface.Level
	// PanicLevel is the level of a emitted log when Logger.Panic or
	// Logger.Panicf is called.
	// If it is not specified, Fatal is used. Otherwise, its value MUST be
	// between Trace and Fatal inclusive.
	PanicLevel iface.Level
	// Filter is the log filter of Logger. If it is not nil, it will be called
	// when a log emits. And if it returns false, the log will be omitted.
	Filter Filter
	// Disabled is a set of flags. If a flag is set, the corresponding feature
	// of Logger will be disabled.
	Disabled Flag
}

A Config is used to configure a Logger.

type Dynamic

type Dynamic func(key interface{}) interface{}

The Dynamic type defines a function type. A value of Dynamic will be regarded as the value getter of a dynamic context key-value pair when it is as an argument to WithContext.

Do NOT call any method of the Logger within the function, or it may deadlock.

type Filter

type Filter func(*iface.Record) bool

The Filter type defines a function type which is used to filter logs.

Do NOT call any method of the Logger within a filter, or it may deadlock.

func And

func And(filter Filter, filters ...Filter) Filter

And returns a function that is the logic AND of all the filters. It has the short circuit feature. Any filter must NOT be nil.

func Not

func Not(filter Filter) Filter

Not returns a function that is the logic NOT of the filter. The filter must NOT be nil.

func Or

func Or(filter Filter, filters ...Filter) Filter

Or returns a function that is the logic OR of all the filters. It has the short circuit feature. Any filter must NOT be nil.

type Flag

type Flag int

The Flag defines the flag type of Logger.

const (
	Prefix Flag = 0x01 << iota
	StaticContext
	DynamicContext
	Mark
	LimitByCount
	LimitByTime
	Runtime
)

All available flags here.

type Logger

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

A Logger is a logging framework that contains EIGHT slots. Each Slot contains a Formatter and a Writer. A Logger has its own level and filter while each Slot has its independent level and filter. Logger calls the Formatter and Writer of each Slot in the order from Slot0 to Slot7 when a log is emitted.

All methods of A Logger are concurrency safe. A Logger MUST be created with New.

func New

func New(config Config) *Logger

New creates a new Logger with the config.

func (*Logger) Config

func (log *Logger) Config() Config

Config returns the Config of the Logger.

func (*Logger) CopySlot

func (log *Logger) CopySlot(dst, src Slot)

CopySlot copies the Formatter, Writer, Level and Filter of Slot src to Slot dst.

func (*Logger) Debug

func (log *Logger) Debug(args ...interface{})

Debug calls Log with level Debug to emit a log.

func (*Logger) Debugf

func (log *Logger) Debugf(fmtstr string, args ...interface{})

Debugf calls Logf with level Debug to emit a log.

func (*Logger) Disable

func (log *Logger) Disable(flags Flag)

Disable disables the flags of the Logger.

func (*Logger) Disabled

func (log *Logger) Disabled() Flag

Disabled returns the disabled flags of the Logger.

func (*Logger) Enable

func (log *Logger) Enable(flags Flag)

Enable enables the flags of the Logger.

func (*Logger) Error

func (log *Logger) Error(args ...interface{})

Error calls Log with level Error to emit a log.

func (*Logger) Errorf

func (log *Logger) Errorf(fmtstr string, args ...interface{})

Errorf calls Logf with level Error to emit a log.

func (*Logger) ExitLevel

func (log *Logger) ExitLevel() iface.Level

ExitLevel returns the exit level of the Logger.

func (*Logger) Fatal

func (log *Logger) Fatal(args ...interface{})

Fatal calls Log with level Fatal to emit a log.

func (*Logger) Fatalf

func (log *Logger) Fatalf(fmtstr string, args ...interface{})

Fatalf calls Logf with level Fatal to emit a log.

func (*Logger) Filter

func (log *Logger) Filter() Filter

Filter returns the filter of the Logger.

func (*Logger) Info

func (log *Logger) Info(args ...interface{})

Info calls Log with level Info to emit a log.

func (*Logger) Infof

func (log *Logger) Infof(fmtstr string, args ...interface{})

Infof calls Logf with level Info to emit a log.

func (*Logger) Level

func (log *Logger) Level() iface.Level

Level returns the level of the Logger.

func (log *Logger) Link(slot Slot, formatter iface.Formatter,
	writer iface.Writer, opts ...interface{})

Link sets the formatter and writer to the slot. The opts is used to specify the slot Level and/or the slot Filter. An opt MUST be a value of type Level, Filter or func(*Record)bool (the underlying type of Filter). The formatter and the writer must NOT be nil. If the Level of the slot is not specified, Trace is used.

func (*Logger) Log

func (log *Logger) Log(callDepth int, level iface.Level, args ...interface{})

Log calls the Formatter and Writer in each Slot to format and write a log.

The level MUST be between Trace and Fatal inclusive. If the level is lower than the level of Logger, the log will NOT be emitted. If the level is lower than the level of a Slot, the Formatter and Writer of the Slot will NOT be called. If the level is NOT lower than the track level of Logger, the stack of the current goroutine will be output. If the level is NOT lower than the exit level of Logger, the Logger will call os.Exit at last.

The callDepth is used to set the offset of stack. It makes sense when you are customizing your own log wrapper function. Otherwise, 0 is just ok.

The args are handled in the manner of fmt.Sprint.

ATTENTION: the log may NOT be output when a Writer is in asynchronous mode and os.Exit has been called.

func (*Logger) LogError

func (log *Logger) LogError(level iface.Level, text string) error

LogError calls Log to emit a log and calls errors.New to return an error. The level MUST be between Trace and Fatal inclusive.

func (*Logger) LogErrorf

func (log *Logger) LogErrorf(level iface.Level, fmtstr string, args ...interface{}) error

LogErrorf calls Logf to emit a log and calls fmt.Errorf to return an error. The level MUST be between Trace and Fatal inclusive.

func (*Logger) Logf

func (log *Logger) Logf(callDepth int, level iface.Level, fmtstr string, args ...interface{})

Logf does the same with Log except that it calls fmt.Sprintf to format a log.

ATTENTION: the log may NOT be output when a Writer is in asynchronous mode and os.Exit has been called.

func (*Logger) MoveSlot

func (log *Logger) MoveSlot(to, from Slot)

MoveSlot copies the Formatter, Writer, Level and Filter of Slot from to Slot to, and then unlinks Slot from.

func (*Logger) Panic

func (log *Logger) Panic(args ...interface{})

Panic calls the Formatters and Writers in each Slot to format and write a log and panics at last.

The level of the emitted log is the panic level of Logger. If the level is lower than the level of Logger, the log will NOT be output. If the level is lower than the level of a Slot, the Formatter and Writer of the Slot will NOT be called.

The args are handled in the manner of fmt.Sprint.

ATTENTION: the log may NOT be output when a Writer is in asynchronous mode and there is NO recovery after panicking. Panic never outputs the stack with the log or calls os.Exit.

func (*Logger) PanicLevel

func (log *Logger) PanicLevel() iface.Level

PanicLevel returns the panic level of the Logger.

func (*Logger) Panicf

func (log *Logger) Panicf(fmtstr string, args ...interface{})

Panicf does the same with Panic except it calls fmt.Sprintf to format a log.

ATTENTION: the log may NOT be output when a Writer is in asynchronous mode and there is NO recovery after panicking. Panicf never outputs the stack with the log or calls os.Exit.

func (*Logger) SetConfig

func (log *Logger) SetConfig(config Config)

SetConfig sets the config to the Logger.

func (*Logger) SetDisabled

func (log *Logger) SetDisabled(flags Flag)

SetDisabled sets the disabled flags of the Logger.

func (*Logger) SetExitLevel

func (log *Logger) SetExitLevel(level iface.Level)

SetExitLevel sets the exit level of the Logger.

func (*Logger) SetFilter

func (log *Logger) SetFilter(filter Filter)

SetFilter sets the filter of the Logger.

func (*Logger) SetLevel

func (log *Logger) SetLevel(level iface.Level)

SetLevel sets the level of the Logger.

func (*Logger) SetPanicLevel

func (log *Logger) SetPanicLevel(level iface.Level)

SetPanicLevel sets the panic level of the Logger.

func (*Logger) SetSlotFilter

func (log *Logger) SetSlotFilter(slot Slot, filter Filter)

SetSlotFilter sets the Filter of the slot.

func (*Logger) SetSlotFormatter

func (log *Logger) SetSlotFormatter(slot Slot, formatter iface.Formatter)

SetSlotFormatter sets the Formatter of the slot. The formatter must NOT be nil.

func (*Logger) SetSlotLevel

func (log *Logger) SetSlotLevel(slot Slot, level iface.Level)

SetSlotLevel sets the Level of the slot.

func (*Logger) SetSlotWriter

func (log *Logger) SetSlotWriter(slot Slot, writer iface.Writer)

SetSlotWriter sets the Writer of the slot. The writer must NOT be nil.

func (*Logger) SetTimingLevel added in v0.7.0

func (log *Logger) SetTimingLevel(level iface.Level)

SetTimingLevel sets the timing level of the Logger.

func (*Logger) SetTrackLevel

func (log *Logger) SetTrackLevel(level iface.Level)

SetTrackLevel sets the track level of the Logger.

func (*Logger) SlotFilter

func (log *Logger) SlotFilter(slot Slot) Filter

SlotFilter returns the Filter of the slot.

func (*Logger) SlotFormatter

func (log *Logger) SlotFormatter(slot Slot) iface.Formatter

SlotFormatter returns the Formatter of the slot.

func (*Logger) SlotLevel

func (log *Logger) SlotLevel(slot Slot) iface.Level

SlotLevel returns the Level of the slot.

func (*Logger) SlotWriter

func (log *Logger) SlotWriter(slot Slot) iface.Writer

SlotWriter returns the Writer of the slot.

func (*Logger) SwapSlot

func (log *Logger) SwapSlot(left, right Slot)

SwapSlot swaps the Formatter, Writer, Level and Filter of the slots.

func (*Logger) Timing added in v0.7.0

func (log *Logger) Timing(args ...interface{}) func()

Timing returns a function. When the function is called, it outputs the log with the time elapsed since the call of Timing.

The level of the emitted log is the timing level of Logger. If the level is lower than the level of Logger, the log will NOT be output. If the level is lower than the level of a Slot, the Formatter and Writer of the Slot will NOT be called.

The args are handled in the manner of fmt.Sprint.

It works well with `defer' and do NOT forget the last empty pair of parentheses.

func (*Logger) TimingLevel added in v0.7.0

func (log *Logger) TimingLevel() iface.Level

TimingLevel returns the timing level of the Logger.

func (*Logger) Timingf added in v0.7.0

func (log *Logger) Timingf(fmtstr string, args ...interface{}) func()

Timingf does the same with Timing except it calls fmt.Sprintf to format a log.

It works well with `defer' and do NOT forget the last empty pair of parentheses.

func (*Logger) Trace

func (log *Logger) Trace(args ...interface{})

Trace calls Log with level Trace to emit a log.

func (*Logger) Tracef

func (log *Logger) Tracef(fmtstr string, args ...interface{})

Tracef calls Logf with level Trace to emit a log.

func (*Logger) TrackLevel

func (log *Logger) TrackLevel() iface.Level

TrackLevel returns the track level of the Logger.

func (log *Logger) Unlink(slot Slot)

Unlink sets the Formatter, Writer and Filter of the slot to nil and the Level of the slot to Off.

func (*Logger) UnlinkAll

func (log *Logger) UnlinkAll()

UnlinkAll sets the Formatter, Writer and Filter of all slots to nil and the Level of all slots to Off.

func (*Logger) UpdateConfig

func (log *Logger) UpdateConfig(fn func(Config) Config)

UpdateConfig calls the fn with the Config of the Logger, and then sets the returned Config to the Logger. The fn must NOT be nil.

Do NOT call any method of the Logger within the fn, or it may deadlock.

func (*Logger) Warn

func (log *Logger) Warn(args ...interface{})

Warn calls Log with level Warn to emit a log.

func (*Logger) Warnf

func (log *Logger) Warnf(fmtstr string, args ...interface{})

Warnf calls Logf with level Warn to emit a log.

func (*Logger) WithContext

func (log *Logger) WithContext(kvs ...interface{}) *Logger

WithContext returns a new Logger that is a shallow copy of the Logger. With the new Logger, all the logs it outputs will have all contexts attached as long as the StaticContext or DynamicContext flag is NOT disabled.

The kvs is regarded as an interleaved key-value sequence, e.g. key1, value1, key2, value2 ... If the count of the arguments is odd, the last argument will be ignored.

WithContext also supports dynamic contexts. If a value of type Dynamic is as the value of a key-value pair passed to WithContext, it will be regarded as the value getter of a dynamic context key-value pair. The value getter will be called whenever a log is emitted. All the key-value pairs of dynamic contexts will be concatenated to the end of static contexts.

ATTENTION: you SHOULD be very careful to concurrency safety or deadlocks with dynamic contexts.

func (*Logger) WithCountLimit

func (log *Logger) WithCountLimit(batch, limit int64) *Logger

WithCountLimit returns a new Logger that is a shallow copy of the Logger. With the new Logger, the count of logs it outputs will be limited as long as the LimitByCount flag is NOT disabled.

The batch MUST be positive and the limit must NOT be negative. As a result, limit logs will be output every batch logs.

THINK TWICE before you decide to limit the output of logs, you may miss logs which you need.

func (*Logger) WithMark

func (log *Logger) WithMark(ok bool) *Logger

WithMark returns a new Logger that is a shallow copy of the Logger. With the new Logger, all the logs it outputs will be marked as long as the Mark flag is NOT disabled.

func (*Logger) WithPrefix

func (log *Logger) WithPrefix(prefix string) *Logger

WithPrefix returns a new Logger that is a shallow copy of the Logger. With the new Logger, all the logs it outputs will have the prefix attached as long as the Prefix flag is NOT disabled.

func (*Logger) WithTimeLimit

func (log *Logger) WithTimeLimit(duration time.Duration, limit int) *Logger

WithTimeLimit returns a new Logger that is a shallow copy of the Logger. With the new Logger, the count of logs it outputs will be limited as long as the LimitByTime flag is NOT disabled.

The duration MUST be positive and the limit must NOT be negative. As a result, at most limit logs will be output during any interval of duration.

THINK TWICE before you decide to limit the output of logs, you may miss logs which you need.

NOTICE: The space complexity of WithTimeLimit is O(limit). Try to specify reasonable duration and limit.

type Slot

type Slot int

The Slot defines the slot type of Logger.

const (
	Slot0 Slot = iota
	Slot1
	Slot2
	Slot3
	Slot4
	Slot5
	Slot6
	Slot7
)

All available slots here.

Jump to

Keyboard shortcuts

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