logging

package
v0.0.0-...-e6ff806 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFlags

func AddFlags(set *pflag.FlagSet)

AddFlags adds the flags related to logging to the given flag set.

func AppendCtx

func AppendCtx(ctx context.Context, attr slog.Attr) context.Context

AppendCtx adds an slog attribute to the provided context so that it will be included in any Record created with such context

Types

type LoggerBuilder

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

LoggerBuilder contains the data and logic needed to create a logger. Don't create instances of this directly, use the NewLogger function instead.

func NewLogger

func NewLogger() *LoggerBuilder

NewLogger creates a builder that can then be used to configure and create a logger.

func (*LoggerBuilder) AddField

func (b *LoggerBuilder) AddField(name string, value any) *LoggerBuilder

AddField adds a field that will be added to all the log messages. The following field values have special meanings:

- %p: Is replaced by the process identifier.

Any other field value is added without change.

func (*LoggerBuilder) AddFields

func (b *LoggerBuilder) AddFields(values map[string]any) *LoggerBuilder

AddFields adds a set of fields that will be added to all the log messages. See the AddField method for the meanings of values.

func (*LoggerBuilder) Build

func (b *LoggerBuilder) Build() (result *slog.Logger, err error)

Build uses the data stored in the buider to create a new logger.

func (*LoggerBuilder) SetErr

func (b *LoggerBuilder) SetErr(value io.Writer) *LoggerBuilder

SetErr sets the standard error output stream. This is optional and will only be used when the log file is 'stderr'.

func (*LoggerBuilder) SetFields

func (b *LoggerBuilder) SetFields(values map[string]any) *LoggerBuilder

SetFields sets the fields tht will be added to all the log messages. See the AddField method for the meanings of values. Note that this replaces any previously configured fields. If you want to preserve them use the AddFields method.

func (*LoggerBuilder) SetFile

func (b *LoggerBuilder) SetFile(value string) *LoggerBuilder

SetFile sets the file that the logger will write to. This is optional, and if not specified the the logger will write to the standard output stream of the process.

func (*LoggerBuilder) SetFlags

func (b *LoggerBuilder) SetFlags(flags *pflag.FlagSet) *LoggerBuilder

SetFlags sets the command line flags that should be used to configure the logger. This is optional.

func (*LoggerBuilder) SetLevel

func (b *LoggerBuilder) SetLevel(value string) *LoggerBuilder

SetLevel sets the log level.

func (*LoggerBuilder) SetOut

func (b *LoggerBuilder) SetOut(value io.Writer) *LoggerBuilder

SetOut sets the standard output stream. This is optional and will only be used then the log file is 'stdout'.

func (*LoggerBuilder) SetRedact

func (b *LoggerBuilder) SetRedact(value bool) *LoggerBuilder

Set redact sets the flag that indicates if security sensitive data should be removed from the log. These fields are indicated by adding an exlamation mark in front of the field name. For example, to write a message with a `public` field that isn't sensitive and another `private` field that is:

logger.Info(
	"SSH keys",
	"public", publicKey,
	"!public", privateKey,
)

When redacting is enabled the value of the sensitive field will be replaced be `***`, so in the example above the resulting message will be like this:

{
	"msg": "SSHKeys",
	"public": "ssh-rsa AAA...",
	"private": "***"
}

The exclamation mark will be always removed from the field name.

func (*LoggerBuilder) SetWriter

func (b *LoggerBuilder) SetWriter(value io.Writer) *LoggerBuilder

SetWriter sets the writer that the logger will write to. This is optional, and if not specified the the logger will write to the standard output stream of the process.

type LoggingContextHandler

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

func NewLoggingContextHandler

func NewLoggingContextHandler(level slog.Level) *LoggingContextHandler

func (LoggingContextHandler) Enabled

func (h LoggingContextHandler) Enabled(ctx context.Context, level slog.Level) bool

func (LoggingContextHandler) Handle

func (h LoggingContextHandler) Handle(ctx context.Context, record slog.Record) error

Handle adds attributes from the context to the log record

func (LoggingContextHandler) WithAttrs

func (h LoggingContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (LoggingContextHandler) WithGroup

func (h LoggingContextHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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