File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change 1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ import Dependencies
1516import Vapor
1617
18+
1719// Replica of the Vapor RouteLoggingMiddleware that's tweaked to explicitly expose the cf-ray header in the logger metadata for the request.
1820public final class CFRayRouteLoggingMiddleware : Middleware {
1921 public let logLevel : Logger . Level
20-
22+
2123 public init ( logLevel: Logger . Level = . info) {
2224 self . logLevel = logLevel
2325 }
24-
26+
2527 public func respond( to request: Request , chainingTo next: Responder ) -> EventLoopFuture < Response > {
28+ @Dependency ( \. environment) var environment
29+
30+ guard environment. enableCFRayLogging ( ) else {
31+ return next. respond ( to: request)
32+ }
2633 guard let cfray = request. headers. first ( name: " cf-ray " ) else {
2734 return next. respond ( to: request)
2835 }
36+
2937 request. logger [ metadataKey: " cf-ray " ] = . string( cfray)
3038 request. logger. log ( level: self . logLevel, " \( request. method) \( request. url. path. removingPercentEncoding ?? request. url. path) " )
3139 return next. respond ( to: request)
You can’t perform that action at this time.
0 commit comments