httplog

package
v1.67.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 11 Imported by: 1

README

Package httplog

Пакет httplog предоставляет middleware для логирования HTTP-запросов и ответов. Поддерживает гибкую настройку: выбор типов контента для логирования тела, управление детализацией и интеграцию с буферизацией данных.

Functions

Log(logger log.Logger, logBody bool) endpoint.LogMiddleware

Создает middleware с базовыми настройками:

  • Логирует тело запроса/ответа, если logBody = true.
  • По умолчанию логирует только для application/json и text/xml.
CombinedLog(logger log.Logger, logBody bool) endpoint.LogMiddleware

Создает middleware с базовыми настройками:

  • Логирует тело запроса/ответа, если logBody = true.
  • По умолчанию логирует только для application/json и text/xml.
  • Собирает запрос и ответ в 1 лог.
LogWithOptions(logger log.Logger, opts ...Option) endpoint.LogMiddleware

Создает middleware с кастомными настройками через опции:

  • WithContentTypes(logBodyContentTypes []string) Option – Задает типы контента, для которых логируется тело (например, application/json).
  • WithLogBody(logBody bool) Option – Включает/отключает логирование тела запроса и ответа.
  • WithLogResponseBody(logResponseBody bool) Option – Включает/отключает логирование тела ответа.
  • WithLogRequestBody(logRequestBody bool) Option – Включает/отключает логирование тела запроса.
  • WithCombinedLog(enable bool) Option – Включает/отключает сборку запроса/ответа в 1 лог.

Usage

Default log middleware
package main

import (
	"log"

	"github.com/txix-open/isp-kit/http/endpoint"
	"github.com/txix-open/isp-kit/http/endpoint/httplog"
	log2 "github.com/txix-open/isp-kit/log"
)

func main() {
	logger, err := log2.New()
	if err != nil {
		log.Fatal(err)
	}

	/* create wrapper with default logging middleware */
	wrapper := endpoint.DefaultWrapper(logger, httplog.Log(logger, true))
}

Customize log middleware
package main

import (
	"log"

	"github.com/txix-open/isp-kit/http/endpoint"
	"github.com/txix-open/isp-kit/http/endpoint/httplog"
	log2 "github.com/txix-open/isp-kit/log"
)

func main() {
	logger, err := log2.New()
	if err != nil {
		log.Fatal(err)
	}

	/* create wrapper with custom logging middleware */
	wrapper := endpoint.DefaultWrapper(logger, httplog.LogWithOptions(
		logger,
		httplog.WithLogRequestBody(true),                         // enable logging request body
		httplog.WithLogResponseBody(false),                       // disable logging response body
		httplog.WithContentTypes([]string{"application/custom"}), // only for `application/custom` content-type
	))
}

Documentation

Overview

Package httplog provides HTTP request/response logging middleware. It supports separate request and response body logging, combined logging, and content-type filtering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombinedLog added in v1.64.4

func CombinedLog(logger log.Logger, logBody bool) endpoint.LogMiddleware

CombinedLog creates a logging middleware that logs requests and responses in a single entry. If logBody is true, it logs request and response bodies for supported content types.

func Log

func Log(logger log.Logger, logBody bool) endpoint.LogMiddleware

Log creates a logging middleware that logs requests and responses separately. If logBody is true, it logs request and response bodies for supported content types.

func LogWithOptions

func LogWithOptions(logger log.Logger, opts ...Option) endpoint.LogMiddleware

LogWithOptions creates a logging middleware with custom configuration options. By default, body logging is disabled. Use options like WithLogBody, WithCombinedLog to customize the behavior.

func Noop

func Noop() endpoint.LogMiddleware

Noop returns a no-operation logging middleware that simply passes through requests. It is useful as a placeholder or for testing when logging is not needed.

Types

type Option

type Option func(cfg *logConfig)

Option is a function that configures the logConfig.

func WithCombinedLog added in v1.64.4

func WithCombinedLog(enable bool) Option

WithCombinedLog enables or disables single combined log entry for request and response. When enabled, the middleware logs request and response in a single log entry instead of two separate entries.

func WithContentTypes

func WithContentTypes(logBodyContentTypes []string) Option

WithContentTypes sets the content types for which request and response bodies will be logged. Content types are matched using prefix comparison (e.g., "application/json" matches "application/json; charset=utf-8").

func WithLogBody

func WithLogBody(logBody bool) Option

WithLogBody is a shortcut to enable both request and response body logging.

func WithLogRequestBody

func WithLogRequestBody(logRequestBody bool) Option

WithLogRequestBody enables or disables request body logging.

func WithLogResponseBody

func WithLogResponseBody(logResponseBody bool) Option

WithLogResponseBody enables or disables response body logging.

Jump to

Keyboard shortcuts

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