slog

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-http/v4/slog

Go package for deriving log/slog loggers decorated with details of an HTTP request.

Functions

  • LoggerWithRequest(req, logger) returns a *slog.Logger with the following keys set: method, user agent, accept, path, remote addr and user ip. If logger is nil the default slog logger is used. If logger was itself returned by LoggerWithRequest (or derived from one with With / WithGroup) the request keys are replaced rather than repeated, and anything added since is kept.
  • ReadUserIP(req) returns the value of the X-Real-Ip header, then the X-Forwarded-For header and finally the request's remote address.

Both headers can be set by the client, so treat user ip as informational unless you are behind a proxy that overwrites them.

Example

package main

import (
	"net/http"

	"github.com/aaronland/go-http/v5/slog"
)

func handler(rsp http.ResponseWriter, req *http.Request) {

	logger := slog.LoggerWithRequest(req, nil)
	logger.Info("Hello")
}

Documentation

Overview

Package slog provides helpers for deriving `log/slog` loggers decorated with details of HTTP requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoggerWithRequest

func LoggerWithRequest(req *http.Request, logger *slog.Logger) *slog.Logger

LoggerWithRequest returns a `slog.Logger` instance with the following keys set: (HTTP) method, user agent, accept, path, remote addr and user ip. If 'logger' is nil `slog.Default()` is used. If 'logger' was itself returned by `LoggerWithRequest` (or derived from such a logger using `With` or `WithGroup`) its request attributes are replaced, not repeated, and anything added since is retained.

func ReadUserIP

func ReadUserIP(req *http.Request) string

ReadUserIP returns the value of the `X-Real-Ip` or `X-Forwarded-For` headers (in that order) or the default remote address reported by 'req'.

Types

This section is empty.

Jump to

Keyboard shortcuts

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