Documentation ¶
Overview ¶
Package log contains rig's logging related types, constants and functions.
Index ¶
- Constants
- Variables
- func ErrorAttr(err error) slog.Attr
- func FileAttr(file string) slog.Attr
- func HasLogger(obj any) bool
- func HostAttr(conn fmt.Stringer) slog.Attr
- func InjectLogger(l Logger, obj any, attrs ...any)
- func SetTraceLogger(l TraceLogger)
- func Trace(ctx context.Context, msg string, keysAndValues ...any)
- type Log
- type Logger
- type LoggerInjectable
- type TraceLogger
Constants ¶
const ( // KeyHost is the host name or address. KeyHost = "host" // KeyExitCode is the exit code of a command. KeyExitCode = "exitCode" // KeyError is an error. KeyError = "error" // KeyBytes is the number of bytes. KeyBytes = "bytes" // KeyDuration is the duration of an operation. KeyDuration = "duration" // KeyCommand is a command-line. KeyCommand = "command" // KeyFile is a file name. KeyFile = "file" // KeySudo is a boolean indicating whether a command is run with sudo. KeySudo = "sudo" // KeyProtocol is a network protocol. KeyProtocol = "protocol" // KeyComponent is a component name. KeyComponent = "component" )
Variables ¶
var Discard slog.Handler = discardHandler{}
Discard is a slog handler that discards all log records.
var ( // Null logger is a no-op logger that does nothing. Null = slog.New(Discard) )
Functions ¶
func HasLogger ¶
HasLogger returns true if the object implements the Log interface and has a logger set.
func InjectLogger ¶
InjectLogger sets the logger for the given object if it implements the injectable interface.
func SetTraceLogger ¶
func SetTraceLogger(l TraceLogger)
SetTraceLogger sets a trace logger. Some of the rig's internal logging is sent to a separate trace logger. It should be quite rare to use this function outside of rig development. It, and all the log.Trace calls will likely be removed once the code is confirmed to be working as expected.
Types ¶
type Log ¶
type Log interface {
Log() Logger
}
Log interface is implemented by the LoggerInjectable struct.
type Logger ¶
type Logger interface { Debug(msg string, keysAndValues ...any) Info(msg string, keysAndValues ...any) Warn(msg string, keysAndValues ...any) Error(msg string, keysAndValues ...any) }
Logger interface is implemented by slog.Logger and some other logging packages and can be easily used via a wrapper with any other logging system. The functions are not sprintf-style. Keys and values are key-value pairs.
type LoggerInjectable ¶
type LoggerInjectable struct {
// contains filtered or unexported fields
}
LoggerInjectable is a struct that can be embedded in other structs to provide a logger and a log setter.
func (*LoggerInjectable) HasLogger ¶
func (li *LoggerInjectable) HasLogger() bool
HasLogger returns true if a logger has been set.
func (*LoggerInjectable) InjectLoggerTo ¶
func (li *LoggerInjectable) InjectLoggerTo(obj any, attrs ...any)
InjectLoggerTo sets the logger for the given object if it implements the injectable interface based on the logger of the current object, optionally with extra attributes.
func (*LoggerInjectable) Log ¶
func (li *LoggerInjectable) Log() Logger
Log returns the logger for the embedding object.
func (*LoggerInjectable) LogWithAttrs ¶
func (li *LoggerInjectable) LogWithAttrs(attrs ...any) Logger
LogWithAttrs returns an instance of the logger with the given attributes applied.
func (*LoggerInjectable) SetLogger ¶
func (li *LoggerInjectable) SetLogger(logger Logger)
SetLogger sets the logger for the embedding object.