Skip to content

Commit

Permalink
[skip ci] WIP logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Dec 20, 2024
1 parent 6b6cef9 commit b796b3a
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ type RequestResponseLogger struct {
response http.HandlerFunc
}

func NewRequestResponseLogger(req, resp http.HandlerFunc) *RequestResponseLogger {
if req == nil {
req = func(w http.ResponseWriter, r *http.Request) {}
}
if resp == nil {
resp = func(w http.ResponseWriter, r *http.Request) {}
}
return &RequestResponseLogger{req, resp}
}

type Server struct {
// The underlying HTTP server
*http.Server
Expand Down Expand Up @@ -122,7 +112,10 @@ func NewServer(options ...func(*Server)) *Server {

Security: NewSecurity(),

requestResponseLogger: NewRequestResponseLogger(RequestLog, ResponseLog),
requestResponseLogger: &RequestResponseLogger{
request: RequestLog,
response: ResponseLog,
},
}

// Default options that can be overridden
Expand Down Expand Up @@ -163,7 +156,13 @@ func NewServer(options ...func(*Server)) *Server {
)
}

if s.requestResponseLogger != nil {
if s.requestResponseLogger.request != nil && s.requestResponseLogger.response != nil {
if s.requestResponseLogger.request == nil {
s.requestResponseLogger.request = func(w http.ResponseWriter, r *http.Request) {}
}
if s.requestResponseLogger.response == nil {
s.requestResponseLogger.response = func(w http.ResponseWriter, r *http.Request) {}
}
s.middlewares = append(s.middlewares, s.requestResponseLogger.Log)
}

Expand Down Expand Up @@ -349,9 +348,15 @@ func WithLogHandler(handler slog.Handler) func(*Server) {
}
}

func WithRequestResponseLog(logger *RequestResponseLogger) func(*Server) {
func WithResponseLog(res http.HandlerFunc) func(*Server) {
return func(c *Server) {
c.requestResponseLogger.response = res
}
}

func WithRequestLog(req http.HandlerFunc) func(*Server) {
return func(c *Server) {
c.requestResponseLogger = logger
c.requestResponseLogger.request = req
}
}

Expand Down

0 comments on commit b796b3a

Please sign in to comment.