Skip to content

Commit

Permalink
feat: low level logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Jul 11, 2024
1 parent f016efa commit d51f7b9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
57 changes: 52 additions & 5 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
)

Expand All @@ -19,14 +20,16 @@ type Builder struct {
writer func(writer http.ResponseWriter)
}

func writeHeaders(writer http.ResponseWriter, builder Builder) {
func writeHeaders(writer http.ResponseWriter, builder Builder) bool {
for key, values := range builder.headers {
for _, value := range values {
writer.Header().Add(key, value)
}
}

writer.WriteHeader(builder.status)

return builder.status >= 500 && builder.status < 600
}

func DefaultResponderHandler(writer http.ResponseWriter, request *http.Request, builder Builder) {
Expand All @@ -38,7 +41,18 @@ func DefaultResponderHandler(writer http.ResponseWriter, request *http.Request,
}

if builder.writer != nil {
writeHeaders(writer, builder)
if writeHeaders(writer, builder) {
logger := request.Context().Value(LoggerKey{}).(*slog.Logger)

logger.Error(
"detected server error",
"code", builder.status,
"text", http.StatusText(builder.status),
"url", request.URL.String(),
"kind", "writer",
)
}

builder.writer(writer)
return
}
Expand All @@ -54,14 +68,37 @@ func DefaultResponderHandler(writer http.ResponseWriter, request *http.Request,
return
}

writeHeaders(writer, builder)
if writeHeaders(writer, builder) {
logger := request.Context().Value(LoggerKey{}).(*slog.Logger)

logger.Error(
"detected server error",
"code", builder.status,
"text", http.StatusText(builder.status),
"url", request.URL.String(),
"kind", "body",
"body", string(body),
)
}

writer.Write(body)

return
}

if builder.stream != nil {
writeHeaders(writer, builder)
if writeHeaders(writer, builder) {
logger := request.Context().Value(LoggerKey{}).(*slog.Logger)

logger.Error(
"detected server error",
"code", builder.status,
"text", http.StatusText(builder.status),
"url", request.URL.String(),
"kind", "stream",
)
}

writer.(http.Flusher).Flush()

for {
Expand All @@ -79,7 +116,17 @@ func DefaultResponderHandler(writer http.ResponseWriter, request *http.Request,
}
}

writeHeaders(writer, builder)
if writeHeaders(writer, builder) {
logger := request.Context().Value(LoggerKey{}).(*slog.Logger)

logger.Error(
"detected server error",
"code", builder.status,
"text", http.StatusText(builder.status),
"url", request.URL.String(),
"kind", "default",
)
}
}

func Response(status int) Builder {
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package akumu

type LoggerKey struct{}
17 changes: 17 additions & 0 deletions middleware/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package middleware

import (
"context"
"log/slog"
"net/http"

"github.com/studiolambda/akumu"
)

func Logger(handler http.Handler, logger *slog.Logger) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
handler.ServeHTTP(writer, request.WithContext(
context.WithValue(request.Context(), akumu.LoggerKey{}, logger),
))
})
}

0 comments on commit d51f7b9

Please sign in to comment.