github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/pkg/logger/http.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package logger
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"go.uber.org/zap"
    23  )
    24  
    25  type injectLogger struct {
    26  	logger *zap.Logger
    27  	inner  http.Handler
    28  }
    29  
    30  func (i *injectLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    31  	ctx := WithLogger(r.Context(), i.logger)
    32  	r2 := r.Clone(ctx)
    33  	i.inner.ServeHTTP(w, r2)
    34  }
    35  
    36  func WithHttpLogger(logger *zap.Logger, inner http.Handler) http.Handler {
    37  	return &injectLogger{
    38  		logger: logger,
    39  		inner:  inner,
    40  	}
    41  }