testlog

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 13 Imported by: 0

README

PkgGoDev

slf4g testing Logger implementation

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:

package foo

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.

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

View Source
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>"
)
View Source
const RootLoggerName = "ROOT"

RootLoggerName specifies the name of the root version of coreLogger instances which are managed by Provider.

Variables

View Source
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

func AllLevels(v level.Levels) func(*Provider)

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

func FailAtLevel(v level.Level) func(*Provider)

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

func FailNowAtLevel(v level.Level) func(*Provider)

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

func FieldKeysSpec(v fields.KeysSpec) func(*Provider)

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

func Level(v level.Level) func(*Provider)

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

func LevelFormatter(v tlevel.Formatter) func(*Provider)

LevelFormatter formats the levels on printing. By default, the Provider will use level.DefaultFormatter.

func Name

func Name(v string) func(*Provider)

Name specifies the name of the Provider. By default, the Provider will use testing.TB#Name().

func NewLogger

func NewLogger(tb testing.TB, customizer ...func(*Provider)) log.Logger

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

func NewNamedLogger(tb testing.TB, name string, customizer ...func(*Provider)) log.Logger

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

func TimeFormat(v string) func(*Provider)

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

func Hook(tb testing.TB, customizer ...func(*Provider)) *Provider

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

func NewProvider(tb testing.TB, customizer ...func(*Provider)) *Provider

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

func (instance *Provider) GetAllLevels() level.Levels

GetAllLevels implements log.Provider#GetAllLevels()

func (*Provider) GetFieldKeysSpec

func (instance *Provider) GetFieldKeysSpec() fields.KeysSpec

GetFieldKeysSpec implements log.Provider#GetFieldKeysSpec()

func (*Provider) GetLevel

func (instance *Provider) GetLevel() level.Level

GetLevel returns the current level.Level where this log.Provider is set to.

func (*Provider) GetLogger

func (instance *Provider) GetLogger(name string) log.Logger

GetLogger implements log.Provider#GetLogger()

func (*Provider) GetName

func (instance *Provider) GetName() string

GetName implements log.Provider#GetName()

func (*Provider) GetRootLogger

func (instance *Provider) GetRootLogger() log.Logger

GetRootLogger implements log.Provider#GetRootLogger()

func (*Provider) SetLevel

func (instance *Provider) SetLevel(v level.Level)

SetLevel changes the current level.Level of this log.Provider. If set to 0 it will force this Provider to use DefaultLevel.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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