feat: use info level for access log to the collaboration service

This commit is contained in:
Juan Pablo Villafáñez
2024-07-18 10:48:08 +02:00
parent 3e3fcf3729
commit 0a33b934c1
2 changed files with 38 additions and 1 deletions
@@ -0,0 +1,37 @@
package middleware
import (
"net/http"
"time"
"github.com/go-chi/chi/v5/middleware"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"go.opentelemetry.io/otel/trace"
)
// AccessLog is a middleware to log http requests at info level logging.
func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
requestID := middleware.GetReqID(r.Context())
// add Request Id to all responses
w.Header().Set(middleware.RequestIDHeader, requestID)
wrap := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(wrap, r)
spanContext := trace.SpanContextFromContext(r.Context())
logger.Info().
Str("proto", r.Proto).
Str(log.RequestIDString, requestID).
Str("traceid", spanContext.TraceID().String()).
Str("remote-addr", r.RemoteAddr).
Str("method", r.Method).
Int("status", wrap.Status()).
Str("path", r.URL.Path).
Dur("duration", time.Since(start)).
Int("bytes", wrap.BytesWritten()).
Msg("access-log")
})
}
}
@@ -44,7 +44,7 @@ func Server(opts ...Option) (http.Service, error) {
options.Config.Service.Name+"."+options.Config.App.Name,
version.GetString(),
),
middleware.Logger(
colabmiddleware.AccessLog(
options.Logger,
),
middleware.ExtractAccountUUID(