add a unified logging handler to the http client

This commit is contained in:
MaysWind
2026-01-15 23:29:48 +08:00
parent 853b0d430e
commit 89fb8a099e
18 changed files with 130 additions and 48 deletions
+35
View File
@@ -0,0 +1,35 @@
package httpclient
import (
"github.com/mayswind/ezbookkeeping/pkg/core"
)
const (
logHandleKey = "log_handler"
)
// HttpResponseLogHandlerFunc represents the http response log handler function
type HttpResponseLogHandlerFunc func([]byte)
// httpRequestContext represents the context for http request
type httpRequestContext struct {
core.Context
logHandler HttpResponseLogHandlerFunc
}
// Value returns the value associated with key
func (c *httpRequestContext) Value(key any) any {
if key == logHandleKey {
return c.logHandler
}
return c.Context.Value(key)
}
// CustomHttpResponseLog returns a context with http response log handler
func CustomHttpResponseLog(c core.Context, responseLogHandler HttpResponseLogHandlerFunc) core.Context {
return &httpRequestContext{
Context: c,
logHandler: responseLogHandler,
}
}