log

package
v2.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package log contains rig's logging related types, constants and functions.

Index

Constants

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

View Source
var Discard slog.Handler = discardHandler{}

Discard is a slog handler that discards all log records.

View Source
var (
	// Null logger is a no-op logger that does nothing.
	Null = slog.New(Discard)
)

Functions

func ErrorAttr

func ErrorAttr(err error) slog.Attr

ErrorAttr returns an error log attribute.

func FileAttr

func FileAttr(file string) slog.Attr

FileAttr returns a file/path log attribute.

func HasLogger

func HasLogger(obj any) bool

HasLogger returns true if the object implements the Log interface and has a logger set.

func HostAttr

func HostAttr(conn fmt.Stringer) slog.Attr

HostAttr returns a host log attribute.

func InjectLogger

func InjectLogger(l Logger, obj any, attrs ...any)

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.

func Trace

func Trace(ctx context.Context, msg string, keysAndValues ...any)

Trace is for rig's internal trace logging that must be separately enabled by providing a TraceLogger logger, which is implemented by slog.Logger.

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.

func GetLogger

func GetLogger(obj any) Logger

GetLogger returns the logger for the given object if it implements the Log interface or a Null logger.

func WithAttrs

func WithAttrs(logger Logger, attrs ...any) Logger

WithAttrs returns a logger that prepends the given attributes to all log messages.

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.

type TraceLogger

type TraceLogger interface {
	Log(ctx context.Context, level slog.Level, msg string, keysAndValues ...any)
}

TraceLogger is a logger for rig's internal trace logging.

Jump to

Keyboard shortcuts

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