DDLogMessage+SwiftLogInformation.swift 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. public import CocoaLumberjack
  16. public import Logging
  17. extension DDLogMessage {
  18. /// Contains the swift-log details of a given log message.
  19. public struct SwiftLogInformation: Equatable, Sendable {
  20. /// Contains information about the swift-log logger that logged this message.
  21. public struct LoggerInformation: Equatable, Sendable {
  22. /// Contains the metadata from the various sources of on a logger.
  23. /// Currently this can be the logger itself, as well as its metadata provider
  24. public struct MetadataSources: Equatable, Sendable {
  25. /// The metadata of the swift-log logger that logged this message.
  26. public let logger: Logging.Logger.Metadata
  27. /// The metadata of the metadata provider on the swift-log logger that logged this message.
  28. public let provider: Logging.Logger.Metadata?
  29. }
  30. /// The label of the swift-log logger that logged this message.
  31. public let label: String
  32. /// The metadata of the swift-log logger that logged this message.
  33. public let metadataSources: MetadataSources
  34. /// The metadata of the swift-log logger that logged this message.
  35. @available(*, deprecated, renamed: "metadataSources.logger")
  36. public var metadata: Logging.Logger.Metadata { metadataSources.logger }
  37. }
  38. /// Contains information about the swift-log message thas was logged.
  39. public struct MessageInformation: Equatable, Sendable {
  40. /// The original swift-log message.
  41. public let message: Logging.Logger.Message
  42. /// The original swift-log level of the message. This could be more fine-grained than ``DDLogMessage/level`` & ``DDLogMessage/flag``.
  43. public let level: Logging.Logger.Level
  44. /// The original swift-log metadata of the message.
  45. public let metadata: Logging.Logger.Metadata?
  46. /// The original swift-log source of the message.
  47. public let source: String
  48. }
  49. /// The information about the swift-log logger that logged this message.
  50. public let logger: LoggerInformation
  51. /// The information about the swift-log message that was logged.
  52. public let message: MessageInformation
  53. /// Merges the metadata from all layers together.
  54. /// The metadata on the logger provides the base.
  55. /// Metadata from the logger's metadata provider (if any) trumps the base.
  56. /// Metadata from the logged message again trumps both the base and the metadata from the logger's metadata provider.
  57. /// Essentially: `logger.metadata < logger.metadataProvider < message.metadata`
  58. /// - Note: Accessing this property performs the merge! Accessing it multiple times can be a performance issue!
  59. public var mergedMetadata: Logging.Logger.Metadata {
  60. var merged = logger.metadataSources.logger
  61. if let providerMetadata = logger.metadataSources.provider {
  62. merged.merge(providerMetadata, uniquingKeysWith: { $1 })
  63. }
  64. if let messageMetadata = message.metadata {
  65. merged.merge(messageMetadata, uniquingKeysWith: { $1 })
  66. }
  67. return merged
  68. }
  69. }
  70. /// The swift-log information of this log message. This only exists for messages logged via swift-log.
  71. @inlinable
  72. public var swiftLogInfo: SwiftLogInformation? {
  73. (self as? SwiftLogMessage)?._swiftLogInfo
  74. }
  75. }