Logger.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 metadata.
  20. init(label: String, metadata: Metadata) {
  21. self.init(label: label)
  22. for (key, value) in metadata {
  23. self[metadataKey: key] = value
  24. }
  25. }
  26. /// Create a logger for the given subsystem and attach the given metadata.
  27. init(subsystem: Subsystem, metadata: Metadata = [:]) {
  28. self.init(label: "io.grpc.\(subsystem.rawValue)", metadata: metadata)
  29. }
  30. /// Create a logger with a label in the format: `"io.grpc.\(suffix)"`.
  31. init(labelSuffix suffix: String, metadata: Metadata = [:]) {
  32. self.init(label: "io.grpc.\(suffix)", metadata: metadata)
  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 serverChannelCall = "server_channel_call"
  46. case serverAccess = "server_access"
  47. case messageReader = "message_reader"
  48. case nio = "nio"
  49. }
  50. }
  51. /// Keys for `Logger` metadata.
  52. enum MetadataKey {
  53. static let requestID = "request_id"
  54. static let responseType = "response_type"
  55. static let channelHandler = "channel_handler"
  56. static let connectionID = "connection_id"
  57. static let error = "error"
  58. }