Added access log to server and modified mock-fts to use it

This commit is contained in:
bwilliams committed 2026-10-16 08:38:41 +01:00
1 parent 2490519c2b
commit 02e3d910a2
10 files changed
+389 -29

No files matched your search

+30
View File
@@ -5,6 +5,8 @@ import (
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@@ -26,12 +28,20 @@ func (s *statusRecorder) WriteHeader(code int) {
s.ResponseWriter.WriteHeader(code)
}
// Unwrap returns the wrapped ResponseWriter, so that http.ResponseController
// can reach its Hijack and Flush: a WebSocket upgrade hijacks the connection.
func (s *statusRecorder) Unwrap() http.ResponseWriter {
return s.ResponseWriter
}
func (s *statusRecorder) Write(data []byte) (int, error) {
if s.status == 0 {
s.status = http.StatusOK
}
written, err := s.ResponseWriter.Write(data)
s.bytes += written
return written, err
}
@@ -108,3 +118,23 @@ func AccessLog(
logger.Println(line)
})
}
// OpenAccessLog opens the access log at path for appending, creating it if
// needed, or stdout for "-", for AccessLog. It returns a logger writing to it
// and a function that closes it (doing nothing for stdout).
func OpenAccessLog(path string) (*log.Logger, func() error, error) {
if path == "-" {
return log.New(os.Stdout, "", 0), func() error {
return nil
}, nil
}
file, err := os.OpenFile(
filepath.Clean(path), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600,
)
if err != nil {
return nil, nil, fmt.Errorf("opening access log: %w", err)
}
return log.New(file, "", 0), file.Close, nil
}