Documentation ¶
Overview ¶
Package native holds the reference implementation of a logger of the slf4g framework (https://github.com/echocat/slf4g).
Usage ¶
For the most common cases it is fully enough to anonymously import this package in your main.go; nothing more is needed.
github.com/foo/bar/main/main.go:
package main import ( "github.com/foo/bar" _ "github.com/echocat/slf4g/native" ) func main() { bar.SayHello() }
github.com/foo/bar/bar.go:
package bar import ( "github.com/echocat/slf4g" ) func SayHello() { log.Info("Hello, world!") }
See more useful stuff in the examples sections.
Example (Customization) ¶
package main import ( "os" "github.com/echocat/slf4g/native/location" "github.com/echocat/slf4g/level" "github.com/echocat/slf4g/native" "github.com/echocat/slf4g/native/color" "github.com/echocat/slf4g/native/consumer" "github.com/echocat/slf4g/native/formatter" "github.com/echocat/slf4g/native/interceptor" ) func main() { // Set the log level globally to Debug native.DefaultProvider.Level = level.Debug // Configure the text formatter to be used. formatter.Default = formatter.NewText(func(v *formatter.Text) { // ... which never colorizes something. v.ColorMode = color.ModeNever // ... and just prints hours, minutes and seconds v.TimeLayout = "150405" }) // Configures a writer consumer that writes everything to stdout (instead // of stderr; which is the default) consumer.Default = consumer.NewWriter(os.Stdout) // Add an interceptor which will exit the application if someone logs // something on level.Fatal or above. This is disabled by default. interceptor.Default.Add(interceptor.NewFatal()) // Change the location.Discovery to log everything detail instead of // simplified (which is the default). location.DefaultDiscovery = location.NewCallerDiscovery(func(t *location.CallerDiscovery) { t.ReportingDetail = location.CallerReportingDetailDetailed }) }
Output:
Index ¶
- Variables
- type CoreLogger
- func (instance *CoreLogger) Accepts(e log.Event) bool
- func (instance *CoreLogger) GetLevel() level.Level
- func (instance *CoreLogger) GetName() string
- func (instance *CoreLogger) GetProvider() log.Provider
- func (instance *CoreLogger) IsLevelEnabled(level level.Level) bool
- func (instance *CoreLogger) Log(event log.Event, skipFrames uint16)
- 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) SetLevel(level level.Level)
- type CoreLoggerCustomizer
- type FieldKeysSpec
- type FieldKeysSpecImpl
- type Provider
- func (instance *Provider) GetAllLevels() level.Levels
- func (instance *Provider) GetConsumer() consumer.Consumer
- func (instance *Provider) GetFieldKeysSpec() fields.KeysSpec
- func (instance *Provider) GetLevel() level.Level
- func (instance *Provider) GetLevelNames() level.Names
- func (instance *Provider) GetLogger(name string) log.Logger
- func (instance *Provider) GetName() string
- func (instance *Provider) GetRootLogger() log.Logger
- func (instance *Provider) SetConsumer(v consumer.Consumer)
- func (instance *Provider) SetLevel(v level.Level)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultFieldKeysSpec = &FieldKeysSpecImpl{}
DefaultFieldKeysSpec is the default instance of FieldKeysSpec which should cover the majority of cases.
var DefaultProvider = &Provider{}
Functions ¶
This section is empty.
Types ¶
type CoreLogger ¶
type CoreLogger struct { Level level.Level Consumer consumer.Consumer LocationDiscovery location.Discovery // contains filtered or unexported fields }
CoreLogger implements log.CoreLogger of the slf4g framework for the "native" implementation.
You cannot create a working instance of this by yourself. It can only be done by the Provider instance. If you want to customize it you can use Provider.CoreLoggerCustomizer to done this.
func (*CoreLogger) Accepts ¶ added in v0.10.0
func (instance *CoreLogger) Accepts(e log.Event) bool
Accepts implements log.CoreLogger#Accepts()
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(level level.Level) bool
IsLevelEnabled implements log.CoreLogger#IsLevelEnabled()
func (*CoreLogger) Log ¶ added in v0.3.0
func (instance *CoreLogger) Log(event log.Event, skipFrames uint16)
Log implements log.CoreLogger#Log()
func (*CoreLogger) NewEventWithFields ¶ added in v0.10.0
func (instance *CoreLogger) NewEventWithFields(l level.Level, f fields.ForEachEnabled) log.Event
NewEventWithFields provides a shortcut if an event should directly created from fields.
func (*CoreLogger) SetLevel ¶
func (instance *CoreLogger) SetLevel(level level.Level)
SetLevel changes the current level.Level of this log.CoreLogger. If set to 0 it use the value of Provider.GetLevel().
type CoreLoggerCustomizer ¶ added in v0.9.0
type CoreLoggerCustomizer func(*Provider, *CoreLogger) log.CoreLogger
CoreLoggerCustomizer can be used by the Provider to customize created instances of CoreLogger. See Provider.CoreLoggerCustomizer
type FieldKeysSpec ¶ added in v0.9.0
type FieldKeysSpec interface { fields.KeysSpec // GetLocation defines the key location information of logged event are // stored inside. Such as the calling method, ... GetLocation() string }
FieldKeysSpec defines the field keys supported this implementation of slf4g.
It is an extension of the default fields.KeysSpec.
func NewFieldKeysSpecFacade ¶ added in v0.9.0
func NewFieldKeysSpecFacade(provider func() FieldKeysSpec) FieldKeysSpec
NewFieldKeysSpecFacade creates a facade of FieldKeysSpec using the given provider.
type FieldKeysSpecImpl ¶ added in v0.9.0
type FieldKeysSpecImpl struct { fields.KeysSpecImpl // Location defines the used key of an location. // If empty "location" will be used instead. Location string }
FieldKeysSpecImpl is a default implementation of FieldKeysSpec.
func (*FieldKeysSpecImpl) GetLocation ¶ added in v0.9.0
func (instance *FieldKeysSpecImpl) GetLocation() string
GetLocation implements FieldKeysSpec#GetLocation()
type Provider ¶
type Provider struct { // Name represents the name of this Provider. If empty it will be "native" // by default. Name string // Level represents the level.Level of this Provider that is at least // required that the loggers managed by this Provider will respect logged // events. This can be overwritten by individual loggers. If this value is // not set it will be log.Info by default. Level level.Level // LevelNames is used to format the levels as human-readable // representations. If this is not set it will be level.DefaultNames by // default. LevelNames level.Names // LevelProvider is used to determine the log.Levels support by this // Provider and all of its managed loggers. If this is not set it will be // level.GetProvider() by default. LevelProvider level.Provider // Consumer is used to handle the logged events with. If this is not set it // will be consumer.Default by default. Consumer consumer.Consumer // LocationDiscovery is used to discover the location.Location where events // are happen. If this is not set it will be location.DefaultDiscovery by // default. LocationDiscovery location.Discovery // FieldKeysSpec defines what are the keys of the major fields managed by // this Provider and its managed loggers. If this is not set it will be // DefaultFieldKeysSpec by default. FieldKeysSpec FieldKeysSpec // CoreLoggerCustomizer will be called in every moment a logger instance // needs to be created (if configured). CoreLoggerCustomizer CoreLoggerCustomizer // contains filtered or unexported fields }
Provider implements log.Provider of the slf4g framework for the "native" implementation.
Usually you should not be required to create by your self. Either use simply log.GetProvider() (which will return this provider once you imported this package at least one time somewhere) or if you want to customize its behavior simply modify DefaultProvider.
func (*Provider) GetAllLevels ¶ added in v0.4.0
GetAllLevels implements log.Provider#GetAllLevels()
func (*Provider) GetConsumer ¶
GetConsumer returns the current consumer.Consumer where this log.Provider is set to.
func (*Provider) GetFieldKeysSpec ¶ added in v0.6.0
GetFieldKeysSpec implements log.Provider#GetFieldKeysSpec()
func (*Provider) GetLevel ¶
GetLevel returns the current level.Level where this log.Provider is set to.
func (*Provider) GetLevelNames ¶ added in v0.3.0
GetLevelNames returns an instance of level.Names that support by formatting level.Level managed by this Provider.
func (*Provider) GetRootLogger ¶ added in v0.9.0
GetRootLogger implements log.Provider#GetRootLogger()
func (*Provider) SetConsumer ¶
SetConsumer changes the current consumer.Consumer of this log.Provider. If set to nil consumer.Default will be used.
Directories ¶
Path | Synopsis |
---|---|
Package color provides function to detect color support and also describe how the application should behave.
|
Package color provides function to detect color support and also describe how the application should behave. |
Package consumer provides the functionally to print log events either to console, files, ...
|
Package consumer provides the functionally to print log events either to console, files, ... |
facade
|
|
value
Package value provides a value facade for native.Provider to be able to easy be configured using flag libraries like the SDK implementation or other compatible ones.
|
Package value provides a value facade for native.Provider to be able to easy be configured using flag libraries like the SDK implementation or other compatible ones. |
Formatter is used to format log events to a format which can logged to a console, file, ...
|
Formatter is used to format log events to a format which can logged to a console, file, ... |
Hints are used while the processing of events and can adjust the behaviour how stuff is processed/handled.
|
Hints are used while the processing of events and can adjust the behaviour how stuff is processed/handled. |
Interceptors are used to intercept instances of log.Event that are requested to be logged.
|
Interceptors are used to intercept instances of log.Event that are requested to be logged. |
internal
|
|
Package native/level provides additions of slf4g/level for the native implementations.
|
Package native/level provides additions of slf4g/level for the native implementations. |
Location defines where a log event happens.
|
Location defines where a log event happens. |