[go: up one dir, main page]

Extends the access logger to accommodate for slog loggers

This MR adds the ability to configure the accesslogger with an slog logger.

Below, you'll find a comparison between the two different setups based on the output from the tests:

LOGRUS LOG LINE
time="2025-11-06T10:03:31Z" level=info msg=access content_type= correlation_id= duration_ms=32 host="127.0.0.1:58658" method=GET proto=HTTP/1.1 read_bytes=96 referrer= remote_addr="127.0.0.1:58659" remote_ip=127.0.0.1 status=500 system=http ttfb_ms=11 uri=/ user_agent=Go-http-client/1.1 written_bytes=2

SLOG LOG LINE
time=2025-11-06T10:04:20.332Z level=INFO msg=access ttfb_ms=11 correlation_id="" host=127.0.0.1:58743 remote_ip=127.0.0.1 remote_addr=127.0.0.1:58744 method=GET uri=/ proto=HTTP/1.1 status=500 written_bytes=2 read_bytes=96 content_type="" duration_ms=32 user_agent=Go-http-client/1.1 system=http referrer=""

Usage

In order to use the slog logger, you can use the log.WithSlogAccessLogger(slog) -

slogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	http.ListenAndServe(":8080",
		log.AccessLogger(handler,
			log.WithSlogAccessLogger(slogger),
			log.WithExtraFields(extraFieldGenerator),                          // Include custom fields into the logs
			log.WithFieldsExcluded(log.HTTPRequestReferrer|log.HTTPUserAgent), // Exclude user-agent and referrer fields from the logs
		),
	)
Edited by Elliot Forbes

Merge request reports

Loading