mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
add a unified logging handler to the http client
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user