eia-api-go/pkg/eia/eia_logging.go

38 lines
828 B
Go
Raw Permalink Normal View History

2024-11-16 19:02:46 +00:00
package eia
import (
"context"
2024-12-11 20:41:09 +00:00
"errors"
2024-11-16 19:02:46 +00:00
"net/http"
"time"
"github.com/rs/zerolog"
2024-12-17 21:02:58 +00:00
eiaapi "gitea.libretechconsulting.com/rmcguire/eia-api-go/api"
2024-11-16 19:02:46 +00:00
)
func newLoggingMiddleware(logger *zerolog.Logger, level zerolog.Level) eiaapi.RequestEditorFn {
return func(_ context.Context, req *http.Request) error {
2024-12-11 20:41:09 +00:00
if req == nil {
return errors.New("invalid nil http request")
} else if req.URL == nil {
return errors.New("missing request url")
} else if req.Method == "" {
return errors.New("missing request method")
}
2024-11-27 18:52:15 +00:00
// Don't log api_key
params := req.URL.Query()
delete(params, "api_key")
2024-11-16 19:02:46 +00:00
logger.WithLevel(level).
Str("method", req.Method).
Str("host", req.URL.Host).
Str("path", req.URL.Path).
2024-11-27 18:52:15 +00:00
Str("query", params.Encode()).
2024-11-16 19:02:46 +00:00
Time("timestamp", time.Now()).
Send()
return nil
}
}