logger

package
v1.0.2-0...-0a9ac6a Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

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

func NewConfig(configMap ConfigMap) Config

func NewConfigFromString

func NewConfigFromString(stringConfig string) Config

NewConfigFromString parses the provided string and returns Config.

type ConfigMap

type ConfigMap map[string]Level

ConfigMap reads the configuration from a CSV string. For example it can be easily used for reading the configuration from an environment variable.

type Ctx

type Ctx map[string]interface{}

Ctx is the logging context for each logging entry.

func (Ctx) WithCtx

func (c Ctx) WithCtx(newCtx Ctx) Ctx

WithCtx returns a new context which is a result of a merge of the current and the new context. The current context is not modified.

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 LevelFromString(str string) (Level, bool)

func (Level) LevelForNamespace

func (l Level) LevelForNamespace(_ string) Level

LevelForNamespace implements Config. When a Level is passed as a config, all namespaces will have the same log level.

func (Level) String

func (l Level) String() string

String returns a string representation of 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

func NewFromEnv(key string) Logger

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.

func (*StringFormatter) Format

func (f *StringFormatter) Format(message Message) ([]byte, error)

Format implements Formatter.

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.

Jump to

Keyboard shortcuts

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