Documentation ¶
Overview ¶
Package logging provides a wrapper around the logr logging system supporting a rule based approach to enable log levels for dedicated message contexts specified at the logging location. For further information see https://github.com/mandelsoft/logging.
Index ¶
- Constants
- func DynSink(level LevelFunc, delta int, orig SinkFunc) logr.LogSink
- func KeyValue(key string, value interface{}) *keyValue
- func LevelName(l int) string
- func ParseLevel(s string) (int, error)
- func SetDefaultContext(ctx Context)
- func UnwrapLogSink(s logr.LogSink) logr.LogSink
- func WrapSink(level, delta int, orig logr.LogSink) logr.LogSink
- type AndExpr
- type Attacher
- type Attribute
- type AttributionContext
- type AttributionContextProvider
- type Condition
- type ConditionRule
- type Context
- func DefaultContext() Context
- func LoggingContext(p interface{}) Context
- func New(logger logr.Logger, writer ...io.Writer) Context
- func NewDefault() Context
- func NewWithBase(base Context, baselogger ...logr.Logger) Context
- func NewWithBaseAndTechnicalSink(base Context, writer io.Writer, baselogger ...logr.Logger) Context
- type ContextId
- type ContextProvider
- type ContextReference
- type ContextSupport
- type Definitions
- type LevelFunc
- type Logger
- type LoggerProvider
- type MessageContext
- type MessageContextProvider
- type Name
- type NotExpr
- type OrExpr
- type Realm
- type RealmPrefix
- type Rule
- type SinkFunc
- type Tag
- type UnboundLogger
- type UpdatableRule
- type UpdateState
- type Updater
Constants ¶
const ( // None level. No logging, None = iota // ErrorLevel level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. ErrorLevel // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel // InfoLevel level. General operational entries about what's going on inside the // application. InfoLevel // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel // TraceLevel level. Designates finer-grained informational events than the Debug. TraceLevel )
These are the different logging levels. You can set the logging level to log on your instance of logger.
const FieldKeyRealm = "realm"
FieldKeyRealm is the name of the logr field set to the realm of a logging message.
Variables ¶
This section is empty.
Functions ¶
func KeyValue ¶
func KeyValue(key string, value interface{}) *keyValue
KeyValue provide a key/value pair for the argument list of logging methods.
Those values can be used as single argument representing a key/value pair together with a sequence of key and value arguments in the argument list of the logging methods.
This function can be used to define standardized keys for various use cases(see package github.com/mandelsoft/logging/keyvalue).
func LevelName ¶
LevelName returns the logical name of a log level. It can be parsed again with ParseLevel.
func ParseLevel ¶
ParseLevel maps a string representation of a log level to it internal value. It also accepts the representation as number.
func SetDefaultContext ¶
func SetDefaultContext(ctx Context)
SetDefaultContext sets the default context. It changes all usages based on the result of DefaultContext(). If the rules of the actual default context should be kept use it as base context or copy the rules with DefaultContext().AddTo(newContext) before setting the new context as default context (potentially unsafe because of race condition).
func UnwrapLogSink ¶
UnwrapLogSink return the original (unmapped) logr.LogSink.
Types ¶
type AndExpr ¶
type AndExpr struct {
// contains filtered or unexported fields
}
func (*AndExpr) Conditions ¶
func (*AndExpr) Match ¶
func (e *AndExpr) Match(messageContext ...MessageContext) bool
type Attacher ¶
Attacher is an optional interface, which can be implemented by a dedicated type of message context. If available it is used to enrich the attributes of a determined logger to describe the given context.
type Attribute ¶
Attribute is a key/value pair usable as logging condition or message context. If used as message context it will be attached to the logging message as additional value.
func NewAttribute ¶
type AttributionContext ¶
type AttributionContext interface { ContextProvider AttributionContextProvider MessageContextProvider LoggerProvider // WithContext provides a new logging Context enriched by the given standard // message context WithContext(messageContext ...MessageContext) AttributionContext // WithName provides a new AttributionContext adding // a name component to provided [Logger] objects. WithName(name string) AttributionContext // WithValues adds keypairs implicitly used for [Logger] object // creation.. WithValues(keypairs ...interface{}) AttributionContext // Match evaluates a condition against the message context. Match(cond Condition) bool }
AttributionContext describes a Context, a MessageContext and a set of preset values. which can be used to gain a Logger or a subsequent AttributionContext.
func NewAttributionContext ¶
func NewAttributionContext(ctxp ContextProvider, messageContext ...MessageContext) AttributionContext
NewAttributionContext returns a new AttributionContext for a Context with an optional additional MessageContext.
type AttributionContextProvider ¶
type AttributionContextProvider interface {
AttributionContext() AttributionContext
}
AttributionContextProvider is an interface for all object able to provide acces to an AttributionContext.
type Condition ¶
type Condition interface {
Match(...MessageContext) bool
}
Condition matches a given message context. It returns true, if the condition matches the context.
type ConditionRule ¶
type ConditionRule struct {
// contains filtered or unexported fields
}
func (*ConditionRule) Conditions ¶
func (r *ConditionRule) Conditions() []Condition
func (*ConditionRule) Level ¶
func (r *ConditionRule) Level() int
func (*ConditionRule) Match ¶
func (r *ConditionRule) Match(sink SinkFunc, messageContext ...MessageContext) Logger
func (*ConditionRule) MatchRule ¶
func (r *ConditionRule) MatchRule(o Rule) bool
type Context ¶
type Context interface { ContextProvider AttributionContextProvider MessageContextProvider LoggerProvider // GetSink returns the effective logr.LOgSink used as base logger // for this context. // In case of a nested context, this is the locally set sink, if set, // or the sink of the base context. GetSink() logr.LogSink // GetDefaultLevel returns the default log level effective, if no rule matches. // These may be locally defined rules, or, in case of a nested logger, // rules of the base context, also. GetDefaultLevel() int // GetDefaultLogger return the effective default logger used if no rule matches // a message context. GetDefaultLogger() Logger // SetDefaultLevel sets the default logging level to use for provided // Loggers, if no rule matches. SetDefaultLevel(level int) // SetBaseLogger sets a new base logger. // If the optional parameter plain is set to true, the base logger // is rebased to the absolute logr level 0. // Otherwise, the base log level is taken from the given logger. This means // ErrorLevel is mapped to the log level of the given logger. // Although the error output is filtered by this log level by the // original sink, error level output, if enabled, is passed as Error to the sink. SetBaseLogger(logger logr.Logger, plain ...bool) // AddRule adds a rule to the actual context. // It may decide to supersede already existing rules. In // such case all matching rules (interface UpdatableRule) // will be deleted from the active rule set. AddRule(...Rule) // ResetRules deletes the actual rule set. ResetRules() // AddRulesTo add the actual rules to another logging context. AddRulesTo(ctx Context) // WithContext provides a new logging Context enriched by the given standard // message context WithContext(messageContext ...MessageContext) Context // Evaluate returns the effective logger for the given message context // based on the given logr.LogSink. Evaluate(SinkFunc, ...MessageContext) Logger // Match evaluates a condition against the message context. Match(cond Condition) bool // Tree provides an interface for the context intended for // context implementations to work together in a context tree. Tree() ContextSupport }
Context describes the interface of a logging context. A logging context determines effective loggers for a given message context based on a set of rules used to map a message context to an effective logger.
func DefaultContext ¶
func DefaultContext() Context
DefaultContext returns a context, which always reflects the actually set default context.
func LoggingContext ¶
func LoggingContext(p interface{}) Context
LoggingContext returns a default logging context for an arbitrary object. If the object supports the LoggingProvider interface, it is used to determine the context. If not, the default logging context is returned.
func NewDefault ¶
func NewDefault() Context
type ContextProvider ¶
type ContextProvider interface {
LoggingContext() Context
}
ContextProvider is able to provide access to a logging context.
type ContextReference ¶
type ContextReference struct {
Context
}
type ContextSupport ¶
type ContextSupport interface { // UpdateState provides information of the update watermark in a context tree Updater() *Updater // GetBaseContext returns the base context for nested logging contexts. GetBaseContext() Context // GetMessageContext returns the configured standard message context // shared for all created Loggers. GetMessageContext() []MessageContext // LogWriter returns an optional writer finally used to write // log entries. // This writer must explicitly be provided appropriately for the given // logr logger, when creating the logging context- LogWriter() io.Writer }
ContextSupport is intended for Context implementations for working together in a context tree consiting of potentially different implementations. It is not intended for the consumer of a logging context.
type Definitions ¶
func GetRealmDefinitions ¶
func GetRealmDefinitions() Definitions
func GetTagDefinitions ¶
func GetTagDefinitions() Definitions
type Logger ¶
type Logger interface { // LogError logs a given error with additional context. LogError(err error, msg string, keypairs ...interface{}) // Error logs an error message. Error(msg string, keypairs ...interface{}) // Warn logs a warning message. Warn(msg string, keypairs ...interface{}) // Error logs an info message Info(msg string, keypairs ...interface{}) // Debug logs a debug message. Debug(msg string, keypairs ...interface{}) // Trace logs an trace message. Trace(msg string, keypairs ...interface{}) // NewName return a new logger with an extended name, // but the same logging activation. WithName(name string) Logger // WithValues return a new logger with more standard key/value pairs, // but the same logging activation. WithValues(keypairs ...interface{}) Logger // Enabled check whether the logger is active for a dedicated level. Enabled(level int) bool // V returns a logr logger with the same activation state // like the actual logger at call time. V(delta int) logr.Logger }
Logger is the main logging interface. It is used to issue log messages. Additionally, it provides methods to create loggers with extend names and key/value pairs.
func Log ¶
func Log(messageContext ...MessageContext) Logger
type LoggerProvider ¶
type LoggerProvider interface { // Logger return the effective logger for the given message context. Logger(...MessageContext) Logger // V returns the effective logr.Logger for the given message context with // the given base level. V(level int, mctx ...MessageContext) logr.Logger // LoggerFor provides a logger according to rules for a dedicated message // context. There is no default level and no log context involved, // only the base logr sink is used. // If no rule matches the [NonLoggingLogger] is returned. LoggerFor(messageContext ...MessageContext) Logger }
LoggerProvider is the interface to gain access to a logger from some logger source. It is implemented by Context and AttributionContext.
type MessageContext ¶
type MessageContext interface { }
MessageContext is an object providing context information for a log message.
func ExcludeFromMessageContext ¶
func ExcludeFromMessageContext[T any](ctx MessageContext) MessageContext
ExcludeFromMessageContext flattens a given message context and eliminates all entries with the given type.
func ExplodeMessageContext ¶
func ExplodeMessageContext(m MessageContext) []MessageContext
ExplodeMessageContext flattens a given message context.
func JoinMessageContext ¶
func JoinMessageContext(base []MessageContext, list ...MessageContext) []MessageContext
type MessageContextProvider ¶
type MessageContextProvider interface {
GetMessageContext() []MessageContext
}
MessageContextProvider is an interface for objects providing a message context. An UnboundLogger or a Context is such a provider. A provider can again be used wherever a message context is required.
type Name ¶
type Name = name
Name is a simple string value, which can be used as message context. It will not be attached to the logger's name.
type NotExpr ¶
type NotExpr struct {
// contains filtered or unexported fields
}
func (*NotExpr) Match ¶
func (e *NotExpr) Match(messageContext ...MessageContext) bool
type OrExpr ¶
type OrExpr struct {
// contains filtered or unexported fields
}
func (*OrExpr) Conditions ¶
func (*OrExpr) Match ¶
func (e *OrExpr) Match(messageContext ...MessageContext) bool
type Realm ¶
type Realm = realm
Realm is some kind of tag, which can be used as message context or logging condition. If used as message context it will be attached to the logging message as additional logger name.
func DefineRealm ¶
DefineRealm creates a tag and registers it together with a description.
type RealmPrefix ¶
type RealmPrefix = realmprefix
RealmPrefix is used as logging condition to match a realm of the message context by checking its value to be a path prefix.
func NewRealmPrefix ¶
func NewRealmPrefix(name string) RealmPrefix
NewRealmPrefix provides a new RealmPrefix object to be used as rule condition matching a realm prefix.
type Rule ¶
type Rule interface {
Match(SinkFunc, ...MessageContext) Logger
}
Rule matches a given message context and returns an appropriate logger
func NewConditionRule ¶
type SinkFunc ¶
func AsSinkFunc ¶
type Tag ¶
type Tag = tag
Tag is a simple string value, which can be used as message context or logging condition. If used as message context it will not be attached to the logging message at all.
type UnboundLogger ¶
type UnboundLogger interface { ContextProvider AttributionContextProvider Logger GetMessageContext() []MessageContext WithContext(messageContext ...MessageContext) UnboundLogger BoundLogger() Logger }
UnboundLogger is a logger, which is never bound to the settings of a matching rule at the time of its creation. It therefore always reflects the state of the rule settings valid at the time of issuing log messages. They are more expensive than regular loggers, but they can be created and configured once and stored in long living variables.
When passing loggers down a dynamic call tree, to control the logging here, only temporary (bound) loggers should be used (as provided by a logging context) to improve performance.
Such a logger can be reused for multiple independent call trees without losing track to the config. Regular loggers provided by a context keep their setting from the matching rule valid during its creation.
An unbound logger can be created with function DynamicLogger for a logging context.
func DynamicLogger ¶
func DynamicLogger(ctxp AttributionContextProvider, messageContext ...MessageContext) UnboundLogger
DynamicLogger returns an unbound logger, which automatically adapts to rule configuration changes applied to its logging context.
Such a logger can be reused for multiple independent call trees without losing track to the config. Regular loggers provided by a context keep their setting from the matching rule valid during its creation.
type UpdatableRule ¶
UpdatableRule is the optional interface for a rule which might replace an old one. If a rule decides to supersede an old one when adding to a ruleset is may return true. Typically, a rule should only supersede rules of its own type.
type UpdateState ¶
type UpdateState struct {
// contains filtered or unexported fields
}
UpdateState remembers the config level of a root logging context.
func (*UpdateState) Generation ¶
func (s *UpdateState) Generation() int64
Generation returns the actual generation number of a context tree.
func (*UpdateState) Next ¶
func (s *UpdateState) Next() int64
Next provides the next generation number of a context tree.
type Updater ¶
type Updater struct {
// contains filtered or unexported fields
}
Updater is used by a logging context to check for new updates in a context tree.
func NewUpdater ¶
func (*Updater) SeenWatermark ¶
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package keyvalue provides some standard key/value pair values usable for the key/value list of logging messages.
|
Package keyvalue provides some standard key/value pair values usable for the key/value list of logging messages. |
Package logrusl is an adapter to use the github.com/mandelsoft/logging library on top of github.com/sirupsen/logrus.
|
Package logrusl is an adapter to use the github.com/mandelsoft/logging library on top of github.com/sirupsen/logrus. |