Documentation
¶
Index ¶
Constants ¶
const ( LevelDisabledString = "disabled" LevelErrorString = "error" LevelWarnString = "warn" LevelInfoString = "info" LevelDebugString = "debug" LevelTraceString = "trace" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config interface {
// LevelForNamespace returns a logging Level for particular namespace.
LevelForNamespace(namespace string) Level
}
Config describes an interface which provides a method for getting a logging level for a particular namespace.
func NewConfigFromString ¶
NewConfigFromString parses the provided string and returns Config.
type ConfigMap ¶
ConfigMap reads the configuration from a CSV string. For example it can be easily used for reading the configuration from an environment variable.
type Factory ¶
type Factory interface {
// Ctx returns the current logger's context.
Ctx() Ctx
// WithCtx returns a new Logger with context appended to existing context.
WithCtx(Ctx) Logger
// WithFormatter returns a new Logger with formatter set.
WithFormatter(Formatter) Logger
// WithWriter returns a new Logger with writer set.
WithWriter(io.Writer) Logger
// WithNamespace returns a new Logger with namespace set.
WithNamespace(namespace string) Logger
// WithNamespaceAppended returns a new Logger with namespace appended.
WithNamespaceAppended(namespace string) Logger
// WithConfig returns a new Logger with config set.
WithConfig(config Config) Logger
}
type Formatter ¶
type Formatter interface {
// Format formats the logging context for transport.
Format(message Message) ([]byte, error)
}
Formatter defines the rules on how to format the logging context before transport. For example, a Formatter might prepare the context for writing to a log file, or serialize it to JSON before sending the bytes to transport.
type Level ¶
type Level int
Level defines the logging level.
const ( // LevelUnknown is an unknown level. LevelUnknown Level = iota - 1 // LevelDisabled means the logging is disabled and no messages will be logged. LevelDisabled // LevelError means only error messages will be logged. LevelError // LevelWarn means only warning, error and trace messages will be logged. LevelWarn // LevelInfo means only info, warning, error and trace messages will be // logged. LevelInfo // LevelDebug means debug, info, warning, error and trace messages will be // logged. LevelDebug // LevelTrace means all messages will be logged. LevelTrace )
func LevelFromString ¶
func (Level) LevelForNamespace ¶
LevelForNamespace implements Config. When a Level is passed as a config, all namespaces will have the same log level.
type Logger ¶
type Logger interface {
Factory
// Level returns the current logger's level.
Level() Level
Namespace() string
// IsLevelEnabled returns true when Level is enabled, false otherwise.
IsLevelEnabled(level Level) bool
// Trace adds a log entry with level trace.
Trace(message string, ctx Ctx) (int, error)
// Debug adds a log entry with level debug.
Debug(message string, ctx Ctx) (int, error)
// Info adds a log entry with level info.
Info(message string, ctx Ctx) (int, error)
// Warn adds a log entry with level warn.
Warn(message string, ctx Ctx) (int, error)
// Error adds a log entry with level error.
Error(message string, err error, ctx Ctx) (int, error)
}
Logger is an interface for logger.
func New ¶
func New() Logger
New returns a new Logger with default StringFormatter. Be sure to call WithConfig to set the required levels for different namespaces.
func NewFromEnv ¶
type Message ¶
type Message struct {
// Timestamp contains the time of the message.
Timestamp time.Time
// Namespace is the full namespace of the Logger this message was sent to.
Namespace string
// Level is the log level of the message.
Level Level
// Body has the message contents.
Body string
// Ctx is the message context.
Ctx Ctx
}
type StringFormatter ¶
type StringFormatter struct {
// contains filtered or unexported fields
}
StringFormatter is the default implementation of Formatter and it prepares the ctx for printing to stdout/stderr or a file.
func NewStringFormatter ¶
func NewStringFormatter(params StringFormatterParams) *StringFormatter
NewStringFormatter creates a new instance of StringFormatter.
type StringFormatterParams ¶
type StringFormatterParams struct {
// DateLayout is the layout to be passed to time.Time.Format function for
// formatting logging timestamp.
DateLayout string
// DisableContextKeySorting will not sort context keys before printing them.
DisableContextKeySorting bool
}
StringFormatterParams are parameters for StringFormatter.