|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +#if canImport(Darwin) |
| 15 | +import OpenAPIRuntime |
| 16 | +import Foundation |
| 17 | +import HTTPTypes |
| 18 | +import OSLog |
| 19 | + |
| 20 | +package actor LoggingMiddleware { |
| 21 | + private let logger: Logger |
| 22 | + package let bodyLoggingPolicy: BodyLoggingPolicy |
| 23 | + |
| 24 | + package init(logger: Logger = defaultLogger, bodyLoggingConfiguration: BodyLoggingPolicy = .never) { |
| 25 | + self.logger = logger |
| 26 | + self.bodyLoggingPolicy = bodyLoggingConfiguration |
| 27 | + } |
| 28 | + |
| 29 | + fileprivate static var defaultLogger: Logger { |
| 30 | + Logger(subsystem: "com.apple.swift-openapi", category: "logging-middleware") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension LoggingMiddleware: ClientMiddleware { |
| 35 | + package func intercept( |
| 36 | + _ request: HTTPRequest, |
| 37 | + body: HTTPBody?, |
| 38 | + baseURL: URL, |
| 39 | + operationID: String, |
| 40 | + next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?) |
| 41 | + ) async throws -> (HTTPResponse, HTTPBody?) { |
| 42 | + let (requestBodyToLog, requestBodyForNext) = try await bodyLoggingPolicy.process(body) |
| 43 | + log(request, requestBodyToLog) |
| 44 | + do { |
| 45 | + let (response, responseBody) = try await next(request, requestBodyForNext, baseURL) |
| 46 | + let (responseBodyToLog, responseBodyForNext) = try await bodyLoggingPolicy.process(responseBody) |
| 47 | + log(request, response, responseBodyToLog) |
| 48 | + return (response, responseBodyForNext) |
| 49 | + } catch { |
| 50 | + log(request, failedWith: error) |
| 51 | + throw error |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +extension LoggingMiddleware: ServerMiddleware { |
| 57 | + package func intercept( |
| 58 | + _ request: HTTPTypes.HTTPRequest, |
| 59 | + body: OpenAPIRuntime.HTTPBody?, |
| 60 | + metadata: OpenAPIRuntime.ServerRequestMetadata, |
| 61 | + operationID: String, |
| 62 | + next: @Sendable (HTTPTypes.HTTPRequest, OpenAPIRuntime.HTTPBody?, OpenAPIRuntime.ServerRequestMetadata) |
| 63 | + async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) |
| 64 | + ) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) { |
| 65 | + let (requestBodyToLog, requestBodyForNext) = try await bodyLoggingPolicy.process(body) |
| 66 | + log(request, requestBodyToLog) |
| 67 | + do { |
| 68 | + let (response, responseBody) = try await next(request, requestBodyForNext, metadata) |
| 69 | + let (responseBodyToLog, responseBodyForNext) = try await bodyLoggingPolicy.process(responseBody) |
| 70 | + log(request, response, responseBodyToLog) |
| 71 | + return (response, responseBodyForNext) |
| 72 | + } catch { |
| 73 | + log(request, failedWith: error) |
| 74 | + throw error |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension LoggingMiddleware { |
| 80 | + func log(_ request: HTTPRequest, _ requestBody: BodyLoggingPolicy.BodyLog) { |
| 81 | + logger.debug( |
| 82 | + "Request: \(request.method, privacy: .public) \(request.path ?? "<nil>", privacy: .public) body: \(requestBody, privacy: .auto)" |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + func log(_ request: HTTPRequest, _ response: HTTPResponse, _ responseBody: BodyLoggingPolicy.BodyLog) { |
| 87 | + logger.debug( |
| 88 | + "Response: \(request.method, privacy: .public) \(request.path ?? "<nil>", privacy: .public) \(response.status, privacy: .public) body: \(responseBody, privacy: .auto)" |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + func log(_ request: HTTPRequest, failedWith error: any Error) { |
| 93 | + logger.warning("Request failed. Error: \(error.localizedDescription)") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +package enum BodyLoggingPolicy { |
| 98 | + /// Never log request or response bodies. |
| 99 | + case never |
| 100 | + /// Log request and response bodies that have a known length less than or equal to `maxBytes`. |
| 101 | + case upTo(maxBytes: Int) |
| 102 | + |
| 103 | + enum BodyLog: Equatable, CustomStringConvertible { |
| 104 | + /// There is no body to log. |
| 105 | + case none |
| 106 | + /// The policy forbids logging the body. |
| 107 | + case redacted |
| 108 | + /// The body was of unknown length. |
| 109 | + case unknownLength |
| 110 | + /// The body exceeds the maximum size for logging allowed by the policy. |
| 111 | + case tooManyBytesToLog(Int64) |
| 112 | + /// The body can be logged. |
| 113 | + case complete(Data) |
| 114 | + |
| 115 | + var description: String { |
| 116 | + switch self { |
| 117 | + case .none: return "<none>" |
| 118 | + case .redacted: return "<redacted>" |
| 119 | + case .unknownLength: return "<unknown length>" |
| 120 | + case .tooManyBytesToLog(let byteCount): return "<\(byteCount) bytes>" |
| 121 | + case .complete(let data): |
| 122 | + if let string = String(data: data, encoding: .utf8) { return string } |
| 123 | + return String(describing: data) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + func process(_ body: HTTPBody?) async throws -> (bodyToLog: BodyLog, bodyForNext: HTTPBody?) { |
| 129 | + switch (body?.length, self) { |
| 130 | + case (.none, _): return (.none, body) |
| 131 | + case (_, .never): return (.redacted, body) |
| 132 | + case (.unknown, _): return (.unknownLength, body) |
| 133 | + case (.known(let length), .upTo(let maxBytesToLog)) where length > maxBytesToLog: |
| 134 | + return (.tooManyBytesToLog(length), body) |
| 135 | + case (.known, .upTo(let maxBytesToLog)): |
| 136 | + let bodyData = try await Data(collecting: body!, upTo: maxBytesToLog) |
| 137 | + return (.complete(bodyData), HTTPBody(bodyData)) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +#endif // canImport(Darwin) |
0 commit comments