logging

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: May 21, 2025 License: MIT Imports: 9 Imported by: 0

README

Logging REST API Interactions

The LogRequest function introduced in logging/logging.go is a shared utility for logging REST API interactions. It is designed to be used in various parts of a service or module to ensure consistent logging practices.

Function Overview

LogRequest takes a LogRequestParams struct as input, which includes the logger instance, start time of the request, the request itself, the response, and any error that occurred. It logs the request method, URL, service, response status, and latency. In case of an error, it logs the error message.

Usage Example

logging.LogRequest(logging.LogRequestParams{
    Logger:    loggerInstance,
    StartTime: time.Now(),
    Request:   httpRequest,
    Response:  httpResponse,
    Error:     err,
})

This function is utilized in policy/policy.go and restlogger/restlogger.go to log interactions with external REST APIs, enhancing the observability of the service.

Integration Guidance

To integrate LogRequest into other services or modules, instantiate a LogRequestParams struct with the appropriate values and call LogRequest. This ensures that all REST API interactions are logged in a standardized format, facilitating debugging and monitoring.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMethodInfo added in v0.0.34

func GetMethodInfo(method string, rawURL string) string

func LogRequest

func LogRequest(params LogRequestParams)

func TrimURL added in v0.0.34

func TrimURL(parsedURL url.URL) string

Types

type LogRequestParams

type LogRequestParams struct {
	Logger    *slog.Logger
	StartTime time.Time
	Request   interface{}
	Response  *http.Response
	Error     error
}

type ResponseWriter added in v0.0.34

type ResponseWriter struct {
	http.ResponseWriter
	StatusCode int
	Buf        *bytes.Buffer
}

ResponseWriter is a custom response writer that buffers the response body and tracks the status code.

func NewResponseWriter added in v0.0.34

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Write added in v0.0.34

func (w *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader added in v0.0.34

func (w *ResponseWriter) WriteHeader(code int)

Jump to

Keyboard shortcuts

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