accesslog

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

README

Access Logging Middleware

The access logging middleware logs each request with the following information:

  • correlationID
  • client address
  • HTTP method
  • path

Example

The access logging will use the default logger slog.Default() and INFO log level.

finalHandler := midgard.StackMiddlewareHandler(
    []midgard.Middleware{
        accesslog.New(),
    },
    http.HandlerFunc(HelloHandler),
)

It can also be configured with a custom logger and level.

finalHandler := midgard.StackMiddlewareHandler(
    []midgard.Middleware{
        accesslog.New(
            accesslog.WithLogger(someOtherLogger),
            accesslog.WithLogLevel(slog.LevelDebug),
        ),
    },
    http.HandlerFunc(HelloHandler),
)

Documentation

Overview

Package accesslog provides a middleware that logs every request.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilOption = errors.New("option cannot be nil")

ErrNilOption is returned when an option is nil.

Functions

func New

func New(options ...func(*Handler) error) (defs.Middleware, error)

New generates a new access logging middleware.

func WithLogLevel

func WithLogLevel(level slog.Level) func(h *Handler) error

WithLogLevel configures the log level to use with the logger.

func WithLogger

func WithLogger(log *slog.Logger) func(h *Handler) error

WithLogger configures the logger to use.

Types

type Handler

type Handler struct {
	defs.MWBase
}

Handler holds the information necessary for the log.

func (*Handler) GetMWBase

func (h *Handler) GetMWBase() *defs.MWBase

GetMWBase returns the MWBase instance of the handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the access logging middleware. It logs every request with its correlationID, the client's address, http method and accessed path.

Jump to

Keyboard shortcuts

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