Documentation ¶
Overview ¶
Package recording provides the possibility to record instances of events.
This can either be done by using a direct instance of a log.Logger using NewLogger() to create it.
... or hook it fully into the full application by calling NewProvider() and using then afterwards Provider.HookGlobally() to make it available for every piece of code that tries to log something. See the example for more details.
Example ¶
package main import ( "fmt" "github.com/echocat/slf4g/testing/recording" log "github.com/echocat/slf4g" ) func main() { // 1. Create at first a new provider provider := recording.NewProvider() // 2. Hook it into the global registry AND ensure it will be always executed // at the end of this method defer provider.HookGlobally()() // 3. Log something with the global logging methods log.Info("foo") log.Warn("bar") // 4. Print every recorded event to stdout fmt.Println(provider.GetAll()) // At the end: Everything will be reset. }
Output:
Index ¶
- Constants
- func BeFalse() *bool
- func BeTrue() *bool
- type CoreLogger
- func (instance *CoreLogger) Accepts(e log.Event) bool
- func (instance *CoreLogger) Contains(expected log.Event) (bool, error)
- func (instance *CoreLogger) ContainsCustom(eef log.EventEquality, expected log.Event) (bool, error)
- func (instance *CoreLogger) Get(index uint) log.Event
- func (instance *CoreLogger) GetAll() []log.Event
- func (instance *CoreLogger) GetLevel() level.Level
- func (instance *CoreLogger) GetName() string
- func (instance *CoreLogger) GetProvider() log.Provider
- func (instance *CoreLogger) IsLevelEnabled(v level.Level) bool
- func (instance *CoreLogger) Len() int
- func (instance *CoreLogger) Log(event log.Event, _ uint16)
- func (instance *CoreLogger) MustContains(expected log.Event) bool
- func (instance *CoreLogger) MustContainsCustom(eef log.EventEquality, expected log.Event) bool
- func (instance *CoreLogger) NewEvent(l level.Level, values map[string]interface{}) log.Event
- func (instance *CoreLogger) NewEventWithFields(l level.Level, f fields.ForEachEnabled) log.Event
- func (instance *CoreLogger) Reset()
- func (instance *CoreLogger) SetLevel(v level.Level)
- type Logger
- func (instance *Logger) Accepts(event log.Event) bool
- func (instance *Logger) GetName() string
- func (instance *Logger) GetProvider() log.Provider
- func (instance *Logger) IsLevelEnabled(v level.Level) bool
- func (instance *Logger) Log(event log.Event, skipFrames uint16)
- func (instance *Logger) NewEvent(l level.Level, values map[string]interface{}) log.Event
- func (instance *Logger) NewEventWithFields(l level.Level, f fields.ForEachEnabled) log.Event
- type Provider
- func (instance *Provider) Contains(expected log.Event) (bool, error)
- func (instance *Provider) ContainsCustom(eef log.EventEquality, expected log.Event) (bool, error)
- func (instance *Provider) GetAll() []log.Event
- func (instance *Provider) GetAllLevels() level.Levels
- func (instance *Provider) GetAllOf(name string) []log.Event
- func (instance *Provider) GetAllRoot() []log.Event
- func (instance *Provider) GetFieldKeysSpec() fields.KeysSpec
- func (instance *Provider) GetLevel() level.Level
- func (instance *Provider) GetLogger(name string) log.Logger
- func (instance *Provider) GetName() string
- func (instance *Provider) GetRootLogger() log.Logger
- func (instance *Provider) HookGlobally() func()
- func (instance *Provider) Len() int
- func (instance *Provider) MustContains(expected log.Event) bool
- func (instance *Provider) MustContainsCustom(eef log.EventEquality, expected log.Event) bool
- func (instance *Provider) Reset(name string)
- func (instance *Provider) ResetAll()
- func (instance *Provider) ResetRoot()
- func (instance *Provider) SetLevel(v level.Level)
Examples ¶
Constants ¶
const DefaultLevel = level.Info
DefaultLevel specifies the default level.Level of an instance of Provider which be used if no other level was defined.
const DefaultProviderName = "recording"
DefaultProviderName specifies the default name an instance of Provider which will be used if no other name was defined.
const RootLoggerName = "ROOT"
RootLoggerName specifies the name of the root version of CoreLogger instances which are managed by Provider.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CoreLogger ¶
type CoreLogger struct { // Provider which are managing this instance. If this value is nil // log.GetProvider() will be used instead. This default behaviour could lead // in some cases to unintended consequences. Provider log.Provider // Name represents the name of this instance of CoreLogger. If this value is // empty RootLoggerName will be used instead. Name string // Level represents the level of this instance of CoreLogger. If this value // is empty the level of its Provider will be used. If it is not possible // to receive the level of its Provider it will use DefaultLevel instead. Level level.Level // contains filtered or unexported fields }
CoreLogger implements log.CoreLogger and records simply every logged event. Each of these events can be received using GetAll() or Get(index).
Mutation ¶
This log.CoreLogger will mutate the recorded instances of log.Event if one of the following fields is absent: logger, timestamp. This behavior can be disabled using SetIfAbsent field.
Mutation will also happen if you use this logger as a global registered one by calling methods like log.Info(..), log.Warn(..), ... because they are adding in any way their context information.
func NewCoreLogger ¶
func NewCoreLogger() *CoreLogger
NewCoreLogger creates a new instance of CoreLogger which is ready to use.
func (*CoreLogger) Contains ¶
func (instance *CoreLogger) Contains(expected log.Event) (bool, error)
Contains checks if the given log.Event was recorded by this CoreLogger. It will use the fields.DefaultEntryEqualityFunction to checks the equality but will ignore the timestamp, log.Event#GetCallDepth() and log.Event#GetContext().
func (*CoreLogger) ContainsCustom ¶
func (instance *CoreLogger) ContainsCustom(eef log.EventEquality, expected log.Event) (bool, error)
ContainsCustom checks if the given log.Event was recorded by this CoreLogger. It will use the given log.EventEquality to checks the equality.
func (*CoreLogger) Get ¶
func (instance *CoreLogger) Get(index uint) log.Event
Get return an event at the given index. If this index does not exists this method will panic.
func (*CoreLogger) GetAll ¶
func (instance *CoreLogger) GetAll() []log.Event
GetAll returns all recorded events.
func (*CoreLogger) GetLevel ¶
func (instance *CoreLogger) GetLevel() level.Level
GetLevel returns the current level.Level where this log.CoreLogger is set to.
func (*CoreLogger) GetName ¶
func (instance *CoreLogger) GetName() string
GetName implements log.CoreLogger#GetName()
func (*CoreLogger) GetProvider ¶
func (instance *CoreLogger) GetProvider() log.Provider
GetProvider implements log.CoreLogger#GetProvider()
func (*CoreLogger) IsLevelEnabled ¶
func (instance *CoreLogger) IsLevelEnabled(v level.Level) bool
IsLevelEnabled implements log.CoreLogger#IsLevelEnabled()
func (*CoreLogger) Len ¶
func (instance *CoreLogger) Len() int
Len returns the length of all recorded events.
func (*CoreLogger) Log ¶
func (instance *CoreLogger) Log(event log.Event, _ uint16)
Log implements log.CoreLogger#Log(event).
func (*CoreLogger) MustContains ¶
func (instance *CoreLogger) MustContains(expected log.Event) bool
MustContains is like Contains but will panic on errors instead returning them.
func (*CoreLogger) MustContainsCustom ¶
func (instance *CoreLogger) MustContainsCustom(eef log.EventEquality, expected log.Event) bool
MustContainsCustom is like ContainsCustom but will panic on errors instead returning them.
func (*CoreLogger) NewEventWithFields ¶ added in v0.10.0
func (instance *CoreLogger) NewEventWithFields(l level.Level, f fields.ForEachEnabled) log.Event
func (*CoreLogger) Reset ¶
func (instance *CoreLogger) Reset()
Reset will remove all recorded events of this CoreLogger.
func (*CoreLogger) SetLevel ¶
func (instance *CoreLogger) SetLevel(v level.Level)
SetLevel changes the current level.Level of this log.CoreLogger. If set to 0 it will force this CoreLogger to use DefaultLevel.
type Logger ¶
type Logger struct { log.Logger *CoreLogger }
Logger is a fully implemented version of CoreLogger and log.Logger.
func NewLogger ¶
func NewLogger() *Logger
NewLogger creates a new instance of Logger which is ready to use.
func (*Logger) GetProvider ¶
GetProvider implements log.Logger#GetProvider()
func (*Logger) IsLevelEnabled ¶
IsLevelEnabled implements log.Logger#IsLevelEnabled()
func (*Logger) NewEventWithFields ¶ added in v0.10.0
NewEvent reimplements CoreLogger#NewEventWithFields()
type Provider ¶
type Provider struct { // Name specifies the name of this Provider. If empty this Provider will // use DefaultProviderName. Name string // Level specifies the level of this Provider which will be also inherited // by all of its loggers. If 0 this Provider will use DefaultLevel. Level level.Level // AllLevels specifies the levels which are supported by this Provider and all // of its loggers. If nil this Provider will use // level.GetProvider()#GetLevels(). AllLevels level.Levels // FieldKeysSpec specifies the spec of the fields are supported by this // Provider and all of its loggers. If nil this Provider will use the // default instance of fields.KeysSpecImpl. FieldKeysSpec fields.KeysSpec // contains filtered or unexported fields }
Provider is an implementation of log.Provider which is simply recording all events which are logged with it's loggers. They can be simply received using GetAll(), GetAllRoot() or GetAllOf()...
func NewProvider ¶
func NewProvider() *Provider
NewProvider creates a new instance of Provider which is ready to use.
func (*Provider) Contains ¶
Contains checks if the given log.Event was recorded by at least of one CoreLogger of this Provider. It will use the fields.DefaultEntryEqualityFunction to checks the equality but will ignore the timestamp, log.Event#GetCallDepth() and log.Event#GetContext().
func (*Provider) ContainsCustom ¶
ContainsCustom checks if the given log.Event was recorded by at least of one CoreLogger of this Provider. It will use the given log.EventEquality to checks the equality.
func (*Provider) GetAll ¶
GetAll returns all instances of log.Event which where recorded so far by all instances of CoreLogger which are associated to this instance of Provider.
func (*Provider) GetAllLevels ¶
GetAllLevels implements log.Provider#GetAllLevels()
func (*Provider) GetAllOf ¶
GetAllOf returns all instances of log.Event which where recorded so far by the instance of named CoreLogger which are associated to this instance of Provider.
func (*Provider) GetAllRoot ¶
GetAllRoot returns all instances of log.Event which where recorded so far by the instance of root CoreLogger which is associated to this instance of Provider.
func (*Provider) GetFieldKeysSpec ¶
GetFieldKeysSpec implements log.Provider#GetFieldKeysSpec()
func (*Provider) GetLevel ¶
GetLevel returns the current level.Level where this log.Provider is set to.
func (*Provider) GetRootLogger ¶
GetRootLogger implements log.Provider#GetRootLogger()
func (*Provider) HookGlobally ¶
func (instance *Provider) HookGlobally() func()
HookGlobally hooks itself into the global provider registry and forces this provider with all its loggers to be used by all libraries. It also returns a function pointer which should be executed when you're done. It will set everything back as it was before. Usually this should be done by using a defer statement.
This is quite useful in tests where you want to record also test outputs and want to reset everything afterwards into a clean state.
func (*Provider) Len ¶
Len the number of all events recorded by all of the CoreLoggers of this Provider.
func (*Provider) MustContains ¶
MustContains is like Contains but will panic on errors instead returning them.
func (*Provider) MustContainsCustom ¶
MustContainsCustom is like ContainsCustom but will panic on errors instead returning them.
func (*Provider) Reset ¶
Reset removes all recorded entries from the instance of named CoreLogger which is associated to this instance of Provider.
func (*Provider) ResetAll ¶
func (instance *Provider) ResetAll()
ResetAll removes all recorded entries from all instances of CoreLogger which are associated to this instance of Provider.