2
0

Logger.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import Logging
  18. extension Logger {
  19. /// Create a logger and attach the given UUID as the request-id metadata.
  20. init(label: String, requestID: String?) {
  21. self.init(label: label)
  22. if let requestID = requestID {
  23. self[metadataKey: MetadataKey.requestID] = "\(requestID)"
  24. }
  25. }
  26. /// Create a logger for the given subsystem and attach the given UUID as the request-id metadata.
  27. init(subsystem: Subsystem, requestID: String? = nil) {
  28. self.init(label: "io.grpc.\(subsystem.rawValue)", requestID: requestID)
  29. }
  30. /// Create a logger with a label in the format: `"io.grpc.\(suffix)"`.
  31. init(labelSuffix suffix: String) {
  32. self.init(label: "io.grpc.\(suffix)")
  33. }
  34. /// Creates a copy of the logger and sets metadata with the given key and value on the copy.
  35. func addingMetadata(key: String, value: MetadataValue) -> Logger {
  36. var newLogger = self
  37. newLogger[metadataKey: key] = value
  38. return newLogger
  39. }
  40. /// Labels for logging subsystems.
  41. enum Subsystem: String {
  42. case connectivityState = "connectivity_state"
  43. case clientChannel = "client_channel"
  44. case clientChannelCall = "client_channel_call"
  45. case messageReader = "message_reader"
  46. case nio = "nio"
  47. }
  48. }
  49. /// Keys for `Logger` metadata.
  50. enum MetadataKey {
  51. static let requestID = "request-id"
  52. static let responseType = "response-type"
  53. static let channelHandler = "channel-handler"
  54. static let connectionID = "connection-id"
  55. static let error = "error"
  56. }