GRPCLogger.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. import NIOCore
  18. /// Wraps `Logger` to always provide the source as "GRPC".
  19. ///
  20. /// See https://github.com/apple/swift-log/issues/145 for rationale.
  21. @usableFromInline
  22. internal struct GRPCLogger {
  23. @usableFromInline
  24. internal var logger: Logger
  25. internal var unwrapped: Logger {
  26. return self.logger
  27. }
  28. @inlinable
  29. internal init(wrapping logger: Logger) {
  30. self.logger = logger
  31. }
  32. internal subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
  33. get {
  34. return self.logger[metadataKey: metadataKey]
  35. }
  36. set {
  37. self.logger[metadataKey: metadataKey] = newValue
  38. }
  39. }
  40. @usableFromInline
  41. internal func trace(
  42. _ message: @autoclosure () -> Logger.Message,
  43. metadata: @autoclosure () -> Logger.Metadata? = nil,
  44. file: String = #fileID,
  45. function: String = #function,
  46. line: UInt = #line
  47. ) {
  48. self.logger.trace(
  49. message(),
  50. metadata: metadata(),
  51. source: "GRPC",
  52. file: file,
  53. function: function,
  54. line: line
  55. )
  56. }
  57. @usableFromInline
  58. internal func debug(
  59. _ message: @autoclosure () -> Logger.Message,
  60. metadata: @autoclosure () -> Logger.Metadata? = nil,
  61. file: String = #fileID,
  62. function: String = #function,
  63. line: UInt = #line
  64. ) {
  65. self.logger.debug(
  66. message(),
  67. metadata: metadata(),
  68. source: "GRPC",
  69. file: file,
  70. function: function,
  71. line: line
  72. )
  73. }
  74. @usableFromInline
  75. internal func notice(
  76. _ message: @autoclosure () -> Logger.Message,
  77. metadata: @autoclosure () -> Logger.Metadata? = nil,
  78. file: String = #fileID,
  79. function: String = #function,
  80. line: UInt = #line
  81. ) {
  82. self.logger.notice(
  83. message(),
  84. metadata: metadata(),
  85. source: "GRPC",
  86. file: file,
  87. function: function,
  88. line: line
  89. )
  90. }
  91. @usableFromInline
  92. internal func warning(
  93. _ message: @autoclosure () -> Logger.Message,
  94. metadata: @autoclosure () -> Logger.Metadata? = nil,
  95. file: String = #fileID,
  96. function: String = #function,
  97. line: UInt = #line
  98. ) {
  99. self.logger.warning(
  100. message(),
  101. metadata: metadata(),
  102. source: "GRPC",
  103. file: file,
  104. function: function,
  105. line: line
  106. )
  107. }
  108. }
  109. extension GRPCLogger {
  110. internal mutating func addIPAddressMetadata(local: SocketAddress?, remote: SocketAddress?) {
  111. self.logger.addIPAddressMetadata(local: local, remote: remote)
  112. }
  113. }
  114. extension Logger {
  115. @inlinable
  116. internal var wrapped: GRPCLogger {
  117. return GRPCLogger(wrapping: self)
  118. }
  119. }