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
- type Config
- type Dynamic
- type Filter
- type Flag
- type Logger
- func (log *Logger) Config() Config
- func (log *Logger) CopySlot(dst, src Slot)
- func (log *Logger) Debug(args ...interface{})
- func (log *Logger) Debugf(fmtstr string, args ...interface{})
- func (log *Logger) Disable(flags Flag)
- func (log *Logger) Disabled() Flag
- func (log *Logger) Enable(flags Flag)
- func (log *Logger) Error(args ...interface{})
- func (log *Logger) Errorf(fmtstr string, args ...interface{})
- func (log *Logger) ExitLevel() iface.Level
- func (log *Logger) Fatal(args ...interface{})
- func (log *Logger) Fatalf(fmtstr string, args ...interface{})
- func (log *Logger) Filter() Filter
- func (log *Logger) Info(args ...interface{})
- func (log *Logger) Infof(fmtstr string, args ...interface{})
- func (log *Logger) Level() iface.Level
- func (log *Logger) Link(slot Slot, formatter iface.Formatter, writer iface.Writer, opts ...interface{})
- func (log *Logger) Log(callDepth int, level iface.Level, args ...interface{})
- func (log *Logger) LogError(level iface.Level, text string) error
- func (log *Logger) LogErrorf(level iface.Level, fmtstr string, args ...interface{}) error
- func (log *Logger) Logf(callDepth int, level iface.Level, fmtstr string, args ...interface{})
- func (log *Logger) MoveSlot(to, from Slot)
- func (log *Logger) Panic(args ...interface{})
- func (log *Logger) PanicLevel() iface.Level
- func (log *Logger) Panicf(fmtstr string, args ...interface{})
- func (log *Logger) SetConfig(config Config)
- func (log *Logger) SetDisabled(flags Flag)
- func (log *Logger) SetExitLevel(level iface.Level)
- func (log *Logger) SetFilter(filter Filter)
- func (log *Logger) SetLevel(level iface.Level)
- func (log *Logger) SetPanicLevel(level iface.Level)
- func (log *Logger) SetSlotFilter(slot Slot, filter Filter)
- func (log *Logger) SetSlotFormatter(slot Slot, formatter iface.Formatter)
- func (log *Logger) SetSlotLevel(slot Slot, level iface.Level)
- func (log *Logger) SetSlotWriter(slot Slot, writer iface.Writer)
- func (log *Logger) SetTimingLevel(level iface.Level)
- func (log *Logger) SetTrackLevel(level iface.Level)
- func (log *Logger) SlotFilter(slot Slot) Filter
- func (log *Logger) SlotFormatter(slot Slot) iface.Formatter
- func (log *Logger) SlotLevel(slot Slot) iface.Level
- func (log *Logger) SlotWriter(slot Slot) iface.Writer
- func (log *Logger) SwapSlot(left, right Slot)
- func (log *Logger) Timing(args ...interface{}) func()
- func (log *Logger) TimingLevel() iface.Level
- func (log *Logger) Timingf(fmtstr string, args ...interface{}) func()
- func (log *Logger) Trace(args ...interface{})
- func (log *Logger) Tracef(fmtstr string, args ...interface{})
- func (log *Logger) TrackLevel() iface.Level
- func (log *Logger) Unlink(slot Slot)
- func (log *Logger) UnlinkAll()
- func (log *Logger) UpdateConfig(fn func(Config) Config)
- func (log *Logger) Warn(args ...interface{})
- func (log *Logger) Warnf(fmtstr string, args ...interface{})
- func (log *Logger) WithContext(kvs ...interface{}) *Logger
- func (log *Logger) WithCountLimit(batch, limit int64) *Logger
- func (log *Logger) WithMark(ok bool) *Logger
- func (log *Logger) WithPrefix(prefix string) *Logger
- func (log *Logger) WithTimeLimit(duration time.Duration, limit int) *Logger
- type Slot
Constants ¶
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 ¶
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 ¶
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.
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 (*Logger) CopySlot ¶
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) Error ¶
func (log *Logger) Error(args ...interface{})
Error calls Log with level Error to emit a log.
func (*Logger) Fatal ¶
func (log *Logger) Fatal(args ...interface{})
Fatal calls Log with level Fatal to emit a log.
func (*Logger) Info ¶
func (log *Logger) Info(args ...interface{})
Info calls Log with level Info to emit a log.
func (*Logger) Link ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
PanicLevel returns the panic level of the Logger.
func (*Logger) Panicf ¶
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) SetDisabled ¶
SetDisabled sets the disabled flags of the Logger.
func (*Logger) SetExitLevel ¶
SetExitLevel sets the exit level of the Logger.
func (*Logger) SetPanicLevel ¶
SetPanicLevel sets the panic level of the Logger.
func (*Logger) SetSlotFilter ¶
SetSlotFilter sets the Filter of the slot.
func (*Logger) SetSlotFormatter ¶
SetSlotFormatter sets the Formatter of the slot. The formatter must NOT be nil.
func (*Logger) SetSlotLevel ¶
SetSlotLevel sets the Level of the slot.
func (*Logger) SetSlotWriter ¶
SetSlotWriter sets the Writer of the slot. The writer must NOT be nil.
func (*Logger) SetTimingLevel ¶ added in v0.7.0
SetTimingLevel sets the timing level of the Logger.
func (*Logger) SetTrackLevel ¶
SetTrackLevel sets the track level of the Logger.
func (*Logger) SlotFilter ¶
SlotFilter returns the Filter of the slot.
func (*Logger) SlotFormatter ¶
SlotFormatter returns the Formatter of the slot.
func (*Logger) SlotWriter ¶
SlotWriter returns the Writer of the slot.
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
TimingLevel returns the timing level of the Logger.
func (*Logger) Timingf ¶ added in v0.7.0
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) TrackLevel ¶
TrackLevel returns the track level of the Logger.
func (*Logger) Unlink ¶
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 ¶
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) WithContext ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.