ThisIsAreku / slog-http

🚨 net/http middleware for slog logger

Home Page:https://pkg.go.dev/github.com/samber/slog-http

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

slog: net/http middleware

tag Go Version GoDoc Build Status Go report Coverage Contributors License

net/http handler to log HTTP requests using slog.

See also:

HTTP middlewares:

Loggers:

Log sinks:

πŸš€ Install

go get github.com/samber/slog-http

Compatibility: go >= 1.21

No breaking changes will be made to exported APIs before v2.0.0.

πŸ’‘ Usage

Handler options

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

	Filters []Filter
}

Attributes will be injected in log payload.

Other global parameters:

sloghttp.TraceIDKey = "trace-id"
sloghttp.SpanIDKey = "span-id"
sloghttp.RequestBodyMaxSize  = 64 * 1024 // 64KB
sloghttp.ResponseBodyMaxSize = 64 * 1024 // 64KB
sloghttp.HiddenRequestHeaders = map[string]struct{}{ ... }
sloghttp.HiddenResponseHeaders = map[string]struct{}{ ... }
sloghttp.RequestIDHeaderKey = "X-Request-Id"

Minimal

import (
	"net/http"
	"os"
	"time"

	sloghttp "github.com/samber/slog-http"
	"log/slog"
)

// Create a slog logger, which:
//   - Logs to stdout.
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// mux router
mux := http.NewServeMux()

// Routes
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}))
mux.Handle("/error", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w,  "I'm angry" http.StatusInternalServerError)
}))

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.New(logger)(handler)

// Start server
http.ListenAndServe(":4242", handler)

// output:
// time=2023-10-15T20:32:58.926+02:00 level=INFO msg="Success" env=production request.time=2023-10-15T20:32:58.626+02:00 request.method=GET request.path=/ request.route="" request.ip=127.0.0.1:63932 request.length=0 response.time=2023-10-15T20:32:58.926+02:00 response.latency=100ms response.status=200 response.length=7 id=229c7fc8-64f5-4467-bc4a-940700503b0d

OTEL

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

config := sloghttp.Config{
	WithSpanID:  true,
	WithTraceID: true,
}

mux := http.NewServeMux()

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.NewWithConfig(logger, config)(handler)

// Start server
http.ListenAndServe(":4242", handler)

Custom log levels

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

config := sloghttp.Config{
	DefaultLevel:     slog.LevelInfo,
	ClientErrorLevel: slog.LevelWarn,
	ServerErrorLevel: slog.LevelError,
}

mux := http.NewServeMux()

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.NewWithConfig(logger, config)(handler)

// Start server
http.ListenAndServe(":4242", handler)

Verbose

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

config := sloghttp.Config{
	WithRequestBody: true,
	WithResponseBody: true,
	WithRequestHeader: true,
	WithResponseHeader: true,
}

mux := http.NewServeMux()

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.NewWithConfig(logger, config)(handler)

// Start server
http.ListenAndServe(":4242", handler)

Filters

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

mux := http.NewServeMux()

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.NewWithFilters(
	logger,
	sloghttp.Accept(func (w sloghttp.WrapResponseWriter, r *http.Request) bool {
		return xxx
	}),
	sloghttp.IgnoreStatus(401, 404),
)(handler)

// Start server
http.ListenAndServe(":4242", handler)

Available filters:

  • Accept / Ignore
  • AcceptMethod / IgnoreMethod
  • AcceptStatus / IgnoreStatus
  • AcceptStatusGreaterThan / IgnoreStatusLessThan
  • AcceptStatusGreaterThanOrEqual / IgnoreStatusLessThanOrEqual
  • AcceptPath / IgnorePath
  • AcceptPathContains / IgnorePathContains
  • AcceptPathPrefix / IgnorePathPrefix
  • AcceptPathSuffix / IgnorePathSuffix
  • AcceptPathMatch / IgnorePathMatch
  • AcceptHost / IgnoreHost
  • AcceptHostContains / IgnoreHostContains
  • AcceptHostPrefix / IgnoreHostPrefix
  • AcceptHostSuffix / IgnoreHostSuffix
  • AcceptHostMatch / IgnoreHostMatch

Using custom time formatters

import (
	"net/http"
	"log/slog"

	sloghttp "github.com/samber/slog-http"
	slogformatter "github.com/samber/slog-formatter"
)

// Create a slog logger, which:
//   - Logs to stdout.
//   - RFC3339 with UTC time format.
logger := slog.New(
	slogformatter.NewFormatterHandler(
		slogformatter.TimezoneConverter(time.UTC),
		slogformatter.TimeFormatter(time.DateTime, nil),
	)(
		slog.NewTextHandler(os.Stdout, nil),
	),
)

// mux router
mux := http.NewServeMux()

// Routes
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}))
mux.Handle("/error", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "I'm angry" http.StatusInternalServerError)
}))

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.New(logger)(handler)

// Start server
http.ListenAndServe(":4242", handler)

// output:
// time=2023-10-15T20:32:58.926+02:00 level=INFO msg="Success" env=production request.time=2023-10-15T20:32:58Z request.method=GET request.path=/ request.route="" request.ip=127.0.0.1:63932 request.length=0 response.time=2023-10-15T20:32:58Z response.latency=100ms response.status=200 response.length=7 id=229c7fc8-64f5-4467-bc4a-940700503b0d

Using custom logger sub-group

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// mux router
mux := http.NewServeMux()

// Routes
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}))
mux.Handle("/error", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w,  "I'm angry" http.StatusInternalServerError)
}))

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.New(logger.WithGroup("http"))(handler)

// Start server
http.ListenAndServe(":4242", handler)

// output:
// time=2023-10-15T20:32:58.926+02:00 level=INFO msg="Success" env=production http.request.time=2023-10-15T20:32:58.626+02:00 http.request.method=GET http.request.path=/ http.request.route="" http.request.ip=127.0.0.1:63932 http.request.length=0 http.response.time=2023-10-15T20:32:58.926+02:00 http.response.latency=100ms http.response.status=200 http.response.length=7 http.id=229c7fc8-64f5-4467-bc4a-940700503b0d

Add logger to a single route

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// mux router
mux := http.NewServeMux()

// Routes
mux.Handler("/", sloghttp.New(logger)(
	http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			return c.String(http.StatusOK, "Hello, World!")
		},
	),
	sloghttp.New(logger),
))

// Start server
http.ListenAndServe(":4242", handler)

Adding custom attributes

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// Add an attribute to all log entries made through this logger.
logger = logger.With("env", "production")

// mux router
mux := http.NewServeMux()

// Routes
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	sloghttp.AddCustomAttributes(r, slog.String("foo", "bar"))
	w.Write([]byte("Hello, World!"))
}))

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.New(logger)(handler)

// Start server
http.ListenAndServe(":4242", handler)

// output:
// time=2023-10-15T20:32:58.926+02:00 level=INFO msg="Success" env=production request.time=2023-10-15T20:32:58.626+02:00 request.method=GET request.path=/ request.route="" request.ip=127.0.0.1:63932 request.length=0 response.time=2023-10-15T20:32:58.926+02:00 response.latency=100ms response.status=200 response.length=7 id=229c7fc8-64f5-4467-bc4a-940700503b0d foo=bar

JSON output

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

// mux router
mux := http.NewServeMux()

// Routes
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}))

// Middleware
handler := sloghttp.Recovery(mux)
handler = sloghttp.New(logger)(handler)

// Start server
http.ListenAndServe(":4242", handler)

// output:
// {"time":"2023-10-15T20:32:58.926+02:00","level":"INFO","msg":"Success","env":"production","http":{"request":{"time":"2023-10-15T20:32:58.626+02:00","method":"GET","path":"/","route":"","ip":"127.0.0.1:55296","length":0},"response":{"time":"2023-10-15T20:32:58.926+02:00","latency":100000,"status":200,"length":7},"id":"04201917-d7ba-4b20-a3bb-2fffba5f2bd9"}}

🀝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

πŸ‘€ Contributors

Contributors

πŸ’« Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

πŸ“ License

Copyright Β© 2023 Samuel Berthe.

This project is MIT licensed.

About

🚨 net/http middleware for slog logger

https://pkg.go.dev/github.com/samber/slog-http

License:MIT License


Languages

Language:Go 94.9%Language:Makefile 5.1%