Documentation
¶
Index ¶
- Constants
- Variables
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Debugln(args ...interface{})
- func EnableHTTPClient(c *http.Client, opts ...ClientLoggerOption) error
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(args ...interface{})
- func FromContext(ctx context.Context) (logger *slog.Logger, ok bool)
- func GetLogger() *slog.Logger
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func Logf(level slog.Level, format string, args ...interface{})
- func Logln(level slog.Level, args ...interface{})
- func Middleware(options ...MiddlewareOption) func(http.Handler) http.Handler
- func Panic(args ...interface{})
- func Panicf(format string, args ...interface{})
- func Panicln(args ...interface{})
- func RequestToAttr(req *http.Request) slog.Attr
- func ResponseToAttr(resp *http.Response) slog.Attr
- func SetIDKey(key string)
- func SetLevel(level slog.Level)
- func SetOutput(out io.Writer)
- func StringerValuer(s fmt.Stringer) slog.LogValuer
- func ToContext(ctx context.Context, logger *slog.Logger) context.Context
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- func Warning(args ...interface{})
- func Warningf(format string, args ...interface{})
- func Warningln(args ...interface{})
- func Warnln(args ...interface{})
- func WithAttrs(attrs ...slog.Attr) *slog.Logger
- func WithContext(ctx context.Context) *slog.Logger
- func WithGroup(name string) *slog.Logger
- type ClientLoggerOption
- func WithClientDurationFunc(df func(time.Time) time.Duration) ClientLoggerOption
- func WithClientGroup(name string) ClientLoggerOption
- func WithClientRequestAttr(requestToAttr func(*http.Request) slog.Attr) ClientLoggerOption
- func WithClientResponseAttr(responseToAttr func(*http.Response) slog.Attr) ClientLoggerOption
- func WithFallbackLogger(logger *slog.Logger) ClientLoggerOption
- type Config
- type ConfigOption
- type Entry
- func EntryWithContext(ctx context.Context) *Entry
- func Log(id string) *Entry
- func LogWithFields(id string, fields ...interface{}) *Entry
- func New() *Entry
- func NewWithContext(ctx context.Context) *Entry
- func OnError(err error) *Entry
- func WithError(err error) *Entry
- func WithFields(fields ...interface{}) *Entry
- func (e *Entry) Debug(args ...interface{})
- func (e *Entry) Debugf(format string, args ...interface{})
- func (e *Entry) Debugln(args ...interface{})
- func (e *Entry) Error(args ...interface{})
- func (e *Entry) Errorf(format string, args ...interface{})
- func (e *Entry) Errorln(args ...interface{})
- func (e *Entry) Fatal(args ...interface{})
- func (e *Entry) Fatalf(format string, args ...interface{})
- func (e *Entry) Fatalln(args ...interface{})
- func (e *Entry) Info(args ...interface{})
- func (e *Entry) Infof(format string, args ...interface{})
- func (e *Entry) Infoln(args ...interface{})
- func (e *Entry) Log(level slog.Level, args ...interface{})
- func (e *Entry) Logf(level slog.Level, format string, args ...interface{})
- func (e *Entry) Logln(level slog.Level, args ...interface{})
- func (e *Entry) OnError(err error) *Entry
- func (e *Entry) Panic(args ...interface{})
- func (e *Entry) Panicf(format string, args ...interface{})
- func (e *Entry) Panicln(args ...interface{})
- func (e *Entry) SetFields(fields ...interface{}) *Entry
- func (e *Entry) Warn(args ...interface{})
- func (e *Entry) Warnf(format string, args ...interface{})
- func (e *Entry) Warning(args ...interface{})
- func (e *Entry) Warningf(format string, args ...interface{})
- func (e *Entry) Warningln(args ...interface{})
- func (e *Entry) Warnln(args ...interface{})
- func (e *Entry) WithContext(ctx context.Context) *Entry
- func (e *Entry) WithError(err error) *Entry
- func (e *Entry) WithField(key string, value interface{}) *Entry
- func (e *Entry) WithFields(fields map[string]interface{}) *Entry
- func (e *Entry) WithTime(t time.Time) *Entry
- type Formatter
- type LoggedWriter
- type MiddlewareOption
- func WithDurationFunc(df func(time.Time) time.Duration) MiddlewareOption
- func WithIDFunc(nextID func() slog.Attr) MiddlewareOption
- func WithLoggedWriter(wrap func(w http.ResponseWriter) LoggedWriter) MiddlewareOption
- func WithMiddlewareGroup(name string) MiddlewareOption
- func WithMiddlewareLogger(logger *slog.Logger) MiddlewareOption
- func WithRequestAttr(requestToAttr func(*http.Request) slog.Attr) MiddlewareOption
Constants ¶
const ( FormatterText = "text" FormatterJSON = "json" )
Format constants
const ( OutputStdout = "stdout" OutputStderr = "stderr" OutputFile = "file" )
Default output destinations
Variables ¶
var ErrNilClient = &clientError{"nil client provided"}
ErrNilClient is returned when a nil client is provided to EnableHTTPClient
Functions ¶
func EnableHTTPClient ¶
func EnableHTTPClient(c *http.Client, opts ...ClientLoggerOption) error
EnableHTTPClient adds slog functionality to the HTTP client. It attempts to obtain a logger with WithContext. If no logger is in the context, it tries to use a fallback logger, which might be set by WithFallbackLogger. If no logger was found finally, the Transport is executed without logging.
func Fatal ¶
func Fatal(args ...interface{})
Fatal logs a fatal message and exits with status code 1
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs a formatted fatal message and exits with status code 1
func Fatalln ¶
func Fatalln(args ...interface{})
Fatalln logs a fatal message with a newline and exits with status code 1
func FromContext ¶
FromContext takes a Logger from the context, if it was previously set by ToContext
func Middleware ¶
func Middleware(options ...MiddlewareOption) func(http.Handler) http.Handler
Middleware enables request logging and sets a logger to the request context. Use FromContext to obtain the logger anywhere in the request lifetime.
The default logger is slog.Default(), with the request's URL and Method as preset attributes. When the request terminates, an INFO line with the Status Code and amount written to the client is printed. This behavior can be modified with options.
func Panicf ¶
func Panicf(format string, args ...interface{})
Panicf logs a formatted panic message and panics
func Panicln ¶
func Panicln(args ...interface{})
Panicln logs a panic message with a newline and panics
func RequestToAttr ¶
RequestToAttr converts an HTTP request to a structured slog attribute containing method and URL information
func ResponseToAttr ¶
ResponseToAttr converts an HTTP response to a structured slog attribute containing status and content length information
func SetLevel ¶
SetLevel configures the minimum log level for the default logger while preserving the existing output writer
func SetOutput ¶
SetOutput configures the output destination for the default logger while preserving the existing log level
func StringerValuer ¶
StringerValuer returns a Valuer that forces the logger to use the type's String method, even in json output mode. By wrapping the type we defer String being called to the point we actually log.
func ToContext ¶
ToContext sets a Logger to the context. If ctx is nil, a new background context is created. If logger is nil, the default logger is used.
func Warning ¶
func Warning(args ...interface{})
Warning is an alias for Warn (maintained for backward compatibility)
func Warningf ¶
func Warningf(format string, args ...interface{})
Warningf is an alias for Warnf (maintained for backward compatibility)
func Warningln ¶
func Warningln(args ...interface{})
Warningln is an alias for Warnln (maintained for backward compatibility)
func WithContext ¶
WithContext returns a logger from the context if present, otherwise returns the default logger
Types ¶
type ClientLoggerOption ¶
type ClientLoggerOption func(*logRoundTripper)
ClientLoggerOption is a function that configures a logRoundTripper
func WithClientDurationFunc ¶
func WithClientDurationFunc(df func(time.Time) time.Duration) ClientLoggerOption
WithClientDurationFunc allows overriding the request duration for testing.
func WithClientGroup ¶
func WithClientGroup(name string) ClientLoggerOption
WithClientGroup groups the log attributes produced by the client.
func WithClientRequestAttr ¶
func WithClientRequestAttr(requestToAttr func(*http.Request) slog.Attr) ClientLoggerOption
WithClientRequestAttr allows customizing the information used from a request as request attributes.
func WithClientResponseAttr ¶
func WithClientResponseAttr(responseToAttr func(*http.Response) slog.Attr) ClientLoggerOption
WithClientResponseAttr allows customizing the information used from a response as response attributes.
func WithFallbackLogger ¶
func WithFallbackLogger(logger *slog.Logger) ClientLoggerOption
WithFallbackLogger uses the passed logger if none was found in the context.
type Config ¶
type Config struct {
// Level sets the minimum log level (debug, info, warn, error)
Level string `json:"level" yaml:"level"`
// Formatter configures the log output format and options
Formatter Formatter `json:"formatter" yaml:"formatter"`
// AddSource adds source code information to log entries
AddSource bool `json:"addSource" yaml:"addSource"`
// Output specifies where logs should be written (default: stderr)
Output string `json:"output" yaml:"output"`
}
Config defines the configuration for the logging system
func NewConfig ¶
func NewConfig(opts ...ConfigOption) *Config
NewConfig creates a new Config with default values
func (*Config) CreateLogger ¶
CreateLogger creates a new logger based on the configuration
func (*Config) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
func (*Config) UnmarshalYAML ¶
UnmarshalYAML implements the yaml.Unmarshaler interface
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a function that modifies a Config
func WithAddSource ¶
func WithAddSource(enable bool) ConfigOption
WithAddSource enables source code information in logs
func WithFormatterData ¶
func WithFormatterData(key string, value interface{}) ConfigOption
WithFormatterData adds custom data to the formatter
func WithJSONFormatter ¶
func WithJSONFormatter() ConfigOption
WithJSONFormatter sets the formatter to JSON
func WithOutput ¶
func WithOutput(output string) ConfigOption
WithOutput sets the output destination
func WithTextFormatter ¶
func WithTextFormatter() ConfigOption
WithTextFormatter sets the formatter to text
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry represents a log entry with additional functionality for conditional logging and field management
func EntryWithContext ¶
EntryWithContext creates a new entry with the given context
func LogWithFields ¶
LogWithFields creates a new entry with an ID and the given fields (maintained for backward compatibility)
func NewWithContext ¶
NewWithContext instantiates a new entry with the default logger and context
func WithFields ¶
func WithFields(fields ...interface{}) *Entry
WithFields creates a new entry with the given fields
func (*Entry) Debugln ¶
func (e *Entry) Debugln(args ...interface{})
Debugln logs a debug message with a newline
func (*Entry) Errorln ¶
func (e *Entry) Errorln(args ...interface{})
Errorln logs an error message with a newline
func (*Entry) Fatal ¶
func (e *Entry) Fatal(args ...interface{})
Fatal logs a fatal message and exits with status code 1
func (*Entry) Fatalln ¶
func (e *Entry) Fatalln(args ...interface{})
Fatalln logs a fatal message with a newline and exits with status code 1
func (*Entry) Infoln ¶
func (e *Entry) Infoln(args ...interface{})
Infoln logs an info message with a newline
func (*Entry) Panic ¶
func (e *Entry) Panic(args ...interface{})
Panic logs a panic message and panics
func (*Entry) Panicln ¶
func (e *Entry) Panicln(args ...interface{})
Panicln logs a panic message with a newline and panics
func (*Entry) SetFields ¶
SetFields sets the given fields on the entry It panics if the length of fields is odd
func (*Entry) Warning ¶
func (e *Entry) Warning(args ...interface{})
Warning is an alias for Warn (maintained for backward compatibility)
func (*Entry) Warningln ¶
func (e *Entry) Warningln(args ...interface{})
Warningln is an alias for Warnln (maintained for backward compatibility)
func (*Entry) Warnln ¶
func (e *Entry) Warnln(args ...interface{})
Warnln logs a warning message with a newline
func (*Entry) WithContext ¶
WithContext sets the context for the entry
func (*Entry) WithFields ¶
WithFields adds multiple fields to the entry
type Formatter ¶
type Formatter struct {
// Format specifies the output format (json or text)
Format string `json:"format" yaml:"format"`
// Data contains additional formatter-specific options
Data map[string]interface{} `json:"data" yaml:"data"`
}
Formatter defines the log format configuration
type LoggedWriter ¶
type LoggedWriter interface {
http.ResponseWriter
// Attr is called after the next handler in the Middleware returns and
// the complete response should have been written.
//
// The returned Attribute should be a [slog.Group]
// containing response Attributes.
Attr() slog.Attr
// Err is called by the middleware to check if the underlying writer
// returned an error. If so, the middleware will print an ERROR line.
Err() error
}
LoggedWriter stores information regarding the HTTP response. This might be status code, amount of data written or headers.
type MiddlewareOption ¶
type MiddlewareOption func(*middleware)
MiddlewareOption is a function that configures a middleware instance
func WithDurationFunc ¶
func WithDurationFunc(df func(time.Time) time.Duration) MiddlewareOption
WithDurationFunc allows overriding the request duration for testing.
func WithIDFunc ¶
func WithIDFunc(nextID func() slog.Attr) MiddlewareOption
WithIDFunc enables the creation of request IDs in the middleware, which are then attached to the logger.
func WithLoggedWriter ¶
func WithLoggedWriter(wrap func(w http.ResponseWriter) LoggedWriter) MiddlewareOption
WithLoggedWriter allows customizing the writer from which post-request attributes are taken.
func WithMiddlewareGroup ¶
func WithMiddlewareGroup(name string) MiddlewareOption
WithMiddlewareGroup groups the log attributes produced by the middleware.
func WithMiddlewareLogger ¶
func WithMiddlewareLogger(logger *slog.Logger) MiddlewareOption
WithLogger sets the passed logger with request attributes into the Request's context.
func WithRequestAttr ¶
func WithRequestAttr(requestToAttr func(*http.Request) slog.Attr) MiddlewareOption
WithRequestAttr allows customizing the information used from a request as request attributes.