logging

package
v0.0.42 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 24 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 LoggerFromContext

func LoggerFromContext(ctx context.Context) *slog.Logger

LoggerFromContext returns the logger from the context. It panics if the given context doesn't contain a logger.

func LoggerIntoContext

func LoggerIntoContext(ctx context.Context, logger *slog.Logger) context.Context

LoggerIntoContext creates a new context that contains the given logger.

Types

type Interceptor

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

Interceptor contains the data needed by the Interceptor, like the logger and settings.

func (*Interceptor) StreamClient

func (i *Interceptor) StreamClient(ctx context.Context, desc *grpc.StreamDesc, conn *grpc.ClientConn, method string,
	streamer grpc.Streamer, opts ...grpc.CallOption) (stream grpc.ClientStream, err error)

StreamClient is the stream client interceptor function.

func (*Interceptor) StreamServer

func (i *Interceptor) StreamServer(server any, stream grpc.ServerStream, info *grpc.StreamServerInfo,
	handler grpc.StreamHandler) error

StreamServer is the stream server interceptor function.

func (*Interceptor) UnaryClient

func (i *Interceptor) UnaryClient(ctx context.Context, method string, request, response any,
	conn *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error

UnaryClient is the unary client interceptor function.

func (*Interceptor) UnaryServer

func (i *Interceptor) UnaryServer(ctx context.Context, request any, info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler) (response any, err error)

UnaryServer is the unary server interceptor function.

type InterceptorBuilder

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

InterceptorBuilder contains the data and logic needed to build an interceptor that writes to the log the details of calls. Don't create instances of this type directly, use the NewInterceptor function instead.

func NewInterceptor

func NewInterceptor() *InterceptorBuilder

NewInterceptor creates a builder that can then be used to configure and create a logging interceptor.

func (*InterceptorBuilder) Build

func (b *InterceptorBuilder) Build() (result *Interceptor, err error)

Build uses the data stored in the builder to create and configure a new interceptor.

func (*InterceptorBuilder) SetBodies

func (b *InterceptorBuilder) SetBodies(value bool) *InterceptorBuilder

SetBodies indicates if details about the request and response bodies should be included in log messages. The default is to not include them.

func (*InterceptorBuilder) SetFlags

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

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

func (*InterceptorBuilder) SetHeaders

func (b *InterceptorBuilder) SetHeaders(value bool) *InterceptorBuilder

SetHeaders indicates if headers should be included in log messages. The default is to not include them.

func (*InterceptorBuilder) SetLogger

func (b *InterceptorBuilder) SetLogger(value *slog.Logger) *InterceptorBuilder

SetLogger sets the logger that will be used to write to the log. This is mandatory.

func (*InterceptorBuilder) SetRedact

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

SetRedact indicates if security sensitive information should be redacted. The default is true.

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 Writer

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

Writer is an implementation of an io.Writer that writes messages to a slog.Logger. Don't create instances of this type directly, use the NewWriterBuilder function instead.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

type WriterBuilder

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

WriterBuilder contains the data and logic needed to create a writer that writes messages to a logger. Don't create instances of this type directly, use the NewWriterBuilder function instead.

func NewWriter

func NewWriter() *WriterBuilder

NewWriter creates a builder that can then be used to configure and create a new logging writer.

func (*WriterBuilder) Build

func (b *WriterBuilder) Build() (result *Writer, err error)

Build uses the configuration stored in the builder to create a new logging writer.

func (*WriterBuilder) SetContext

func (b *WriterBuilder) SetContext(value context.Context) *WriterBuilder

SetContext sets the context that will be passed to the logger from the Write method. This is optional, if not set no context will be passed to the logger.

func (*WriterBuilder) SetLevel

func (b *WriterBuilder) SetLevel(value slog.Level) *WriterBuilder

SetLevel sets the level. This is optional, if not set the 'info' level will be used.

func (*WriterBuilder) SetLogger

func (b *WriterBuilder) SetLogger(value *slog.Logger) *WriterBuilder

SetLogger sets the logger. This is mandatory.

Jump to

Keyboard shortcuts

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