GRPCLogger.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2021, 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 Logging
  17. /// Wraps `Logger` to always provide the source as "GRPC".
  18. ///
  19. /// See https://github.com/apple/swift-log/issues/145 for rationale.
  20. internal struct GRPCLogger {
  21. private var logger: Logger
  22. internal var unwrapped: Logger {
  23. return self.logger
  24. }
  25. internal init(wrapping logger: Logger) {
  26. self.logger = logger
  27. }
  28. internal subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
  29. get {
  30. return self.logger[metadataKey: metadataKey]
  31. }
  32. set {
  33. self.logger[metadataKey: metadataKey] = newValue
  34. }
  35. }
  36. internal func trace(
  37. _ message: @autoclosure () -> Logger.Message,
  38. metadata: @autoclosure () -> Logger.Metadata? = nil,
  39. file: String = #file,
  40. function: String = #function,
  41. line: UInt = #line
  42. ) {
  43. self.logger.trace(
  44. message(),
  45. metadata: metadata(),
  46. source: "GRPC",
  47. file: file,
  48. function: function,
  49. line: line
  50. )
  51. }
  52. internal func debug(
  53. _ message: @autoclosure () -> Logger.Message,
  54. metadata: @autoclosure () -> Logger.Metadata? = nil,
  55. file: String = #file,
  56. function: String = #function,
  57. line: UInt = #line
  58. ) {
  59. self.logger.debug(
  60. message(),
  61. metadata: metadata(),
  62. source: "GRPC",
  63. file: file,
  64. function: function,
  65. line: line
  66. )
  67. }
  68. }