Documentation ¶
Index ¶
- Constants
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Debugln(args ...interface{})
- func EnableHTTPClient(c *http.Client, opts ...ClientLoggerOption)
- 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 Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func Logf(level logrus.Level, format string, args ...interface{})
- func Logln(level logrus.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 SetFormatter(formatter logrus.Formatter)
- func SetGlobal()
- func SetIDKey(key string)
- func SetLevel(level logrus.Level)
- func SetOutput(out io.Writer)
- func StringerValuer(s fmt.Stringer) slog.LogValuer
- func ToContext(ctx context.Context, logger *slog.Logger) context.Context
- func Trace(args ...interface{})
- func Tracef(format string, args ...interface{})
- func Traceln(args ...interface{})
- 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{})
- 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 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 logrus.Level, args ...interface{})
- func (e *Entry) Logf(level logrus.Level, format string, args ...interface{})
- func (e *Entry) Logln(level logrus.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) Trace(args ...interface{})
- func (e *Entry) Tracef(format string, args ...interface{})
- func (e *Entry) Traceln(args ...interface{})
- 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) WithError(err error) *Entry
- func (e *Entry) WithField(key string, value interface{}) *Entry
- func (e *Entry) WithFields(fields logrus.Fields) *Entry
- func (e *Entry) WithTime(t time.Time) *Entry
- type LoggedWriter
- type MiddlewareOption
- func WithDurationFunc(df func(time.Time) time.Duration) MiddlewareOption
- func WithGroup(name string) MiddlewareOption
- func WithIDFunc(nextID func() slog.Attr) MiddlewareOption
- func WithLoggedWriter(wrap func(w http.ResponseWriter) LoggedWriter) MiddlewareOption
- func WithLogger(logger *slog.Logger) MiddlewareOption
- func WithRequestAttr(requestToAttr func(*http.Request) slog.Attr) MiddlewareOption
Constants ¶
const ( FormatterText = "text" FormatterJSON = "json" )
Variables ¶
This section is empty.
Functions ¶
func EnableHTTPClient ¶ added in v0.4.0
func EnableHTTPClient(c *http.Client, opts ...ClientLoggerOption)
EnableHTTPClient adds slog functionality to the HTTP client. It attempts to obtain a logger with FromContext. 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 FromContext ¶ added in v0.4.0
FromContext takes a Logger from the context, if it was previously set by ToContext
func Middleware ¶ added in v0.4.0
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 liftime.
The default logger is slog.Default, with the request's URL and Method as preset attributes. When the request terminates, a INFO line with the Status Code and amount written to the client is printed. This behaviors can be modified with options.
func SetFormatter ¶
func StringerValuer ¶ added in v0.4.0
StringValuer returns a Valuer that forces the logger to use the type's String method, even in json ouput mode. By wrapping the type we defer String being called to the point we actually log.
Types ¶
type ClientLoggerOption ¶ added in v0.4.0
type ClientLoggerOption func(*logRountTripper)
func WithClientDurationFunc ¶ added in v0.4.0
func WithClientDurationFunc(df func(time.Time) time.Duration) ClientLoggerOption
WithClientDurationFunc allows overiding the request duration for testing.
func WithClientGroup ¶ added in v0.4.0
func WithClientGroup(name string) ClientLoggerOption
WithClientGroup groups the log attributes produced by the client.
func WithClientRequestAttr ¶ added in v0.4.0
func WithClientRequestAttr(requestToAttr func(*http.Request) slog.Attr) ClientLoggerOption
WithClientRequestAttr allows customizing the information used from a request as request attributes.
func WithClientResponseAttr ¶ added in v0.4.0
func WithClientResponseAttr(responseToAttr func(*http.Response) slog.Attr) ClientLoggerOption
WithClientResponseAttr allows customizing the information used from a response as response attributes.
func WithFallbackLogger ¶ added in v0.4.0
func WithFallbackLogger(logger *slog.Logger) ClientLoggerOption
WithFallbackLogger uses the passed logger if none was found in the context.
type Config ¶
type Config struct { Level string `json:"level"` Formatter formatter `json:"formatter"` LocalLogger bool `json:"localLogger"` AddSource bool `json:"addSource"` }
func (*Config) Slog ¶ added in v0.5.0
Slog constructs a slog.Logger with the Formatter and Level from config.
func (*Config) UnmarshalJSON ¶
func (*Config) UnmarshalYAML ¶
type Entry ¶
func LogWithFields
deprecated
func WithFields ¶
func WithFields(fields ...interface{}) *Entry
WithFields creates a new entry without an id and the given fields
type LoggedWriter ¶ added in v0.4.0
type LoggedWriter interface { http.ResponseWriter // Attr is called after the next handler // in the Middleware returns and // the complete reponse 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 response. This might be status code, amount of data written or header.
type MiddlewareOption ¶ added in v0.4.0
type MiddlewareOption func(*middleware)
func WithDurationFunc ¶ added in v0.4.0
func WithDurationFunc(df func(time.Time) time.Duration) MiddlewareOption
WithDurationFunc allows overriding the request duration for testing.
func WithGroup ¶ added in v0.4.0
func WithGroup(name string) MiddlewareOption
WithGroup groups the log attributes produced by the middleware.
func WithIDFunc ¶ added in v0.4.0
func WithIDFunc(nextID func() slog.Attr) MiddlewareOption
WithIDFunc enables the creating of request IDs in the middleware, which are then attached to the logger.
func WithLoggedWriter ¶ added in v0.4.0
func WithLoggedWriter(wrap func(w http.ResponseWriter) LoggedWriter) MiddlewareOption
WithLoggedWriter allows customizing the writer from which post-request attributes are taken.
func WithLogger ¶ added in v0.4.0
func WithLogger(logger *slog.Logger) MiddlewareOption
WitLogger sets the passed logger with request attributes into the Request's context.
func WithRequestAttr ¶ added in v0.4.0
func WithRequestAttr(requestToAttr func(*http.Request) slog.Attr) MiddlewareOption
WithRequestAttr allows customizing the information used from a request as request attributes.