Documentation
¶
Overview ¶
Package testlog provides a Logger which will be connected to testing.T.Log() of the go SDK.
If you're looking for an instance to record all logged events see github.com/echocat/slf4g/testing/recording package.
Usage ¶
The easiest way to enable the slf4g framework in your tests is, simply:
import ( "testing" "github.com/echocat/slf4g" "github.com/echocat/slf4g/sdk/testlog" ) func TestMyGreatStuff(t *testing.T) { testlog.Hook(t) log.Info("Yeah! This is a log!") }
... that's it!
See Hook(..) for more details.
Index ¶
- Constants
- Variables
- func AllLevels(v level.Levels) func(*Provider)
- func FailAtLevel(v level.Level) func(*Provider)
- func FailNowAtLevel(v level.Level) func(*Provider)
- func FieldKeysSpec(v fields.KeysSpec) func(*Provider)
- func Level(v level.Level) func(*Provider)
- func LevelFormatter(v tlevel.Formatter) func(*Provider)
- func Name(v string) func(*Provider)
- func NewLogger(tb testing.TB, customizer ...func(*Provider)) log.Logger
- func NewNamedLogger(tb testing.TB, name string, customizer ...func(*Provider)) log.Logger
- func TimeFormat(v string) func(*Provider)
- type Provider
- func (instance *Provider) GetAllLevels() level.Levels
- 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) SetLevel(v level.Level)
Constants ¶
const ( // DefaultLevel specifies the default level.Level of an instance of Provider // which be used if no other level was defined. DefaultLevel = level.Debug // NeverFailLevel is used for Provider.FailAtLevel and indicates that regardless // at which level each log.Event is logged, this event will never lead to a fail of // the tests. NeverFailLevel = level.Level(65535) // NoopTimeFormat tells the Provider to not print any timestamp in the log messages. NoopTimeFormat = "<noop time format>" // SinceTestStartedMcsTimeFormat tells the Provider to print only the microseconds since // the test started (is based on Hook() and/or NewProvider()). SinceTestStartedMcsTimeFormat = "<since test started>" )
const RootLoggerName = "ROOT"
RootLoggerName specifies the name of the root version of coreLogger instances which are managed by Provider.
Variables ¶
var ( // DefaultFailAtLevel is used if FailAtLevel was not used. DefaultFailAtLevel = level.Error // DefaultFailNowAtLevel is used if FailNowAtLevel was not used. DefaultFailNowAtLevel = level.Fatal // DefaultTimeFormat is used if TimeFormat was not used. DefaultTimeFormat = SinceTestStartedMcsTimeFormat )
Functions ¶
func AllLevels ¶
AllLevels specifies the levels which are supported by the Provider and all of its loggers. By default, the Provider will use level.GetProvider()#GetLevels().
func FailAtLevel ¶
FailAtLevel defines a level.Level at which log.Event will lead to a test failure after each code of the test has passed (in contrast to FailNowAtLevel which will fail immediately) if they're logged with this a log.Logger handled by the Provider. If set to NeverFailLevel nothing happens. By default, the Provider will use DefaultFailAtLevel.
func FailNowAtLevel ¶
FailNowAtLevel defines a level.Level at which log.Event will lead to a test fails immediately (in contrast to FailAtLevel which allows to test to finish) if they're logged with this a log.Logger handled by the Provider. If set to NeverFailLevel nothing happens. By default, the Provider will use DefaultFailNowAtLevel.
func FieldKeysSpec ¶
FieldKeysSpec specifies the spec of the fields are supported by the Provider and all of its loggers. By default, the Provider will use the default instance of fields.KeysSpecImpl.
func Level ¶
Level specifies the level of the Provider which will be also inherited by all of its loggers. By default, the Provider will use DefaultLevel.
func LevelFormatter ¶
LevelFormatter formats the levels on printing. By default, the Provider will use level.DefaultFormatter.
func Name ¶
Name specifies the name of the Provider. By default, the Provider will use testing.TB#Name().
func NewLogger ¶
NewLogger creates a new instance of log.Logger ready to use. If you want to use a direct instance of a logger, this is the easiest way to get it.
This is a shortcut for NewProvider(..).GetRootLogger().
func NewNamedLogger ¶
NewNamedLogger creates a new instance of log.Logger ready to use. If you want to use a direct instance of a logger with a specific name, this is the easiest way to get it.
This is a shortcut for NewProvider(..).GetLogger(...).
func TimeFormat ¶
TimeFormat defines how each entry will be formatted on print. If NoopTimeFormat is used, nothing will be printed. If SinceTestStartedMcsTimeFormat will be used no time is printed, but the microseconds since the test started. By default, the Provider will use DefaultTimeFormat. See time.Layout for more details.
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is an implementation of log.Provider which ensures that everything is logged using testing.TB#Log(). Use NewProvider(..) to get a new instance.
func Hook ¶
Hook creates and registers for the given *testing.T, *testing.B or *testing.F a new instance of a log.Logger / log.Provider.
The related Provider will be automatically cleanup at the end of the related test run (see testing.TB#Cleanup).
customizer can be used to change the behavior of the managed Provider.
The method returns the related Provider instance but while the test run it is also available via log.GetProvider().
func NewProvider ¶
NewProvider creates a new instance of Provider which is ready to use.
tb should hold an instance of either *testing.T, *testing.B or *testing.F.
customizer can be used to change the behavior of the Provider:
- Level
- FailAtLevel
- FailNowAtLevel
- TimeFormat
- LevelFormatter
- Name
- AllLevels
- FieldKeysSpec
func (*Provider) GetAllLevels ¶
GetAllLevels implements log.Provider#GetAllLevels()
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()