Skip to content

Commit 66df15e

Browse files
committed
Toggle logging according to env variable
1 parent 9a1a097 commit 66df15e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/App/Core/CFRayRouteLoggingMiddleware.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Dependencies
1516
import 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.
1820
public 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)

0 commit comments

Comments
 (0)