Skip to content

Commit

Permalink
Print in a place that hopefully survives more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-inside committed Oct 26, 2023
1 parent a361687 commit db0fbc9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cmd/http-log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ func main() {
}

loggingMux := handlers.NewLogMiddle(
op,
actionMux,
)

Expand Down Expand Up @@ -337,11 +336,16 @@ func main() {
// Called when an http server connection changes state
// Called by h2.
ConnState: func(c net.Conn, cs http.ConnState) {
_, log := fromHackStore(c, log)
ctx, log := fromHackStore(c, log)

log.Info("Connection state change", "state", cs)

if cs == http.StateClosed {
srvData := ctxt.SrvDataFromContext(ctx)
reqData := ctxt.ReqDataFromContext(ctx)
respData := ctxt.RespDataFromContext(ctx)
op.Output(srvData, reqData, respData)

delete(hackStore, c.RemoteAddr())
log.Debug("Connection closed; removing entry from hackStore", "remoteAddr", c.RemoteAddr())
}
Expand Down
2 changes: 1 addition & 1 deletion melange.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package:
name: http-log
epoch: 0
version: v0.7.17-8-gb993e41-dirty
version: v0.7.17-9-ga361687-dirty
description: Logs HTTP requests
url: https://github.com/mt-inside/http-log
copyright:
Expand Down
7 changes: 1 addition & 6 deletions pkg/handlers/log_middle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ import (
"github.com/mt-inside/http-log/internal/ctxt"
"github.com/mt-inside/http-log/pkg/enricher"
"github.com/mt-inside/http-log/pkg/extractor"
"github.com/mt-inside/http-log/pkg/output"
"github.com/mt-inside/http-log/pkg/parser"
)

type LogMiddle struct {
op output.Renderer
next http.Handler
}

func NewLogMiddle(
op output.Renderer,
next http.Handler,
) http.Handler {
return &LogMiddle{
op,
next,
}
}
Expand All @@ -38,7 +34,6 @@ func (lm LogMiddle) ServeHTTP(w http.ResponseWriter, r *http.Request) {

srvData := ctxt.SrvDataFromContext(ctx)
reqData := ctxt.ReqDataFromContext(ctx)
respData := ctxt.RespDataFromContext(ctx)

/* Record request info */

Expand All @@ -63,7 +58,7 @@ func (lm LogMiddle) ServeHTTP(w http.ResponseWriter, r *http.Request) {

/* Print */

lm.op.Output(srvData, reqData, respData)
// Printing is done when ConnState(closed)

/* Done */

Expand Down

0 comments on commit db0fbc9

Please sign in to comment.