Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
troydai committed Nov 28, 2024
1 parent ffcb17e commit fa00824
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package main

import (
"log/slog"
"net/http"
"os"
"strconv"
"sync/atomic"
)

func (s *server) handler(w http.ResponseWriter, r *http.Request) {
s.logger.Info("inbound", "method", r.Method, "url", r.URL.Path, "host", r.Host, "proto", r.Proto, "agent", r.UserAgent(), "peer", r.RemoteAddr, "counter", s.counter.Load())

if newValue := s.counter.Add(1); newValue%s.frequency == 0 {
s.logger.Warn("crash", "counter", newValue)
panic("crash after 10 requests")
}

s.logger.Info("outbound", "status", 200)
w.WriteHeader(200)
}

type server struct {
counter *atomic.Uint64
frequency uint64
logger *slog.Logger
}

func main() {
Expand All @@ -30,6 +36,7 @@ func main() {
s := &server{
counter: &atomic.Uint64{},
frequency: frequency,
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}

http.HandleFunc("/", s.handler)
Expand Down

0 comments on commit fa00824

Please sign in to comment.