eia-api-go/pkg/eia/eia_logging.go
Ryan McGuire 7431eb94f1
All checks were successful
Build and Publish / build (push) Successful in 46s
Build and Publish / publish (push) Successful in 5s
Move to public repo
2024-12-17 16:02:58 -05:00

38 lines
828 B
Go

package eia
import (
"context"
"errors"
"net/http"
"time"
"github.com/rs/zerolog"
eiaapi "gitea.libretechconsulting.com/rmcguire/eia-api-go/api"
)
func newLoggingMiddleware(logger *zerolog.Logger, level zerolog.Level) eiaapi.RequestEditorFn {
return func(_ context.Context, req *http.Request) error {
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")
}
// Don't log api_key
params := req.URL.Query()
delete(params, "api_key")
logger.WithLevel(level).
Str("method", req.Method).
Str("host", req.URL.Host).
Str("path", req.URL.Path).
Str("query", params.Encode()).
Time("timestamp", time.Now()).
Send()
return nil
}
}