|
| 1 | +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +struct SearchQuery: Hashable, Codable { |
| 18 | + let searchID: UUID |
| 19 | + let query: String |
| 20 | +} |
| 21 | + |
| 22 | +struct SearchResultFragment: Hashable, Codable { |
| 23 | + let searchID: UUID |
| 24 | + let result: Search.Result? |
| 25 | + |
| 26 | + enum CodingKeys: String, CodingKey { |
| 27 | + case searchID = "id" |
| 28 | + case result = "r" |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +enum SearchLogger { |
| 33 | + static func log(query: String, results: [Search.Result]) { |
| 34 | + guard Current.environment() != .production else { return } |
| 35 | + let uniqueSearchID = UUID() |
| 36 | + let jsonEncoder = JSONEncoder() |
| 37 | + jsonEncoder.dateEncodingStrategy = .iso8601 |
| 38 | + jsonEncoder.outputFormatting = .sortedKeys |
| 39 | + |
| 40 | + let baseQuery = SearchQuery(searchID: uniqueSearchID, query: query) |
| 41 | + do { |
| 42 | + let stringdata = String(decoding: try jsonEncoder.encode(baseQuery), as: UTF8.self) |
| 43 | + AppEnvironment.logger.info("search: \(stringdata)") |
| 44 | + } catch { |
| 45 | + AppEnvironment.logger.warning("unable to encode search query: \(error)") |
| 46 | + } |
| 47 | + |
| 48 | + for (idx, result) in results.enumerated() { |
| 49 | + let fragment = SearchResultFragment(searchID: uniqueSearchID, result: result) |
| 50 | + do { |
| 51 | + let stringdata = String(decoding: try jsonEncoder.encode(fragment), as: UTF8.self) |
| 52 | + AppEnvironment.logger.info("searchresult[\(idx)]: \(stringdata)") |
| 53 | + } catch { |
| 54 | + AppEnvironment.logger.warning("unable to encode search fragment: \(error)") |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments