Documentation
¶
Index ¶
- Variables
- func AddContextAttributes(ctx context.Context, attrs ...slog.Attr)
- func AddCustomAttributes(r *http.Request, attrs ...slog.Attr)
- func GetRequestID(r *http.Request) string
- func GetRequestIDFromContext(ctx context.Context) string
- func New(logger *slog.Logger) func(http.Handler) http.Handler
- func NewWithConfig(logger *slog.Logger, config Config) func(http.Handler) http.Handler
- func NewWithFilters(logger *slog.Logger, filters ...Filter) func(http.Handler) http.Handler
- func Recovery(next http.Handler) http.Handler
- type Config
- type Filter
- func Accept(filter Filter) Filter
- func AcceptHost(hosts ...string) Filter
- func AcceptHostContains(parts ...string) Filter
- func AcceptHostMatch(regs ...regexp.Regexp) Filter
- func AcceptHostPrefix(prefixs ...string) Filter
- func AcceptHostSuffix(prefixs ...string) Filter
- func AcceptMethod(methods ...string) Filter
- func AcceptPath(urls ...string) Filter
- func AcceptPathContains(parts ...string) Filter
- func AcceptPathMatch(regs ...regexp.Regexp) Filter
- func AcceptPathPrefix(prefixs ...string) Filter
- func AcceptPathSuffix(prefixs ...string) Filter
- func AcceptStatus(statuses ...int) Filter
- func AcceptStatusGreaterThan(status int) Filter
- func AcceptStatusGreaterThanOrEqual(status int) Filter
- func AcceptStatusLessThan(status int) Filter
- func AcceptStatusLessThanOrEqual(status int) Filter
- func Ignore(filter Filter) Filter
- func IgnoreHost(hosts ...string) Filter
- func IgnoreHostContains(parts ...string) Filter
- func IgnoreHostMatch(regs ...regexp.Regexp) Filter
- func IgnoreHostPrefix(prefixs ...string) Filter
- func IgnoreHostSuffix(suffixs ...string) Filter
- func IgnoreMethod(methods ...string) Filter
- func IgnorePath(urls ...string) Filter
- func IgnorePathContains(parts ...string) Filter
- func IgnorePathMatch(regs ...regexp.Regexp) Filter
- func IgnorePathPrefix(prefixs ...string) Filter
- func IgnorePathSuffix(suffixs ...string) Filter
- func IgnoreStatus(statuses ...int) Filter
- func IgnoreStatusGreaterThan(status int) Filter
- func IgnoreStatusGreaterThanOrEqual(status int) Filter
- func IgnoreStatusLessThan(status int) Filter
- func IgnoreStatusLessThanOrEqual(status int) Filter
- type WrapResponseWriter
Constants ¶
This section is empty.
Variables ¶
var ( TraceIDKey = "trace_id" SpanIDKey = "span_id" RequestIDKey = "id" RequestBodyMaxSize = 64 * 1024 // 64KB ResponseBodyMaxSize = 64 * 1024 // 64KB HiddenRequestHeaders = map[string]struct{}{ "authorization": {}, "cookie": {}, "set-cookie": {}, "x-auth-token": {}, "x-csrf-token": {}, "x-xsrf-token": {}, } HiddenResponseHeaders = map[string]struct{}{ "set-cookie": {}, } // Formatted with http.CanonicalHeaderKey RequestIDHeaderKey = "X-Request-Id" )
Functions ¶
func AddContextAttributes ¶ added in v1.7.0
AddContextAttributes is the same as AddCustomAttributes, but it doesn't need access to the request struct.
func AddCustomAttributes ¶
AddCustomAttributes adds custom attributes to the request context. This func can be called from any handler or middleware, as long as the slog-http middleware is already mounted.
func GetRequestID ¶ added in v1.2.0
GetRequestID returns the request identifier.
func GetRequestIDFromContext ¶ added in v1.4.0
GetRequestIDFromContext returns the request identifier from the context.
func New ¶
New returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().
func NewWithConfig ¶
NewWithConfig returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
func NewWithFilters ¶
NewWithFilters returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().
Types ¶
type Config ¶
type Config struct {
DefaultLevel slog.Level
ClientErrorLevel slog.Level
ServerErrorLevel slog.Level
WithUserAgent bool
WithRequestID bool
WithRequestBody bool
WithRequestHeader bool
WithResponseBody bool
WithResponseHeader bool
WithSpanID bool
WithTraceID bool
WithClientIP bool
WithCustomMessage func(w http.ResponseWriter, r *http.Request) string
Filters []Filter
}
func DefaultConfig ¶ added in v1.10.0
func DefaultConfig() Config
DefaultConfig returns the default configuration for the request logger.
type Filter ¶
type Filter func(w WrapResponseWriter, r *http.Request) bool