Documentation ¶
Overview ¶
Package slf provides a Structured Log Facade for Go and factory functions to retrieve a a logger instance in code using this interface. Its use is analogous to slf4j in Java, with the difference that "s" stands here for structured rather than simple..
The package not provide any actual logger implementation with the exception of the internal noop one (No Operation) delivered by default via the factory functions to permit using the interface without any further configuration. For a matching logger implementation see e.g. github.com/KristinaEtc/slog.
Index ¶
- Variables
- func IsSet() bool
- func Set(log LogFactory)
- type CallerInfo
- type Fields
- type Level
- type LogFactory
- type Logger
- type Noop
- func (log *Noop) Debug(string) Tracer
- func (log *Noop) Debugf(string, ...interface{}) Tracer
- func (log *Noop) Error(string) Tracer
- func (log *Noop) Errorf(string, ...interface{}) Tracer
- func (*Noop) Fatal(string)
- func (*Noop) Fatalf(string, ...interface{})
- func (log *Noop) Info(string) Tracer
- func (log *Noop) Infof(string, ...interface{}) Tracer
- func (log *Noop) Log(level Level, message string) Tracer
- func (*Noop) Panic(message string)
- func (*Noop) Panicf(message string, args ...interface{})
- func (*Noop) Trace(*error)
- func (log *Noop) Warn(string) Tracer
- func (log *Noop) Warnf(string, ...interface{}) Tracer
- func (log *Noop) WithCaller(CallerInfo) StructuredLogger
- func (log *Noop) WithContext(string) StructuredLogger
- func (log *Noop) WithError(error) Logger
- func (log *Noop) WithField(string, interface{}) StructuredLogger
- func (log *Noop) WithFields(Fields) StructuredLogger
- type StructuredLogger
- type Tracer
Constants ¶
This section is empty.
Variables ¶
var ExitProcessor = func() { os.Exit(1) }
ExitProcessor is executed on Log(LevelFatal) to terminate the application.
Functions ¶
Types ¶
type CallerInfo ¶
type CallerInfo int
CallerInfo defines an enumeration for the types of caller information to output, short or long.
const ( // CallerNone defines no caller info in the log. CallerNone CallerInfo = iota // CallerShort defines short caller info in the log (base name). CallerShort // CallerLong defines long caller info in the log (full path). CallerLong )
type Level ¶
type Level int
Level represents log level of the structured logger.
Log level constants.
func (Level) MarshalJSON ¶
MarshalJSON provides a JSON representation of the log level.
func (*Level) UnmarshalJSON ¶
UnmarshalJSON parses the JSON representation of the log level into a Level object.
type LogFactory ¶
type LogFactory interface { // WithContext returns a logger with context set to a string. WithContext(string) StructuredLogger }
LogFactory represents a logger API for structured logging.
type Logger ¶
type Logger interface { // Log logs the string with the given level. Log(Level, string) Tracer // Debug logs the string with the corresponding level. Debug(string) Tracer // Debugf formats and logs the string with the corresponding level. Debugf(string, ...interface{}) Tracer // Info logs the string with the corresponding level. Info(string) Tracer // Infof formats and logs the string with the corresponding level. Infof(string, ...interface{}) Tracer // Warn logs the string with the corresponding level. Warn(string) Tracer // Warnf formats and logs the string with the corresponding level. Warnf(string, ...interface{}) Tracer // Error logs the string with the corresponding level. Error(string) Tracer // Errorf formats and logs the string with the corresponding level. Errorf(string, ...interface{}) Tracer // Panic logs the string with the corresponding level and panics. Panic(string) // Panicf formats and logs the string with the corresponding level and panics. Panicf(string, ...interface{}) // Fatal logs the string with the corresponding level and then calls os.Exit(1). Fatal(string) // Fatalf formats and logs the string with the corresponding level and then calls os.Exit(1). Fatalf(string, ...interface{}) }
Logger represents a generic leveled log interface.
type Noop ¶
type Noop struct{}
Noop implements the LogFactory, StructuredLogger and Logger interfaces with void operations (except for panic in Panic and Panicf, and os.Exit(1) in Fatal and Fatalf).
func (*Noop) WithCaller ¶
func (log *Noop) WithCaller(CallerInfo) StructuredLogger
WithCaller implements the Logger interface.
func (*Noop) WithContext ¶
func (log *Noop) WithContext(string) StructuredLogger
WithContext implements the Logger interface.
func (*Noop) WithField ¶
func (log *Noop) WithField(string, interface{}) StructuredLogger
WithField implements the Logger interface.
func (*Noop) WithFields ¶
func (log *Noop) WithFields(Fields) StructuredLogger
WithFields implements the Logger interface.
type StructuredLogger ¶
type StructuredLogger interface { Logger // WithField adds a named data field to the logger context. WithField(string, interface{}) StructuredLogger // WithFields adds a number of named fields to the logger context. WithFields(Fields) StructuredLogger // WithCaller adds caller information to the data fields. WithCaller(CallerInfo) StructuredLogger // WithError adds an error record to the logger context (only one permitted). WithError(error) Logger }
StructuredLogger represents a logger that can define a structured context by adding data fields..
func WithContext ¶
func WithContext(context string) StructuredLogger
WithContext returns a logger with context set to the given string.