diff options
author | Alan Pearce | 2024-05-19 15:42:36 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-19 15:53:32 +0200 |
commit | 09ee2de72936ea0b0edc5d3360ce1c72803a817b (patch) | |
tree | 427a590fba06e58554586aad8995fcb6e7a559f3 /internal/server | |
parent | 4064f933a965f2fb2ce43d3c2d5bd54ccd33978c (diff) | |
download | website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.lz website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.zst website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.zip |
log in logfmt via zap, with nicer console output in dev
Diffstat (limited to 'internal/server')
-rw-r--r-- | internal/server/filemap.go | 8 | ||||
-rw-r--r-- | internal/server/server.go | 19 |
2 files changed, 13 insertions, 14 deletions
diff --git a/internal/server/filemap.go b/internal/server/filemap.go index 466db49..6130e65 100644 --- a/internal/server/filemap.go +++ b/internal/server/filemap.go @@ -5,12 +5,12 @@ import ( "hash/fnv" "io" "io/fs" - "log" - "log/slog" "os" "path/filepath" "strings" + "website/internal/log" + "github.com/pkg/errors" ) @@ -36,7 +36,7 @@ func hashFile(filename string) (string, error) { func registerFile(urlpath string, filepath string) error { if files[urlpath] != (File{}) { - log.Printf("registerFile called with duplicate file, urlPath: %s", urlpath) + log.Info("registerFile called with duplicate file", "url_path", urlpath) return nil } hash, err := hashFile(filepath) @@ -61,7 +61,7 @@ func registerContentFiles(root string) error { } urlPath, _ := strings.CutSuffix(relPath, "index.html") if !f.IsDir() { - slog.Debug("registering file", "urlpath", "/"+urlPath) + log.Debug("registering file", "urlpath", "/"+urlPath) return registerFile("/"+urlPath, filePath) } return nil diff --git a/internal/server/server.go b/internal/server/server.go index cbee989..62e32d5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -4,8 +4,6 @@ import ( "context" "fmt" "io" - "log" - "log/slog" "mime" "net" "net/http" @@ -16,6 +14,7 @@ import ( "time" cfg "website/internal/config" + "website/internal/log" "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" @@ -89,7 +88,7 @@ func (fn webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { if fail := recover(); fail != nil { w.WriteHeader(http.StatusInternalServerError) - slog.Error("runtime panic!", "error", fail) + log.Error("runtime panic!", "error", fail) } }() w.Header().Set("Server", fmt.Sprintf("website (%s)", ShortSHA)) @@ -111,7 +110,7 @@ var newMIMEs = map[string]string{ func fixupMIMETypes() { for ext, newType := range newMIMEs { if err := mime.AddExtensionType(ext, newType); err != nil { - slog.Error("could not update mime type", "ext", ext, "mime", newType) + log.Error("could not update mime type", "ext", ext, "mime", newType) } } } @@ -134,7 +133,7 @@ func New(runtimeConfig *Config) (*Server, error) { } prefix := path.Join(runtimeConfig.Root, "public") - slog.Debug("registering content files", "prefix", prefix) + log.Debug("registering content files", "prefix", prefix) err = registerContentFiles(prefix) if err != nil { return nil, errors.WithMessagef(err, "registering content files") @@ -161,7 +160,7 @@ func New(runtimeConfig *Config) (*Server, error) { top := http.NewServeMux() mux := http.NewServeMux() - slog.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") + log.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") mux.Handle(runtimeConfig.BaseURL.Hostname()+"/", webHandler(serveFile)) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -205,19 +204,19 @@ func (s *Server) Start() error { } func (s *Server) Stop() chan struct{} { - slog.Debug("stop called") + log.Debug("stop called") idleConnsClosed := make(chan struct{}) go func() { - slog.Debug("shutting down server") + log.Debug("shutting down server") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err := s.Server.Shutdown(ctx) - slog.Debug("server shut down") + log.Debug("server shut down") if err != nil { // Error from closing listeners, or context timeout: - log.Printf("HTTP server Shutdown: %v", err) + log.Warn("HTTP server Shutdown", "error", err) } close(idleConnsClosed) }() |