log

package
v0.0.0-...-333f614 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TraceLevel is the logging level when set to Trace verbosity.
	TraceLevel = slog.LevelDebug - 1

	// TraceLevelText is the text representation of Trace verbosity.
	TraceLevelText = "TRACE"

	// LevelField is the log field that stores the verbosity.
	LevelField = "level"
	// ComponentField is the log field that stores the calling component.
	ComponentField = "component"
	// CallerField is the log field that stores the calling file and line number.
	CallerField = "caller"
	// TimestampField is the field that stores the timestamp the log was emitted.
	TimestampField = "timestamp"
)

Variables

View Source
var (
	// ErrFileSharedWriterClosed is returned when file shared writer is already closed.
	ErrFileSharedWriterClosed = errors.New("file shared writer is closed")
)
View Source
var SupportedLevelsText = []string{
	TraceLevelText,
	slog.LevelDebug.String(),
	slog.LevelInfo.String(),
	slog.LevelWarn.String(),
	slog.LevelError.String(),
}

SupportedLevelsText lists the supported log levels in their text representation. All strings are in uppercase.

Functions

func Initialize

func Initialize(loggerConfig Config) (*slog.Logger, *slog.LevelVar, error)

Initialize configures the default global logger based on the provided configuration. The slog.Logger and slog.LevelVar

func NewPackageLogger

func NewPackageLogger(args ...any) *slog.Logger

NewPackageLogger creates a slog.Logger that defers setting any groups or attributes until the first use of the logger. This allows package global loggers to be created prior to any custom slog.Handler can be set via slog.Default AND still respect the formatting of the default handler set at runtime.

func NewSyslogWriter

func NewSyslogWriter() (io.Writer, error)

NewSyslogWriter creates a writer that outputs to the local machine syslog.

func StringerAttr

func StringerAttr(s fmt.Stringer) slog.LogValuer

StringerAttr creates a slog.LogValuer that will defer to the provided fmt.Stringer. All slog attributes are always evaluated, even if the log event is discarded due to the configured log level. A text slog.Handler will try to defer evaluation if the attribute is a fmt.Stringer, however, the JSON slog.Handler only defers to [json.Marshaler]. This means that to defer evaluation and creation of the string representation, the object must implement fmt.Stringer and [json.Marshaler], otherwise additional and unwanted values may be emitted if the logger is configured to use JSON instead of text. This wrapping mechanism allows a value that implements fmt.Stringer, to be guaranteed to be lazily constructed and always output the same content regardless of the output format.

func TypeAttr

func TypeAttr(val any) slog.LogValuer

TypeAttr creates a lazily evaluated log value that presents the pretty type name of a value as a string. It is roughly equivalent to the '%T' format option, and should only perform reflection in the event that logs are actually being generated.

func ValidateFields

func ValidateFields(formatInput []string) (result []string, err error)

ValidateFields ensures the provided fields map to the allowed fields. An error is returned if any of the fields are invalid.

Types

type Config

type Config struct {
	// Output defines where logs go. It can be one of the following: "stderr", "stdout" or
	// a path to a log file
	Output string
	// Severity defines how verbose the log will be. Possible values are "error", "info", "warn"
	Severity string
	// Format defines the output format. Possible values are 'text' and 'json'.
	Format string
	// ExtraFields lists the output fields from KnownFormatFields. Example format: [timestamp, component, caller]
	ExtraFields []string
	// EnableColors dictates if output should be colored.
	EnableColors bool
	// Padding to use for various components.
	Padding int
}

Config configures teleport logging

type DiscardHandler

type DiscardHandler struct{}

DiscardHandler is a slog.Handler that discards all messages. It is more efficient than a slog.Handler which outputs to io.Discard since it performs zero formatting. TODO(tross): Use slog.DiscardHandler once upgraded to Go 1.24.

func (DiscardHandler) Enabled

func (DiscardHandler) Handle

func (DiscardHandler) WithAttrs

func (dh DiscardHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (DiscardHandler) WithGroup

func (dh DiscardHandler) WithGroup(name string) slog.Handler

type FileSharedWriter

type FileSharedWriter struct {
	// contains filtered or unexported fields
}

FileSharedWriter is similar to SharedWriter except that it requires a `os.File` instead of a `io.Writer`. This is to allow the File reopen required by logrotate and similar tools.

func NewFileSharedWriter

func NewFileSharedWriter(logFileName string, flag int, mode fs.FileMode) (*FileSharedWriter, error)

NewFileSharedWriter wraps the provided os.File in a writer that is thread safe, with ability to enable filesystem notification watch and reopen file on specific events.

func (*FileSharedWriter) Close

func (s *FileSharedWriter) Close() error

Close stops the internal watcher and close the log file.

func (*FileSharedWriter) Reopen

func (s *FileSharedWriter) Reopen() error

Reopen closes the file and opens it again using APPEND mode.

func (*FileSharedWriter) RunWatcherReopen

func (s *FileSharedWriter) RunWatcherReopen(ctx context.Context) error

RunWatcherReopen runs a filesystem watcher for rename/remove events to reopen the log.

func (*FileSharedWriter) Write

func (s *FileSharedWriter) Write(b []byte) (int, error)

Write writes len(b) bytes from b to the File.

type SlogJSONHandler

type SlogJSONHandler struct {
	*slog.JSONHandler
}

SlogJSONHandler is a slog.Handler that outputs messages in a json format per the config file.

func NewSlogJSONHandler

func NewSlogJSONHandler(w io.Writer, cfg SlogJSONHandlerConfig) *SlogJSONHandler

NewSlogJSONHandler creates a SlogJSONHandler that outputs to w.

func (*SlogJSONHandler) Handle

func (j *SlogJSONHandler) Handle(ctx context.Context, r slog.Record) error

type SlogJSONHandlerConfig

type SlogJSONHandlerConfig struct {
	// Level is the minimum record level that will be logged.
	Level slog.Leveler
	// ConfiguredFields are fields explicitly set by users to be included in
	// the output message. If there are any entries configured, they will be honored.
	// If empty, the default fields will be populated and included in the output.
	ConfiguredFields []string
	// ReplaceAttr is called to rewrite each non-group attribute before
	// it is logged.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

SlogJSONHandlerConfig allows the SlogJSONHandler functionality to be tweaked.

type SlogTextHandler

type SlogTextHandler struct {
	// contains filtered or unexported fields
}

SlogTextHandler is a slog.Handler that outputs messages in a textual manner as configured by the Teleport configuration.

func NewSlogTextHandler

func NewSlogTextHandler(w io.Writer, cfg SlogTextHandlerConfig) *SlogTextHandler

NewSlogTextHandler creates a SlogTextHandler that writes messages to w.

func (*SlogTextHandler) Enabled

func (s *SlogTextHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled returns whether the provided level will be included in output.

func (*SlogTextHandler) Handle

func (s *SlogTextHandler) Handle(ctx context.Context, r slog.Record) error

Handle formats the provided record and writes the output to the destination.

func (*SlogTextHandler) WithAttrs

func (s *SlogTextHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs clones the current handler with the provided attributes added to any existing attributes. The values are preformatted here so that they do not need to be formatted per call to Handle.

func (*SlogTextHandler) WithGroup

func (s *SlogTextHandler) WithGroup(name string) slog.Handler

WithGroup opens a new group.

type SlogTextHandlerConfig

type SlogTextHandlerConfig struct {
	// Level is the minimum record level that will be logged.
	Level slog.Leveler
	// EnableColors allows the level to be printed in color.
	EnableColors bool
	// Padding to use for various components.
	Padding int
	// ConfiguredFields are fields explicitly set by users to be included in
	// the output message. If there are any entries configured, they will be honored.
	// If empty, the default fields will be populated and included in the output.
	ConfiguredFields []string
	// ReplaceAttr is called to rewrite each non-group attribute before
	// it is logged.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

SlogTextHandlerConfig allow the SlogTextHandler functionality to be tweaked.

type WriterFinalizer

type WriterFinalizer[T io.WriteCloser] struct {
	// contains filtered or unexported fields
}

WriterFinalizer is a wrapper for the io.WriteCloser to automate resource cleanup.

func NewWriterFinalizer

func NewWriterFinalizer[T io.WriteCloser](writer T) *WriterFinalizer[T]

NewWriterFinalizer wraps the provided writer io.WriteCloser to trigger Close function after writer is unassigned from any variable.

func (*WriterFinalizer[T]) Close

func (w *WriterFinalizer[T]) Close() error

Close wraps closing function of internal writer.

func (*WriterFinalizer[T]) Write

func (w *WriterFinalizer[T]) Write(b []byte) (int, error)

Write writes len(b) bytes from b to the writer.

Jump to

Keyboard shortcuts

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